如何获得装置的屏幕大小? [英] How to get the screen size of the device?

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

问题描述

的愿望有480 * 800像素,3.7英寸的HD拥有480 * 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.

我运行从该线程接受的答案code如何获得设备的屏幕大小?但它是错误的!例如为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.

注:此code是错误的(在唯恐不为我工作)

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 * 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

这是code工作正常,但我不回像素,它返回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天全站免登陆