如何获取设备的屏幕尺寸? [英] How to get the screen size of the device?

查看:25
本文介绍了如何获取设备的屏幕尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲望有 480 x 800 像素,3.7 英寸,HD 有 480 x 800 像素,4.3 英寸屏幕规格.

The desire have 480 x 800 pixels, 3.7 inches and the HD have 480 x 800 pixels, 4.3 inches screen specification.

我运行了该线程中被接受作为答案的代码 如何获取设备的屏幕尺寸?但它是错误的!例如3.7英寸的屏幕尺寸返回2.4这是非常错误的!

I run the code that is accepted as answer from this thread How to get screen size of device? but it is wrong !, for example for screen size of 3.7 inches returns 2.4 which is very wrong !

有人知道如何获得真实的屏幕尺寸吗?,我不需要分辨率我需要屏幕的屏幕尺寸(以英寸为单位).

Does anybody know how to get the real screen size ?, I do not need the resolution I need the screen size of the screen in inches.

注意:这段代码是错误的(至少对我不起作用)

NOTE: this code is wrong (at lest doesn't work for me)

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //give me 320
int height = dm.heightPixels; //give me 533

我有 HTC Desire,我的分辨率是 480 x 800 像素!

I have HTC Desire and my resolution is 480 x 800 pixels !

我知道我很笨!,显然我做错了什么,但有人可以请教我如何获得以英寸为单位的真实屏幕尺寸,并获得设备的真实分辨率吗?

I know I am stupid ! , and obviously I do something wrong but can someone please tall me how to get the real screen size in inches, and get the real resolution of the device ?

谢谢

推荐答案

好的,我终于找到了我的问题

OK FINALLY I found what is my problem

是的,这段代码工作正常,但我不返回像素,它返回不一样的 DIP(密度独立像素)!

Yes this code works fine but I doesn't return pixels, It returns DIP (Density independent pixels ) which is not the same !

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //320 dip
int height = dm.heightPixels; //533 dip

我需要的是真正的像素,它们的计算是这样的:

What I needed is the REAL pixels and the calculation for them is like this:

int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));

现在 widthPix 是 480 !

now the widthPix is 480 !

这篇关于如何获取设备的屏幕尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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