Android gets the screen length and width implementation code (handwritten)

Source: Internet
Author: User

The screen length obtained in android is longer than the width. According to the code on the Internet, the result is inconsistent with the actual value. 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:

Copy codeThe Code is as follows: // obtain the screen density (method 1)
Int screenWidth = getWindowManager (). getdefadisplay display (). getWidth (); // screen width (pixels, for example, PX)
Int screenHeight = getWindowManager (). getdefadisplay display (). getHeight (); // screen height (pixels, 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, PX)
ScreenHeight = dm. heightPixels; // 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 (). getDefaultDisplay (). 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); // screen height (px, for example, 800px)
Log. e (TAG + "DisplayMetrics (222)", "screenWidth =" + screenWidth + "; screenHeight =" + screenHeight );

The result is as follows::Copy codeThe Code is as follows: e/= MyScreenActivity ========================================== get ultdisplay display (8509): screenWidth = 320; screenHeight = 533
E/= MyScreenActivity ========================================== displayMetrics (8509): xdpi = 156.3077; ydpi = 157.51938
E/= MyScreenActivity ========================================== displayMetrics (8509): density = 1.0; densityDPI = 160
E/= MyScreenActivity ========================================== displayMetrics (111) (8509): screenWidth = 320; screenHeight = 533
E/= MyScreenActivity ========================================== displayMetrics (8509): xdpi = 234.46153; ydpi = 236.27907
E/= MyScreenActivity ========================================== displayMetrics (8509): density = 1.5; densityDPI = 240
E/= MyScreenActivity ========================================== displayMetrics (222) (8509): screenWidthDip = 320; screenHeightDip = 533
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: When the density is greater than 1, you need to 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.