如何在Android中获取以英寸为单位的显示尺寸? [英] How to get the Display Size in Inches in Android?

查看:58
本文介绍了如何在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).

这篇关于如何在Android中获取以英寸为单位的显示尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆