Android resolution adaptation
Screen Resolution adaptation issues caused by the diversity of Android devices have been a headache in Android development.
I. screen size)
The size of the screen is measured in inches Based on the diagonal length of the screen.
For example, a 4 inch screen indicates that the diagonal line length of the screen is 4 inch, 1 inch = 2.54 cm, 4 inch = 4*2.54 cm = 10.16 cm.
In fact, this method applies not only to cell phone screens, but also to computer monitors and TV screens.
Note: The screen sizes of Android devices are varied on the market, which leads to the problems described in this article.
2. image resolution)
Any image on a mobile phone is composed of small dots, which are so-called pixel points. The product of the pixels contained in the horizontal and vertical sides of a square screen is the so-called resolution. For example, we usually get the product of 1024*768, or pixels in a digital camera.
We often see the ratio of the screen to, and, which is actually the ratio of the horizontal pixel to the vertical pixel, for example, 1024: 768.
Resolution is also similar to 1080 p (that is, the resolution is 1920*1080 pixels). In fact, the p here is progressive scan, that is, row-by-row scan.
Generally, the actual screen display effect depends on the relationship between the screen size and resolution. That is, when the screen size is fixed, the larger the Resolution, the more delicate the display effect, the resolution remains unchanged, and the smaller the screen, the more delicate the display.
3. Screen density (screendensity)
The screen density (pixels per inch) is defined based on the screen resolution, that is, the number of pixels per inch of the screen.
Pixel density = (length pixel ^ 2 + width pixel ^ 2) square open/screen size
For example, the resolution is 1280*720 and the screen size is 6 inch. The screen density is about 245.
Android supports three types of screen density: low (ldpi), medium (mdpi), and high (hdpi). The difference between these three density is the number of pixels per inch on the screen. By default, the Android browser and WebView are designed for Medium Density screens. On a high density screen, the Web page is scaled by about 1.5 times, and the low density screen is scaled by 0.75 times.
4. devicePixelRatio)
Device pixel ratio = physical pixel/device independent Pixel
Physical pixels can be considered as resolutions.
Device independent pixels (dip or dp) is related to the screen density.
On a display with a screen density of 160, 1dip = 1px, that is, an independent pixel of a device is equal to one pixel. In other words, 160dpi is standard, that is, the pixel ratio of a device is 1. If your screen resolution is large but the screen density is not properly set, the image displayed with dip will be too small. The Conversion Relationship between dip and pixel is as follows:
Dip (value) = (int) (px (value)/1.5 + 0.5)
The screen density on Android is usually 120 dpi, 160 dpi, 240 dpi, and 320 dpi, the folders corresponding to the Android project are drawable-ldpi, drawable-mdpi, drawable-hdpi, and drawable-xhdpi respectively. That is, the size ratio of the same image to these folders is 3: 4: 6: 8.
To adapt to various screens as much as possible, it is best to use dp as the unit, or multiply px by the device pixel ratio as the actual pixel.