why adapt
Due to the openness of the Android system, any user, developer, OEM manufacturer, or operator can customize Android, resulting in a variety of devices running Android with different screen sizes and pixel densities . While the system can adapt the interface to different screens through basic zooming and resizing functionality, further optimization should be done to ensure that the interface renders beautifully on various screens.
screen size
Screen size refers to the diagonal length of the screen in inches, 1 inch = 2.54 cm. Common screen sizes are 2.4, 2.8, 3.5, 3.7, 4.2, 5.0, 5.5, 6.0, etc.
Screen Resolution
Screen resolution refers to the number of pixels in the horizontal and vertical directions, the unit is px, 1px = 1 pixel. Generally, vertical pixels*horizontal pixels, such as 1960*1080. Indicates that there are 1980 pixels in the height direction and 1080 pixels in the width direction. Common resolutions for Android phones: 320x480, 480x800, 720x1280, 1080*1920. The UI designer's design drawings will use px as a unified unit of measurement.
screen pixel density
Screen pixel density refers to the number of pixels per inch , and the unit is dpi, which is the abbreviation of "dot per inch". Screen pixel density is related to screen size and screen resolution. Under a single change condition, the smaller the screen size and the higher the resolution, the greater the pixel density, and vice versa.
Android phones have a corresponding screen pixel density for each type of phone screen size:
density type | Represent resolution (px) | Screen pixel density (dpi) |
low density (ldpi) | 240x320 | 120 |
medium density (mdpi) | 320x480 | 160 |
high density (hdpi) | 480x800 | 240 |
Extra High Density (xhdpi) | 720x1280 | 320 |
Ultra Ultra High Density (xxhdpi) | 1080x1920 | 480 |
The relationship between screen size, resolution, and pixel density
The resolution of a mobile phone is width x height, and the screen size is in inches, so the relationship between the three is:
Density-Independent Pixels
density-independent pixel, called dp or dip , has nothing to do with the actual physical pixels on the terminal. It is guaranteed to display the same effect on devices with different screen pixel densities . During Android development, use dp instead of px unit to set image size, which is a unique unit of Android.
Application scenario:
If you also draw a line that is half the length of the screen, if you use px as the unit of measurement, then set it to 240px on a mobile phone with a resolution of 480x480; set it to 160px on a mobile phone with a resolution of 320x480, and the settings of the two are different; if Using dp as a unit, 160dp is displayed as half the length of the screen at both resolutions.
dp to px conversion
px = dp * (dpi / 160)
independent scale pixels
sp, that is, scale-independent pixels, is similar to dp, but can be scaled according to the text size preference, and is the queen unit for setting the font size .