The difference between SP, DP and PX in layout file

Time:2021-1-16

preface:

As we all know, there are many Android manufacturers, and Android phones and tablets of various sizes emerge in an endless stream. The fragmentation of Android ecological environment is becoming more and more serious. In order to solve the problem of excessive resolution, Google defines Px, DP and SP in the Android development document, which is convenient for developers to adapt to Android devices with different resolutions. It is necessary for junior programmers to understand and master some basic knowledge of adaptation.

Basic concept:

  • PX: in fact, it is pixel unit. For example, the 800 * 400 resolution list of mobile phones is PX unit
  • SP: similar to DP, it also scales according to the user’s font size preference
  • DP: virtual pixel, which will automatically adapt to different pixel density devices
  • Dip: same as DP

A chestnut: PX and DP

Pixel, or pixel, 1px represents a physical pixel on the screen. But PX units are not recommended. Because the actual size of the same pixel image may be different in different mobile phones. When PX is used, if you need to draw 1 pixel table line or shadow line, it will be blurred if you draw it in other units.

To understand DP, we must first introduce the concept of DPI. The full name of DPI is dots per inch, the number of pixels per inch of diagonal. Therefore, its calculation formula is as follows:

For example, height and width are the pixels of length and width, the sum of squares is the number of diagonal pixels, and size is the 5 and 4 in 5-inch and 4-inch mobile phones, which is the length of diagonal.

Therefore, the higher the resolution, the higher the DPI. With the same resolution, the smaller the diagonal inches, the higher the DPI.

DP, also known as dip, is device independent pixels. The device does not depend on a unit of pixels. It will automatically adapt on devices with different pixel densities, such as:

At 320×480 resolution, the pixel density is 160, 1DP = 1px

At 480×800 resolution, the pixel density is 240, 1DP = 1.5px

Calculation formula: PX = DP * (DPI / 160)

Let’s do a simple sample verification, as follows, a layout code

In 480 * 800 resolution, the device effect of 3.7 diagonal inches of screen is as follows

image

In 480 * 800 resolution, the device effect figure of 5.1 diagonal inches of screen is as follows

image

From this we can see that PX as a unit will display different effects in different devices. If DP is used as a unit, it will be converted according to different devices and adapted to different models. Therefore, it is suggested to use DP as the unit of length and width.

Another Chestnut: DP and SP

Since we said above that DP can automatically adapt to the device model, is it also feasible in the font? Let’s do a simple sample verification, as follows, a layout code

In 480 * 800 resolution, the device effect of 3.7 diagonal inches of screen is as follows

image

With 480 * 800 resolution and 3.7 diagonal inches of screen, we modify the font size of the mobile phone system and get the following effect

image

It can be seen that using SP as the font size unit will change with the font size of the system, while DP as the unit will not. Therefore, it is recommended to use SP as the unit of font size

Development

Provides a tool class: DP and PX value conversion

public class DensityUtil {

    /**
     *Convert from DP to PX (pixel) according to the resolution of mobile phone
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     *Change from PX (pixel) to DP according to the resolution of the phone
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}