getting the screen density programmatically in android?

后端 未结 19 2653
感动是毒
感动是毒 2020-11-22 01:56

How to get the screen density programmatically in android?

I mean: How to find the screen dpi of the current device?

19条回答
  •  迷失自我
    2020-11-22 02:32

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    switch(metrics.densityDpi) {
         case DisplayMetrics.DENSITY_LOW:
             break;
    
         case DisplayMetrics.DENSITY_MEDIUM:
             break;
    
         case DisplayMetrics.DENSITY_HIGH:
             break;
    }
    

    This will work on API level 4 and higher.

提交回复
热议问题