Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

android - How to get the screen size of the device?

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

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

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 ?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

OK FINALLY I found what is my problem

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));

now the widthPix is 480 !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...