if you want to get the density of the screen, you can follow these code:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidthPixel = metrics.widthPixels;
System.out.println("the screen width pixel is:"+screenWidthPixel);
int density = metrics.densityDpi;
if(density==DisplayMetrics.DENSITY_LOW)
System.out.println("ldpi");
else if(density==DisplayMetrics.DENSITY_MEDIUM)
System.out.println("mdpi");
else if(density==DisplayMetrics.DENSITY_HIGH)
System.out.println("hdpi");
else if(density==DisplayMetrics.DENSITY_XHIGH)
System.out.println("xhdpi");
else
System.out.println("unknow");
By the way, how to calculate your device PPI?
First, get the device screen size, for example, if your device is Samsung S2 I9100, the screen size is 4.28".
Second, get the resolution of screen, for example, resolution of S2 is 800*480.
Third, Diagonal resolution = square root of (800^2+480^2).
Fourth, divide the diagonal resolution by the screen size 4.28.
Fifth, the PPI of Samsung S2 is 217.9.
If 120 > PPI, metrics.densityDpi = 120 = DENSITY_LOW,
160 > PPI >= 120, metrics.densityDpi = 160 = DENSITY_MEDIUM,
240 > PPI >= 160, metrics.densityDpi = 240 = DENSITY_HIGH,
PPI > 240, metrics.densityDpi = 320 = DENSITY_XHIGH.
if you want to get the overall layout of the screen, you can follow these code:
if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL)
System.out.println("This device is normal");
else if((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL)
System.out.println("This device is small");
else if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
System.out.println("This device is large");
else if(screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE)
System.out.println("This device is x-large");
else if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_UNDEFINED)
System.out.println("This device is undefined");
0 comments:
Post a Comment