Since the launch of Android, the topic of screen adaptation has been a constant theme. There is no doubt that the development of Android system today is inseparable from its openness. With the rise of domestic mobile phone manufacturers, any user, developer, OEM and operator can customize Android, which has resulted in the serious fragmentation problem that has been criticized for a long time.

Such as:

Fragmentation of Android system, such as customization based on Google’s native system: Xiaomi’s MIUI system, Meizu’s Flyme system and so on.

Android screen size fragmentation, such as 5 “, 5.99 “, 6 “, etc.

Android screen resolution fragmentation, such as 320×480, 480×800, 720×1280, 1080×1920, etc.

When the Android system, screen size, screen density fragmentation, it is easy to appear the same element in different mobile phones display different problems, in order to ensure that a certain element in Android phones with different sizes, different resolutions have the same display effect, this time it is necessary to screen adaptation.

Screen adaptation related concepts:

The screen size

Screen size Refers to the physical length of the diagonal of the screen, in inches. 1 inch =2.54 cm.

For example, common screen sizes: 5.0, 5.99, 6.0, etc

Screen resolution

Screen resolution refers to the sum of pixels in the horizontal and vertical of the mobile phone, the unit is PX, 1px=1 pixel point, generally in vertical pixel * horizontal pixel.

For example, a mobile phone with a resolution of 2160×1080 has 2160 pixels in height and 1080 pixels in width, and a screen resolution of 2160 × 1080.

Common Android phone resolutions are 320×480, 480×800, 720×1280, and 1080×1920.

Screen pixel density

Screen pixel density is the number of pixels per inch in dpi, short for dot per inch.

For example, if the device has 320 pixels per inch, the screen density of the device is 320dpi.

The relationship between screen size, resolution and pixel density

Screen pixel density is related to screen size and screen resolution. Under a single change condition, the smaller the screen size, the higher the resolution, the higher the pixel density, and vice versa.

The screen resolution of the mobile phone is wide x high, and the screen size is in inches, so the relationship between the three is:

Here is an example: if the screen resolution of the mobile phone is 2160×1080 and the screen size is 5.99 inches, all these are known for a mobile phone, what is the pixel density of the mobile phone?

That’s 403 pixels per inch.

px

Px refers to pixel, which is the most basic point for displaying data on the screen. The unit of screen resolution described above is PX, and px is used as the unified unit of measurement for UI design drawings and Android native APIS.

dpi

Dpi is the pixel density of the screen, the number of pixels per inch, calculated in a way that has been described above and won’t be repeated here.

Dp, dip

Dp is used as a unit when we set a fixed value. Dp and DIP are both short for Density Independent Pixels and unrelated to the actual physical Pixels on the terminal. This ensures the same effect on devices with different screen pixel densities.

For example, if we want to set an image to be half the width of the screen, then:

  • In pixels px, we need to set the width to 160px on a 320×480 phone and 240px on a 480×800 phone, which is different.
  • In terms of dp, set the width to 160dp at both resolutions to display half of the screen.

sp

Sp stands for Scale-Independent Pixels. Similar to DP, SP is commonly used to specify the font size. When users modify the font displayed on their mobile phones, the font size changes accordingly.

Conversion of DP to PX

Generally, the design drawing given by UI designers is based on PX, while Android development uses DP as the unit, so we need to convert, and the conversion relationship is as follows:

px = dp * (dpi / 160)
Copy the code

In Android, it is defined as 160dpi (320×480 screen resolution) : 1dp=1px

After combing through the above concepts, the following is about to start to explain the adaptation scheme, because the length is too long, we will separate a few updates, please look forward to. Welcome to pay attention to the public account [Dragon Xuan] can get the latest updates oh.