Difference between the declared size in inches and the size obtained using DisplayMetrics

三世轮回 提交于 2019-12-11 06:22:18

问题


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).


回答1:


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

The screen density expressed as dots-per-inch. May be either DENSITY_LOW, DENSITY_MEDIUM, or DENSITY_HIGH.

But Android Compatibility Definition Document (CDD). provides a more comprehensive explanation

Screen Density
The Android UI framework defines a set of standard logical densities to help application developers target application resources. Device implementations MUST report one of the following logical Android framework densities through the android.util.DisplayMetrics APIs, and MUST execute applications at this standard density.
> 120 dpi, known as 'ldpi'
> 160 dpi, known as 'mdpi'
> 213 dpi, known as 'tvdpi'
> 240 dpi, known as 'hdpi'
> 320 dpi, known as 'xhdpi'
> 400 dpi, known as '400dpi'
> 480 dpi, known as 'xxhdpi'
> 640 dpi, known as 'xxxhdpi'
Device implementations SHOULD define the standard Android framework density that is numerically closest to the physical density of the screen, unless that logical density pushes the reported screen size below the minimum supported. If the standard Android framework density that is numerically closest to the physical density results in a screen size that is smaller than the smallest supported compatible screen size (320 dp width), device implementations SHOULD report the next lowest standard Android framework density.

You can get the exact physical pixels per inch of the screen in the X and Y dimension from DespalyMetrics.xdpi and DespalyMetrics.ydpi




回答2:


densityDpi returns the screen density expressed as dots-per-inch, but this value is approximated.

densityDpi is calculated from density 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:

This value does not exactly follow the real screen size (as given by xdpi and ydpi) , but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi.

So in order to obtain the correct DPI I can use xdpi and ydpi.

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:

screenInches = 4.9963


来源:https://stackoverflow.com/questions/23475200/difference-between-the-declared-size-in-inches-and-the-size-obtained-using-displ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!