Android obtains the screen size and density

Source: Internet
Author: User

In Android, the screen length and width are obtained.CodeBut the results do not match the actual results. For example, if my mobile phone is i9000 and the screen size is 480 * 800px, the result is 320*533.

The results were unreliable, so I wrote a few lines of code to test it.

Test parameters:

Test environment: i9000 (Samsung)

Physical screen: 480 * PX

Density: 1.5

Test code:

// Obtain screen density (method 1) <br/> int screenwidth = getwindowmanager (). getdefadisplay display (). getwidth (); // screen width (pixels, for example, PX) <br/> int screenheight = getwindowmanager (). getdefadisplay display (). getheight (); // screen height (pixels, for example: 800 p) </P> <p> log. E (TAG + "getdefaultdisplay", "screenwidth =" + screenwidth + "; screenheight =" + screenheight); </P> <p> // obtain the screen density (method 2) <br/> displaymetrics dm = new displaymetrics (); <br/> dm = getresources (). getdisplaymetrics (); </P> <p> float density = DM. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) <br/> int densitydpi = DM. densitydpi; // screen density (pixels per inch: 120/160/240/320) <br/> float xdpi = DM. xdpi; <br/> float ydpi = DM. ydpi; </P> <p> log. E (TAG + "displaymetrics", "xdpi =" + xdpi + "; ydpi =" + ydpi); <br/> log. E (TAG + "displaymetrics", "density =" + density + "; densitydpi =" + densitydpi); </P> <p> screenwidth = DM. widthpixels; // screen width (pixels, for example, 480px) <br/> screenheight = DM. heightpixels; // screen height (pixels, for example, 800px) </P> <p> log. E (TAG + "displaymetrics (111)", "screenwidth =" + screenwidth + "; screenheight =" + screenheight ); </P> <p> // obtain the screen density (method 3) <br/> dm = new displaymetrics (); <br/> getwindowmanager (). getdefadisplay display (). getmetrics (DM); </P> <p> density = DM. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) <br/> densitydpi = DM. densitydpi; // screen density (pixels per inch: 120/160/240/320) <br/> xdpi = DM. xdpi; <br/> ydpi = DM. ydpi; </P> <p> log. E (TAG + "displaymetrics", "xdpi =" + xdpi + "; ydpi =" + ydpi); <br/> log. E (TAG + "displaymetrics", "density =" + density + "; densitydpi =" + densitydpi); </P> <p> int screenwidthdip = DM. widthpixels; // screen width (DIP, for example, 320dip) <br/> int screenheightdip = DM. heightpixels; // screen width (DIP, for example, 533dip) </P> <p> log. E (TAG + "displaymetrics (222)", "screenwidthdip =" + screenwidthdip + "; screenheightdip =" + screenheightdip); </P> <p> screenwidth = (INT) (DM. widthpixels * density + 0.5f); // screen width (PX, for example, 480px) <br/> screenheight = (INT) (DM. heightpixels * density + 0.5f); // screen height (PX, for example, 800px) </P> <p> log. E (TAG + "displaymetrics (222)", "screenwidth =" + screenwidth + "; screenheight =" + screenheight );
The result is as follows:

E/= myscreenactivity ========================================== get ultdisplay display (8509): screenwidth = 320; screenheight = 533 <br/> E/= myscreenactivity ======================== ======= displaymetrics (8509): xdpi = 156.3077; ydpi = 157.51938 <br/> E/= myscreenactivity ======================== ======= displaymetrics (8509): density = 1.0; densitydpi = 160 <br/> E/= myscreenactivity ======================== ======= displaymetrics (111) (8509): screenwidth = 320; screenheight = 533 <br/> E/= myscreenactivity ======================== ======= displaymetrics (8509): xdpi = 234.46153; ydpi = 236.27907 <br/> E/= myscreenactivity ======================== ======= displaymetrics (8509): density = 1.5; densitydpi = 240 <br/> E/= myscreenactivity ======================== ======= displaymetrics (222) (8509): screenwidthdip = 320; screenheightdip = 533 <br/> E/= myscreenactivity ====================== ======= displaymetrics (222) (8509): screenwidth = 480; screenheight = 800

Analysis result:

In the ondraw () method

Methods 1 and 2 are the same, and the results are both 320*533, which is obviously not the size of the i9000 screen of the test machine.

Method 3: multiply the results obtained from methods 1 and 2 by the density, and then the perfect 480*800, perfect!

Note: If the density is greater than 1, you must set the targetsdkversion to 4-9. For example:
<Uses-SDK Android: minsdkversion = "3" Android: targetsdkversion = "10"/>


Analyze the code carefully and find that the problem lies in the Code:

Getwindowmanager (). getdefaultdisplay (). getmetrics (DM)

Initialize a displaymetricsObject from this display's data.

Dm = getresources (). getdisplaymetrics ()

ReturnCurrent display metricsThat are in effect for this resource object. The returned object shoshould be treated as read-only.


Appendix: test source code


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.