Old Deng's android Study Notes (2)-Android gets screen resolutions in N ways, android-android
Android gets the screen resolution
Method 1: the simplest method
12 |
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); |
It can be done in two simple sentences. Try to use this method for testing first. If this method cannot pass the test, replace the following method.
Method 2: obtain a general algorithm for screen size comparison
12345678 |
DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); float density = dm.density; int densityDPI = dm.densityDpi; float xdpi = dm.xdpi; float ydpi = dm.ydpi; screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; |
This method supports most mobile phones. In general, if the above method fails, you can test it by modifying the method. It is best to do this...
Method 3:
12345678910 |
dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); density = dm.density; densityDPI = dm.densityDpi; xdpi = dm.xdpi; ydpi = dm.ydpi; int screenWidthDip = dm.widthPixels; int screenHeightDip = dm.heightPixels; screenWidth = ( int )(dm.widthPixels * density + 0 .5f); screenHeight = ( int )(dm.heightPixels * density + 0 .5f); |
This method is suitable for special models, such as i9000. It can be used for good identification and verification. The principle is to multiply the result of method 1 and method 2 by the density (when the density is greater than 1, The targetSdkVersion must be set to 4-9)
Note: currently, the fragmentation of android is very serious, and many methods cannot be used.
Address: http://www.itmmd.com/201411/161.html
This article is organized and published by Meng IT personnel. The reprinted article must indicate the source.