Android obtains the screen size and density

Source: Internet
Author: User
// Obtain the screen density (method 1) int screenWidth = getWindowManager (). getdefadisplay display (). getWidth (); // screen width (pixels, for example, 480px) int screenHeight = getWindowManager (). getdefadisplay display (). getHeight (); // screen height (pixel, for example, 800 p) Log. e (TAG + "getDefaultDisplay", "screenWidth =" + screenWidth + "; screenHeight =" + screenHeight); // obtain the screen density (method 2) displayMetrics dm = new DisplayMetrics (); dm = getResources (). getDisplayMetrics (); float density = dm. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) int densityDPI = dm. densityDpi; // screen density (pixels per inch: 120/160/240/320) float xdpi = dm. xdpi; float ydpi = dm. ydpi; Log. e (TAG + "DisplayMetrics", "xdpi =" + xdpi + "; ydpi =" + ydpi); Log. e (TAG + "DisplayMetrics", "density =" + density + "; densityDPI =" + densityDPI); screenWidth = dm. widthPixels; // screen width (pixels, for example, 480px) screenHeight = dm. heightPixels; // The screen height (pixels, for example, 800px) Log. e (TAG + "DisplayMetrics (111)", "screenWidth =" + screenWidth + "; screenHeight =" + screenHeight); // obtain the screen density (method 3) dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); density = dm. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) densityDPI = dm. densityDpi; // screen density (pixels per inch: 120/160/240/320) xdpi = dm. xdpi; ydpi = dm. ydpi; Log. e (TAG + "DisplayMetrics", "xdpi =" + xdpi + "; ydpi =" + ydpi); Log. e (TAG + "DisplayMetrics", "density =" + density + "; densityDPI =" + densityDPI); int screenWidthDip = dm. widthPixels; // screen width (dip, for example, 320dip) int screenHeightDip = dm. heightPixels; // screen width (dip, for example, 533dip) Log. e (TAG + "DisplayMetrics (222)", "screenWidthDip =" + screenWidthDip + "; screenHeightDip =" + screenHeightDip); screenWidth = (int) (dm. widthPixels * density + 0.5f); // screen width (px, for example: 480px) screenHeight = (int) (dm. heightPixels * density + 0.5f); // The screen height (px, for example, 800px) Log. e (TAG + "DisplayMetrics (222)", "screenWidth =" + screenWidth + "; screenHeight =" + screenHeight );

The test results are as follows:

E/== MyScreenActivity ===================================  getDefaultDisplay( 8509): screenWidth=320; screenHeight=533E/== MyScreenActivity ===================================  DisplayMetrics( 8509): xdpi=156.3077; ydpi=157.51938E/== MyScreenActivity ===================================  DisplayMetrics( 8509): density=1.0; densityDPI=160E/== MyScreenActivity ===================================  DisplayMetrics(111)( 8509): screenWidth=320; screenHeight=533E/== MyScreenActivity ===================================  DisplayMetrics( 8509): xdpi=234.46153; ydpi=236.27907E/== MyScreenActivity ===================================  DisplayMetrics( 8509): density=1.5; densityDPI=240E/== MyScreenActivity ===================================  DisplayMetrics(222)( 8509): screenWidthDip=320; screenHeightDip=533E/== 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"/>

 

 

However, does this mean that method 3 must be generic?

The answer is no, because I also tested it on the simulator, HTC G14 physical machine, ViewSonic, and Galaxy tablet. Method 3 amplified the actual screen value when density = 1.5, example: HTC G14

On HTC G14, the actual screen size is obtained through dm. widthPixels and dm. heightPixels (540,960)

The reason why the actual physical screen size cannot be obtained through a common method may be that the Android system is open-source and different mobile phone manufacturers do not have a uniform manufacturing standard to define the mobile phone screen.

 

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

GetWindowManager (). getDefaultDisplay (). getMetrics (dm)

Initialize a DisplayMetrics object from this display's data.

 

Dm = getResources (). getDisplayMetrics ()

Return the current display metrics that are in effect for this resource object. The returned object shoshould be treated as read-only.

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.