【问题标题】:How I detect the screen size of android phone [duplicate]我如何检测android手机的屏幕尺寸[重复]
【发布时间】:2013-09-06 07:56:22
【问题描述】:

如何以英寸为单位检测 android 手机的屏幕尺寸?getSize 方法从像素返回尺寸,但是当适应不同的屏幕尺寸时它不起作用。在这种情况下,屏幕具有相同的像素尺寸但以英寸为单位更小某些视图在屏幕上没有获得所需的空间。如果无法以英寸为单位获得大小,可以使用像素大小做同样的事情吗?

【问题讨论】:

标签: android


【解决方案1】:

使用以下内容:

DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels/dm.xdpi,2);
    double y = Math.pow(dm.heightPixels/dm.ydpi,2);
    double screenInches = Math.sqrt(x+y);

【讨论】:

【解决方案2】:

如果您想要以像素为单位的显示尺寸,您可以使用以下代码:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

然后您可以添加比较高度以满足您的需求的条件。

英寸:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screenInches);

希望这会对你有所帮助。

【讨论】:

  • 同样的评论,除了你的代表你可能会要求问题被关闭为重复
【解决方案3】:

您将通过,,, 正确地将像素转换为英寸

要获得 a 的大小,可以说 1pixel = x 英寸。让这个人拿一把尺子放在屏幕上,测量一条线的宽度,例如320 像素的线。然后你会知道 320pixels = x 英寸,给定 x 320/x = 1 英寸。

或者

对于您的地图,只需让 x 像素 = y 英寸,地图将根据不同的因素进行垂直和水平缩放,只要您显示水平和垂直比例,人们就会知道大小。

编辑

嘿,我有一个更好的方法来做到这一点,但是当屏幕没有填满整个显示器时(通常在 CRT 显示器上)它不起作用。只需询问该人正在使用的显示器的尺寸以及您需要的一切设备。 1024x768、800x600、640x480的比例都是4/3,物理显示器的高宽也是4/3。 15" 显示器上移 9" 左移 12"。因此像素高度在 9" 中均分,宽度在 12" 中均分。因此:

x_screen_resolution = 12"
y_screen_resolution = 9"

现在您有了 15" 显示器从像素到英寸的转换系数。4/3 的比例可能是一个标准,所以如果您得到显示器尺寸的英寸 Z,那么

    x*x + y*y = Z*Z ... eq1
    x/y = 4/3 .... eq2

    x = 4y/3 ...

    16yy/9 + yy = ZZ
    25yy/16 = 9ZZ/16
    yy = 9ZZ/25
    -------------
    y = 3Z/5

    We now know y solve for screen width x
    x = sqrt(ZZ-yy)
    and thus 
    x_screen_resolution = x inches
    y_screen_resolution = y inches

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多