如何在Android中获取以英寸为单位的显示尺寸?

问题描述:


对于我的应用程序,我需要显示器的确切英寸.我当前的解决方案是:


I need for my Application the exact inch of a display. My current solution is:

double inch;
double x = Math.pow(widthPix/dm.xdpi,2);
double y = Math.pow(heigthPix/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);

inch = screenInches;

inch = inch * 10;
inch = Math.round(inch);
inch = inch / 10;

是否有更好的方法来获得显示英寸?我的4英寸测试设备上的电流为5.9,这是错误的...

Is there a better way to get the display inch? Current on my test device 4 inch it say 5.9 and that is wrong...

以下内容为我提供了非常接近规格的结果:

The following gave me a a result quite close to the specs:

    DisplayMetrics dm = getResources().getDisplayMetrics();

    double density = dm.density * 160;
    double x = Math.pow(dm.widthPixels / density, 2);
    double y = Math.pow(dm.heightPixels / density, 2);
    double screenInches = Math.sqrt(x + y);
    log.info("inches: {}", screenInches);

输出:英寸:4.589389937671455
规格:三星Galaxy Nexus(720 x 1280像素,〜320 dpi,4.65英寸)

Output: inches: 4.589389937671455
Specs: Samsung Galaxy Nexus (720 x 1280 px, ~320 dpi, 4.65")

请注意, dm.heightPixels (或 dm.widthPixels 取决于您的方向)不一定提供准确的信息,因为软键(如Galaxy中使用的那样)Nexus)未添加到高度(或宽度)中.

Please note, that dm.heightPixels (or dm.widthPixels depending on your orientatation) does not necessarily provide accurate information, since soft keys (as used in the Galaxy Nexus) are not added to the height (or width).