foreword
The development of the Android system today is inseparable from its open source nature. However, as more and more devices are connected to the Android system and various customizations are made to the Android system, various serious fragmentation has occurred for a long time. The problem. For example, Android screens have various sizes, such as 5 inches, 5.9 inches, 6 inches, 6.1 inches, etc. Of course, the screen resolutions are also varied, which can easily lead to the problem that the same element displays different effects on different phones , therefore, the screen adaptation work in Android application development is becoming more and more important.
This article introduces the knowledge about screen adaptation in Android.
Basic concept of screen adaptation
screen size
The screen size refers to the physical length of the diagonal of the mobile phone , and the unit is 英寸
(inch, 1 inch = 2.54 cm ),
For example, our common 6-inch mobile phone means that the diagonal length of the mobile phone is 6 * 2.54 = 15.24 cm.
Screen Resolution
Refers to the sum of the number of pixels in the horizontal and vertical directions of the mobile phone, the unit is px
(pixel, 1 pixel = 1 pixel),
The resolution is generally described as A * B, for example: 1080 * 1920, which means that each row of the screen has 1080 pixels, and each column has 1920 pixels.
For Android devices, we can quickly check the screen resolution information through the ADB command:
adb shell wm size
screen pixel density
It refers to the number of pixels per inch, and the unit is dpi
(dots per ich). Assuming that there are 160 pixels per inch in the device, then the screen pixel density of the device is 160 dpi.
The calculation formula of screen pixels is as follows:
screen pixel density = single row pixel number 2 + single column pixel number 2 screen size screen pixel density = \frac{\sqrt{single row pixel number^2 + single column pixel number^2}}{screen size}screen pixel density=screen sizeNumber of pixels in a single row2+Number of pixels in a single column2
That is, first use the Pythagorean theorem to calculate the number of pixels on the diagonal, and then divide the number by the screen size.
For example: 4.7 inches, 1080 * 1920 resolution mobile phone, its pixel density is:
screen pixel density = 108 0 2 + 192 0 2 4.7 ≈ 469 dpi screen pixel density = \frac{\sqrt{1080^2 + 1920^ 2}}{4.7} ≈ 469 dpiscreen pixel density=4.710802+19202≈469 d p i
For Android devices, we can quickly check the screen pixel density information through the ADB command:
adb shell wm density
Density-Independent Pixels
Density-independent pixel, the English name density-independent pixel, unit dp, it is a unique unit of Android, it has nothing to do with the actual physical pixels on the device, only related to the screen pixel density, it can guarantee that on devices with different screen pixel densities shows the same effect.
Note: In Android, 1 inch = 160 dp .
Why use dp instead of px directly? Let's take a simple example:
Assuming that a line whose length is half the screen width needs to be displayed on the screen, expressed in px, then:
- On a device with a resolution of 480 * 800 and a screen density of 240 dpi (about 3.89 inches), the length of this line should be 240 px
- On a device with a resolution of 320 * 480 and a screen density of 160 dpi (about 3.61 inches), the length of this line should be 160 px
For the above cases, if you use px to set the length of this line, you need to use two different values to adapt to two devices with different resolutions .
But if we dp
use units, then in both cases, we only need to use 160 dp to display the line as half the length of the screen.
In normal development, the design drawings given to us by UI designers are all px
in units, and we want to use dp
it as a unit for development, so we need to convert px to dp. The conversion relationship between them is as follows :
density type | Representative resolution (px) | screen density (dpi) | Conversion (px/dp) |
---|---|---|---|
low density (ldpi) | 240 * 320 | 120 | 1 dp = 0.75 px |
medium density (mdpi) | 320 * 480 | 160 | 1 dp = 1 px |
high density (hdpi) | 480 * 800 | 240 | 1 dp = 1.5 px |
Extra High Density (xhdpi) | 720 * 1280 | 320 | 1 dp = 2 px |
Ultra Ultra High Density (xxhdpi) | 1080 * 1920 | 480 | 1 dp = 3 px |
It can be seen that, in the case of a device with a screen density of 160 dpi, 1 dp = 1 px.
independent scale pixels
Independent scale pixel, English name scale-independent pixel, unit sp, it is a unique unit of Android, generally used to set the font size in Android.
In general, 1 sp = 1dp. It is recommended to use an even sp value to set the font size, and it is not recommended to use odd and decimal numbers , because it is easy to cause loss of precision.
Screen adaptation scheme
dp native solution
We need to think, what problem dp
does solve?
In the introduction of the dp concept above, we know that in Android, 1 inch = 160 dp ,
For devices with the same size and different resolutions , they have different screen pixel densities, so the number of pixels represented by each 1 dp is different.
If a line is displayed with the same px value, the length of the line will be displayed in different proportions on the two devices,
If dp is used as the unit, since the corresponding physical length of each dp is the same, therefore, using the same dp value, their display ratios are the same.
In fact, the advantages of dp are mainly reflected in the adaptation effect of devices with the same size and different resolutions . For devices with different sizes and different resolutions, the adaptation effect of dp is not satisfactory!
for example.
Assuming that our UI design is designed for a device with a screen resolution of 1080 * 2670 and a screen size of 6 inches (dpi is about 480), then by calculation, the maximum width of the device screen in the UI diagram is 360 dp.
But for a device with a screen resolution of 1080 * 1920 and a screen size of 5 inches (dpi is about 440), by calculation,
Its screen width is actually 1080 / (440/160) = 392.7 dp
,
That is, the screen is wider than the design drawing.
In this case, even if dp is used, the same effect cannot be displayed on different devices. At the same time, there are still some devices whose screen width is less than 360 dp. At this time, it will lead to development according to 360 dp width, but the actual display is incomplete.
Summary: Only use the dp native solution for screen adaptation, the adaptability is very poor, and this solution is not recommended.
sw qualifier adaptation scheme
The sw qualifier adaptation is the smallestWidth adaptation, also called the smallest width qualifier adaptation.
Its principle is that Android will identify the dp value of the minimum size of the available height and width of the screen, and then search for resource files under the folder corresponding to the qualifier according资源文件
to the identified results . In fact, the system selects the corresponding file through specific rules.
for example.
The screen resolution of a mobile phone is 1080 * 1920, and the screen pixel density is 480 dpi. Through calculation,
The dp value corresponding to its maximum width is:1080 / (480 / 160) = 360 dp
According to this 360 dp
value , the system will look for value-sw360dp
the folder and the corresponding resource file and use it.
If there is no value-sw360dp
folder , the system will look down, for example, only the one closest to 360 dp value-sw350dp
, then Android will select the resource file under value-sw350dp
the folder .
Generally speaking, the value-swXXXdp folders will be created under /src/main/res/
the directory , such as value-sw480dp
the folder created in my project:
Before creating these folders, we need to set the reference size in advance. Generally, we will use the dp value corresponding to the maximum width used in the UI design drawing as the reference dp value. For example, I will use 360 dp as the reference below. We Take a look at the baseline resource file /src/main/res/value/dimens.xml
, which looks like this:
Next, we create a value-sw480dp
folder and create dimens.xml
files :
So how is this data calculated? Of course, it is also calculated on the basis of the benchmark size.
Right now:dp_2 = (480 / 基准) x 2 = (480 / 360) x 2 = 2.6667 dp
This adaptation scheme has a low fault tolerance rate, and its disadvantage is that it is highly intrusive, and multiple folders need to be created according to the maximum width dp of different models, and the maintenance cost is relatively high.
It is obviously more suitable than using it dp 原生方案
.sw 限定符适配方案