Home>

This article details the meaning of common size units in android development,Focus on explaining the nature of the two size units sp and dp and their conversion formulas with px,The article is very detailed,

Common size units

The commonly used size units in android development are as follows:

dp (dip) px pt inch sp

Do not know the exact meaning,I believe that everyone is familiar with the above sizes.Let's revisit them first:

dp (dip):Device independent pixels. This size unit has the same physical size on different devices. px:Pixel, needless to say. pt:usually used as the unit of font size,1 pt is equivalent to 1/72 inch. inch:inch, 1 inch is about 2.54 centimeters, which is mainly used to describe the size of the mobile phone screen. sp:most people only know that it is usually used as the unit of measurement for fonts,The actual size also depends on the user settings on the specific device.(If you stop here with "sp",Then after reading this article you will understand it more thoroughly ^ _ ^)

In the above common dimensions,dp and sp can be regarded as virtual sizes.Where dp is a device-independent virtual pixel unit,After the developer specifies the size in dp for the ui control,It is possible to have the same physical size on android devices with different screen densities.The advent of dp frees developers from paying attention to the conversion relationship between screen density and physical pixels.sp is similar to dp, but it is mainly used as the unit of measurement for fonts.The difference with dp is:android system supports user to set font size,Therefore, the actual size of sp will be scaled based on the original setting by the user.

The following describes in detail the two dimensional units of dp and sp.

Detailed dp of size unit

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, which is the number of pixels per inch.For android phones,The common dpi are as follows:

ldpi:The corresponding dpi range is 0 to 120, which means that the screen density of a screen with 0 to 120 pixels per inch belongs to ldpi. mdpi:dpi range is 120 ~ 160 hdpi:dpi range is 160 ~ 240 xhdpi:dpi range is 240 ~ 320 xxhdpi:dpi range is 320 ~ 480

In actual development,The dpi values ​​of 120, 160, 240, 320, and 480 are usually used to refer to ldpi, mdpi, hdpi, xhdpi, and xxhdpi, respectively. Mobile phones with higher screen density usually have more delicate images.The screen density of the current android device can be obtained by the following code:

private void getdpi () {
 displaymetrics dm=getresources (). getdisplaymetrics ();
 log.i ("tag", "density =" + dm.density);
 log.i ("tag", "densitydpi =" + dm.densitydpi);
}

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

density=2
densitydpi=320

The densitydpi in the above output is the dpi value of the android phone screen, so what is the density?In fact, it represents the ratio of the dpi value of the current screen to the reference dpi value.This baseline dpi value is 160.

Now that we understand dpi, let's unravel the mystery of dp.

dp

We mentioned above that we chose 160 dpi as the reference screen density.This benchmark screen density artificially establishes the relationship between dp and px:on an android device with a dpi of 160, 1 dp=1px. Suppose x is the size of a ui control in px,y is the size of the same ui control in dp,densitydpi represents the screen density,Then the relationship between x and y is:x=y * densitydpi/160.

Having introduced dp, let's explore the true face of the size unit sp.

Detailed explanation of size unit sp

Before introducing sp, let's take a look at a static method in the typedvalue class that converts dp, sp and other units to px:

public static float applydimension (int unit, float value, displaymetrics metrics) {
  switch (unit) {
  case complex_unit_px:
   return value;
  case complex_unit_dip:
   return value * metrics.density;
  case complex_unit_sp:
   return value * metrics.scaleddensity;
  case complex_unit_pt:
   return value * metrics.xdpi * (1.0f/72);
  case complex_unit_in:
   return value * metrics.xdpi;
  case complex_unit_mm:
   return value * metrics.xdpi * (1.0f/25.4f);
  }
  return 0;
 }

To convert dp to px, the following code is executed:

return value * metrics.density;

We introduced density earlier,Refers to the ratio of the current dpi to the reference dpi (160). The density is calculated by dividing the dpi of the current screen by 160. That is,When the screen dpi is 120, 160, 320, and 480, the values ​​of density are 0.75, 1, 2, and 3, respectively.

To convert sp to px, the following code is executed:

return value * metrics.scaleddensity;

can be seen,The calculation formula for sp to px conversion is similar to that for dp to px conversion. So what is scaleddensity?In fact, scaleddensity is different from density. Scaleddensity can be changed dynamically.When the user changes the font scaling of the android device,The value of scaleddensity will change.The calculation formula of scaleddensity is:scaleddensity=density * fontscale. Where fontscale represents the user-defined font scaling of the android device,The default is 1. 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-resolutionWar

There are various android devices with different resolutions on the market, which have dug numerous pits for the majority of android developers.such as:

A separate set of dimens files needs to be maintained for android devices of different resolutions; Usually the ui designer will only mark the pixel size of the ui control for us for a certain resolution device.I believe that many of my friends have had enough pain to manually convert the pixel size of the ui control on different resolution devices; Usually we need to maintain a drawable folder for each resolution android device to get a better picture display effect.This can lead to bloated apk file sizes;And if the pictures in a drawable folder need to be modified,Then you need to replace the corresponding pictures in all other drawable folders.If you accidentally miss a picture in a drawable folder,This can cause the picture to be distorted on phones with certain resolutions.

to sum up

  • Previous Vue model vue advanced tutorial
  • Next Timer and TimerTask examples in Java