Getting the length and width of the screen in Android is referenced to a lot of codes on the Internet, but the result is inconsistent with the actual situation. For example, my phone is i9000, the screen size is 480*800px, but the result is 320*533
The result is very unreliable, so I wrote a few lines of code myself and tested it personally
Test parameters:
Test environment: i9000 (Samsung)
Physical screen: 480*800px
density :1.5
Test code:
Copy the codeThe code is as follows:
// Get screen density (Method 1)
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // Screen width (pixels, such as: 480px)
int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // Screen high (pixels, such as: 800p)
(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// Get screen density (Method 2)
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = ; // Screen density (pixel ratio: 0.75/1.0/1.5/2.0)
int densityDPI = ; // Screen density (pixels per inch: 120/160/240/320)
float xdpi = ;
float ydpi = ;
(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
screenWidth = ; // Screen width (pixels, such as: 480px)
screenHeight = ; // Screen high (pixels, such as: 800px)
(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// Get screen density (Method 3)
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
density = ; // Screen density (pixel ratio: 0.75/1.0/1.5/2.0)
densityDPI = ; // Screen density (pixels per inch: 120/160/240/320)
xdpi = ;
ydpi = ;
(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
int screenWidthDip = ; // Screen width (dip, such as: 320dip)
int screenHeightDip = ; // Screen width (dip, such as: 533dip)
(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
screenWidth = (int)( * density + 0.5f); // Screen width (px, such as: 480px)
screenHeight = (int)( * density + 0.5f); // Screen high (px, such as: 800px)
(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
The results are as follows:
Copy the codeThe code is as follows:
E/== MyScreenActivity =================================== getDefaultDisplay( 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 results:
In the onDraw() method
The results obtained in Methods 1 and 2 are the same, both of which are 320*533, which is obviously not the screen size of the test machine i9000.
Method 3, multiply the results obtained by methods 1 and 2 by density, perfect 480*800, perfect!
Note: When density is greater than 1, you need to set targetSdkVersion between 4-9, for example
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" />
However, does this mean that Method 3 must be universal?
The answer is no, because I have also tested it on the emulator, HTC G14 physical machine, ViewSonic, Galaxy tablets. Method 3 enlarges the actual screen value when density=1.5, for example: HTC G14
On HTC G14, the actual screen size is obtained directly by passing the actual physical screen size (540,960)
The reason why the real physical screen size cannot be obtained through a general method may be because the Android system is open source and different mobile phone manufacturers do not have unified manufacturing standards to stipulate mobile phone screens.
Carefully analyze the code 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 should be treated as read-only.