android gets the screen length and width of the implementation code of handwritten
- 2020-05-07 20:24:17
- OfStack
In android, the length of the screen is longer than the width, referring to a lot of code on the Internet, but the result is not consistent with the actual, such as my phone is i9000, screen size is 480*800px, the result is 320*533
The result is very unreliable, so I wrote a few lines of code, test 1
Test parameters:
Test environment: i9000(3 stars)
Physical screen: 480*800px
density: 1.5
test code :
// Gets screen density (method) 1 )
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // Screen width (pixels, e.g. : 480px )
int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // Screen height (pixels, e.g. : 800p )
Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// Gets 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, e.g. : 480px )
screenHeight = dm.heightPixels; // Screen height (pixels, e.g. : 800px )
Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// Gets 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 , such as: 320dip )
int screenHeightDip = dm.heightPixels; // Screen width ( dip , such as: 533dip )
Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
screenWidth = (int)(dm.widthPixels * density + 0.5f); // Screen width ( px , such as: 480px )
screenHeight = (int)(dm.heightPixels * density + 0.5f); // The screen height ( px , such as: 800px )
Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
results are 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 of methods 1 and 2 are 1, both of which are 320*533, obviously not the screen size of the test machine i9000
Method 3, multiply the results of methods 1 and 2 by density, perfect 480*800, perfect!
note : if density is greater than 1, targetSdkVersion needs to be set between 4-9, for example
< uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" / >
But does this mean that method 31 must be universal?
The answer is no, because I have also tested it on emulators, HTC G14 physical machines, and ViewSonic and Galaxy tablets. Method 3 enlarges the actual screen value at density=1.5, for example: HTC G14
On HTC G14, the actual screen size is obtained directly from dm.widthPixels, dm.heightPixels (540,960)
The reason why it is impossible to obtain the real physical screen size through a common method may be that the Android system is open source, and different mobile phone manufacturers do not have a unified manufacturing standard to specify the mobile phone screen.
After careful analysis of the code, found the problem in 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.