Quick start tutorial on common size units (DP, SP) in Android

Common size units

The common size units in Android development are as follows:

I don't know the exact meaning. I believe we are familiar with the above size units. Let's re understand them first:

In the above several common size units, DP and SP can be regarded as virtual sizes. DP is a virtual pixel unit independent of the device. After the developer specifies the size in DP for the UI control, it can have the same physical size on Android devices with different screen densities. The emergence of DP allows developers not to pay attention to the conversion relationship between screen density and physical pixels. SP is similar to DP, but it is mainly used as the size unit of font. The difference between SP and DP is that Android system supports users to set the font size, so the actual size of SP will be scaled on the original basis according to user settings.

The following describes DP and SP in detail.

Detailed explanation of dimension units DP

The full name of DP is device independent pixels. On devices with different screen densities, the physical size of 1 DP is the same. So, what is screen density?

Screen density

The screen density of a mobile phone usually refers to the DPI (dots per inch) of the mobile phone screen, that is, the number of pixels per inch. For Android phones, common DPIs are as follows:

In actual development, DPI values 120, 160, 240, 320 and 480 are usually used to refer to LDPI, MDPI, hdpi, xhdpi and xxhdpi respectively. Usually, the higher the screen density, the finer the image displayed by the mobile phone. You can obtain the screen density of the current Android device through the following code:

If we run the above code on an Android phone with a screen density of 320dpi, we will get the following output:

The density DPI in the above output is the DPI value of the Android phone screen, so what is density? In fact, it represents the ratio of the DPI value of the current screen to the reference DPI value, which is 160. Now that we have understood DPI, let's uncover the mystery of DP.

dp

We mentioned above that the DPI value 160 is selected as the reference screen density, which artificially establishes the relationship between DP and PX: on Android devices with a DPI of 160, 1 DP = 1px. Assuming that x is the size of a UI control in PX, y is the size of the same UI control in DP, and densitydpi represents the screen density, the relationship between X and Y is: x = y * densitydpi / 160.

After introducing DP, let's explore the true face of the size unit sp.

Detailed explanation of dimension units

Before introducing SP, let's take a look at a static method contained in the typedvalue class that converts units such as DP and SP into PX:

To convert DP to PX, execute the following code:

Density, which we introduced earlier, refers to the ratio of the current DPI to the benchmark DPI (160). Density is calculated by dividing the DPI of the current screen by 160. That is, when the DPI of the screen is 120, 160, 320 and 480, the values of density are 0.75, 1, 2 and 3 respectively. To convert SP to PX, execute the following code:

It can be seen that the calculation formula for converting SP to PX is similar to that for converting DP to PX. What is scaleddensity? In fact, scaleddensity is different from density. Scaleddensity can be changed dynamically. When users change the font scaling of Android devices, the value of scaleddensity will change. The calculation formula of scaleddensity is: scaleddensity = density * fontscale. Where fontscale represents the font scaling scale of Android device set by the user, which is 1 by default. That is, when the user does not change the font scaling of the Android device, the conversion of SP, DP and PX is the same.

Multi resolution war

Various Android devices with different resolutions on the market have dug many holes for Android developers, such as:

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>