On a DPI mobile phone, 1dp = 1px; on a 320dpi mobile phone, 1dp = 1.5px
Android obtains the screen size and density
In some cases, we need to obtain the physical size of the screen of the Android mobile phone or Pad to facilitate the interface design or other functions. The following describes how to obtain the physical size of a screen:
I have found a lot of information on the Internet and found that obtaining the screen size is not very complex programming operations. The following code can be used to obtain the screen size.
Write the following code in the onCreate method of an Activity:
DisplayMetrics metric = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (metric );
Int width = metric. widthPixels; // Screen width (pixels)
Int height = metric. heightPixels; // Screen height (pixels)
Float density = metric. density; // Screen density (0.75/1.0/1.5)
Int densityDpi = metric. densityDpi; // Screen density DPI (120/160/240)
However, it should be noted that, on a low-density cell phone, only the code above cannot obtain the correct size. For example, if you run the above Code on A 320x20 pixel low-density mobile phone, the screen size obtained is 427 x. Therefore, the study found that if multi-resolution support is not set, the Android system will convert the low density (120) of 160 x320 to the corresponding size of medium density, in this way, the code of the program is greatly affected. Therefore, add the supports-screens node to the AndroidManifest. xml file of the project. The specific content is as follows:
<Supports-screens
Android: smallScreens = "true"
Android: normalScreens = "true"
Android: largeScreens = "true"
Android: resizeable = "true"
Android: anyDensity = "true"/>
In this way, the current Android program supports multiple resolutions, so you can get the correct physical size.