Size specification

The size of Android is an age-old topic, and the most obvious manifestation of size in Android development is the adaptation of the screen.

About the relevant size concept of Android, here is not a development, post a about the design specification: Android UI interface design specification complete version

Here are the typical Android size units:

  • Px — pixels (not specific)
  • Dp — describes the spacing unit
  • Sp — describes the size unit

px

Px is a universal unit. A pixel can be understood as the number of pixels, the smallest unit that is indivisible (by definition).

You may have heard of the concept of resolution, 72 pixels per inch, which means that an inch is divided into 72 parts, and each part is a pixel point, so there is no fixed size of the pixel, it is related to the resolution of the load.

dp

Because of the relationship between pixel and resolution, After so many years of development of Android, mobile phone screens with different resolutions have been diversified. If you directly use PX for UI development, there will be many adaptation problems, so Android introduced the concept of DP.

Dp is a universal number derived from PX combined with resolution, which gives a good universal reference for the size of fragmented Android screens. (See the link above for specific calculation method)

sp

The purpose of sp is the same as dp, but the applicable objects are different. Sp is more used to describe the size of text, and the effect is the same as DP.

This is related to a function of Android system, users can control the font size, which means that sp set by our developers is only a reference value, it will combine the user’s options to act on the text size, so it is differentiated from DP.

Note: many developers also set the text size to DP in their development. This is not a good practice, and the correct adaptation should be based on the user’s choice.

Unit and unit conversion introduced in Android

Units introduced in Android:

  • COMPLEX_UNIT_PX
  • COMPLEX_UNIT_DIP
  • COMPLEX_UNIT_SP
  • COMPLEX_UNIT_PT
  • COMPLEX_UNIT_IN
  • COMPLEX_UNIT_MM

The units marked in Android are as follows:

Size of the unit instructions
dp Density-independent pixels – abstract units based on the physical density of the screen, providing consistency in the actual size of UI elements across devices
sp Zoom independent pixels – like DP units, generally used for font units
pt Points – 1/72 inch based on the physical size of the screen (assuming a screen density of 72dpi).
px Pixel – corresponds to the actual pixels on the screen. It is not recommended to use this unit of measure because the actual representation may vary from device to device.
mm Mm – Based on the physical size of the screen.
in Inches – Based on the physical size of the screen.

Since Android uses PX for size processing, there is a problem of size conversion. Android provides us with a method:

public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
    switch (unit) {
        case COMPLEX_UNIT_PX:   // px to px
            return value;
        case COMPLEX_UNIT_DIP:  // dp to px
            return value * metrics.density;
        case COMPLEX_UNIT_SP:   // sp to px
            return value * metrics.scaledDensity;
        case COMPLEX_UNIT_PT:   // pt to px
            return value * metrics.xdpi * (1.0 f/72);
        case COMPLEX_UNIT_IN:   // in to px
            return value * metrics.xdpi;
        case COMPLEX_UNIT_MM:   // mm to px
            return value * metrics.xdpi * (1.0 f/25.4 f);
    }
    return 0;
}
Copy the code

This method is included in the TypedValue class.

So we can define a utility class to simplify the unit conversion:

/** * Pixel Density Tool ** Related concepts: * px: pixels * DP: abstract units of screen density * sp: scale according to the user's font size preferences * pt: pounds * in: inches * mm: millimeters */
object DensityUtils {

    /**
     * dp to px
     * @param value     dp value
     * @return          px value
     */
    fun dp2px(value: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, value,
            Resources.getSystem().displayMetrics
        )
    }

    /**
     * sp to px
     * @param value     sp value
     * @return          px value
     */
    fun sp2px(value: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP, value,
            Resources.getSystem().displayMetrics
        )
    }

    /**
     * pt to px
     * @param value     pt value
     * @return          px value
     */
    fun pt2px(value: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_PT, value,
            Resources.getSystem().displayMetrics
        )
    }

    /**
     * in to px
     * @param value     in value
     * @return          px value
     */
    fun in2px(value: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_IN, value,
            Resources.getSystem().displayMetrics
        )
    }

    /**
     * mm to px
     * @param value     mm value
     * @return          px value
     */
    fun mm2px(value: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_MM, value,
            Resources.getSystem().displayMetrics
        )
    }
}
Copy the code

The screen size

Speaking of size, post something about screen size as well.

Wide screen high

Speaking of screen size, the first thing that comes to mind is the width and height of the screen. In our projects, the width and height of the screen are often used for some size adaptation.

Post a utility class that you can use directly:

/** * Screen parameter tool */
object ScreenUtils {

    private fun getDisplayMetrics(a): DisplayMetrics {
        return Resources.getSystem().displayMetrics
    }

    /** * get the screen width */
    fun getScreenWidth(a): Int {
        return getDisplayMetrics().widthPixels
    }

    /** * Get the screen height */
    fun getScreenHeight(a): Int {
        return getDisplayMetrics().heightPixels
    }
}
Copy the code

There are many other ways to get the screen width, but most of those are not very intuitive or require a convenient context, so I’ll just write them out, and you can look them up for yourself if you’re interested.

conclusion

Here is the basic concept of size in Android. It is important to clarify the concept of each size unit and the transformation between sizes. In the future, the article will not be confused.