I have got a Samsung Galaxy S4 Active
When I execute on Android, the following code:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
I obtain
width = 1080
heigh = 1920
dens = 480
So, is the used formula is correct, screenInches
is 4.589. But the specification said that the screenInches size is 5.
I have also tried with my Samsung Galaxy SII that gives me:
width = 480
heigh = 800
dens = 240
that corresponds to a screenInches
of 3.887 instead of 4.3 (as said in the specs)
Why there is this difference between the declared size in inches and the size I obtain?
EDIT: It seems that densityDpi returns one of these values: (120, 160, 213, 240, 320, 480 or 640 dpi).
I don't think that you can get the real screen density from DisplayMetrics.densityDpi. It can return only one of particular constant value. DisplayMetrics documentation says that
But Android Compatibility Definition Document (CDD). provides a more comprehensive explanation
You can get the exact physical pixels per inch of the screen in the X and Y dimension from DespalyMetrics.xdpi and DespalyMetrics.ydpi
densityDpi
returns the screen density expressed as dots-per-inch, but this value is approximated.densityDpi
is calculated fromdensity
which is a logical density of display.This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.As said in the documentation:
So in order to obtain the correct DPI I can use
xdpi
andydpi
.Applying these to my Samsung Galaxy S4 Active, I obtain:
xdpi = 442.451 ydpi = 439.351
My phone spec said ~441 ppi pixel density, so I think these value are correct.
So, getting the average value 440.901 DPI the result is: