dpi , dip , resolution, screen size, px, density relationship and conversion

 

The dip setting has nothing to do with resolution, but with screen density. By default,

LDPI density is 120, coefficient is 0.75,

MDPI has a density of 160 and a coefficient of 1.0;

The password of HDPI is 240, and the coefficient is 1.5;

The density of XHDPI is 320 and the coefficient is 2.0;

The so-called density is the number of pixels in a unit square inch

 

1. Basic Concepts

  • dip : Density independent pixels, device independent pixels.
  • dp : is dip
  • px : pixels
  • dpi        : dots per inch, directly speaking, how many pixels per inch. Common values ​​are 120, 160, and 240. I generally call it pixel density, or simply density
  • density  : It seems to be called density in direct translation. Common values ​​are 1.5, 1.0. Ratio to standard dpi (160px/inc)
  • Resolution: The number of pixels in horizontal and vertical directions, common values ​​are 480X800, 320X480
  • Screen size: The length of the diagonal of the screen. Computer TV is the same.
  • screen ratio issue. Because only the diagonal length is determined, the length of the two sides is not necessarily determined. So with 4:3 and 16:9, the side length of the screen can be calculated.

2. Application

  In android, get the metrics of a window, there are so many values ​​in it

     metrics.density;
     metrics.densityDpi;

  densityDpi: This is the dpi we often say.

  density : In fact, it is the value obtained after DPI / (160 pixels/inch). Isn't it a bit strange because I brought the unit. . . This involves a more important thing in the back, which will be discussed later.

  As can be seen from the above, the unit of DPI itself is also pixel/inch, so density is actually unitless, it is a proportional value.

  The unit of dpi is pixel/inch, which is more in line with the definition of density in physics. Isn’t density a value measured in units, so I prefer to call dpi pixel density, referred to as density, and density is still called density.

 

3. Conversion between units

1. Calculate dpi 

  For example, a machine with a 4-inch screen and a resolution of 480X800 can count its dpi.
  Because you don't know the side length, you can't calculate it separately. 4 is the diagonal length. Then use the Pythagorean theorem to calculate the diagonal pixels directly. Divide by 4, and the calculation is about dpi = 233 pixels/inch.
  Then the density is (233 px/inch) / (160 px/inch) = about 1.46

  By the way, android has only 3 dpi by default, low, medium and high, corresponding to 120, 160, 240. If there is no special setting, all dpi will be counted as these 3. For details, please refer to this post
  http:/ The default in /android.tgbus.com/Android/tutorial/201103/347176.shtml
  is 160.

2. Calculate dp and px

  When we write the layout, we must still know how many px are in 1 dp.

  The conversion formula is as follows: dp = (DPI/(160 pixels/inch)) px = density px

  Note that this is all with units. px is the unit, dp is the unit, and density has no unit.

  For convenience, assuming dpi is 240 pixels/inch, then density is 1.5

  Then it is dp=1.5px, note that this is with units, that is, device independent pixels = density pixels

  Then converted to numerical calculation, it should be the following formula

  PX = density * DP

That is 
  pixel value = density * device independent pixel value, please note that there is a value word here.

 3. Why standard dpi = 160

  (1) In Android Design [1], the dpi of mainstream devices is classified into four grades, 120 dpi, 160 dpi, 240 dpi, 320 dpi

  In actual development, we often need to convert these sizes to each other (for example, complete the design at a certain resolution first, then scale to other sizes for fine-tuning and output), generally according to the ratio between dpi, that is, 2:1.5:1 :0.75 to define the size of the elements in the interface.

  That is to say, if 160 dpi is used as the benchmark, as long as the DP of the size is a common multiple of 4, XHDPI is multiplied by 2, HDPI is multiplied by 1.5, and LDPI is multiplied by 0.75 to satisfy all sizes of integer pixels.

  But assuming that 240 dpi is used as the standard, it requires DP to be a common multiple of 3, multiply by 1.333 under XHDPI, multiply by 0.666 under MDPI, and divide by 2 under LDPI

  Benchmarking LDPI and XHDPI is more complicated, so choose 160 dpi

     (2) This is explained in Google's official documentation, because the first Android device (HTC's T-Mobile G1) was 160dpi .

 

4. Example Analysis

1. Screen size

  It is the size of the mobile phone screen we usually talk about, which is the diagonal length of the screen. Generally speaking, the size unit is inches.
  For example, the screen size of the iPhone 5S is 4 inches. Samsung Note3 is 5.7 inches.

  

figure 1

2. Pixel (pixel)

  Imagine zooming in on the screen and zooming in again, yes! The small dots or squares you see are pixels.

figure 2

3. Resolution

  Refers to the number of pixels in the vertical and horizontal directions on the screen.
  For example, the resolution of iPhone5S is 1136*640; the resolution of Samsung Note3 is 1920*1080;

image 3 

4.dpi

  It is the abbreviation of dot per inch, which is the number of pixels per inch, also called screen density. The larger the value, the clearer the screen.
  The dpi of the iPhone5S is 326; the dpi of the Samsung Note3 is 386

Figure 4

5.dip

  It is the abbreviation of Density independent pixel, which refers to the pixel in the abstract sense. It depends on the screen density of the device.

  It is a unit in Android, dip and dp are the same.

Google's official description is this:
  

Density Independent Pixels (dp)

When defining UI layouts, a virtual pixel unit should be used to represent layout dimensions or positions in a density-independent manner.

A density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density the system assumes for a "medium" density screen. At runtime, the system transparently handles any expansion of dp units according to the actual screen density. Converting dp units to screen pixels is simple: px=dp(dpi/160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. When defining your application's UI, you should always use dp units to ensure that the UI is displayed correctly on screens with different densities.

  That is, on a 160dpi screen, 1dip=1px.
  It is related to the screen density. If the screen density is large, 1dip represents more px. For example, on a 320dpi screen, 1dip=2px.

Why do we better use dip instead of px when laying out?

   It is because there are many mobile phones with different screen densities in this world. What is the screen density? dpi is the number of pixels per unit length.

  Imagine if these phones are the same size and the screen densities are very different, does it mean that one phone has few pixels horizontally and the other phone has many pixels horizontally? Then when we draw the same number of pix, it shows

  Wouldn't the length shown be different?

  For example, in the two mobile phones in the figure below, if a Button with a length of 2px is set at the same time, it will be displayed smaller on a mobile phone with a higher screen density.

  The 2dip-length Buttons set at the same time display the same size on the two mobile phones.

 

Figure 5

  So if you use px as the unit in the App layout, then your App will appear strange phenomena on various devices. 

  Let's take a look at the effect on the emulator. I defined two Buttons, using px and dip as units respectively.

  The layout file is written like this

copy code
<Button android:layout_width="100px"
    android:layout_height="100px"
    android:text="@string/str_button1"/>
 
    <Button android:layout_width="100dip"
    android:layout_height="100dip"
    android:text="@string/str_button1"/>
copy code

The interface displayed is as follows:

  

Image 6 

  getResources().getDisplayMetrics().densityDpi is the screen density.
  getResources().getDisplayMetrics().density can also be understood as how many px is equivalent to 1dip.
  The dpi above is 240, 1dip=1.5px
  , you see, the Button of 100dip is 1.5 times as long as the Button of 100px.

1. Basic Concepts

  • dip : Density independent pixels, device independent pixels.
  • dp : is dip
  • px : pixels
  • dpi        : dots per inch, directly speaking, how many pixels per inch. Common values ​​are 120, 160, and 240. I generally call it pixel density, or simply density
  • density  : It seems to be called density in direct translation. Common values ​​are 1.5, 1.0. Ratio to standard dpi (160px/inc)
  • Resolution: The number of pixels in horizontal and vertical directions, common values ​​are 480X800, 320X480
  • Screen size: The length of the diagonal of the screen. Computer TV is the same.
  • screen ratio issue. Because only the diagonal length is determined, the length of the two sides is not necessarily determined. So with 4:3 and 16:9, the side length of the screen can be calculated.

2. Application

  In android, get the metrics of a window, there are so many values ​​in it

     metrics.density;
     metrics.densityDpi;

  densityDpi: This is the dpi we often say.

  density : In fact, it is the value obtained after DPI / (160 pixels/inch). Isn't it a bit strange because I brought the unit. . . This involves a more important thing in the back, which will be discussed later.

  As can be seen from the above, the unit of DPI itself is also pixel/inch, so density is actually unitless, it is a proportional value.

  The unit of dpi is pixel/inch, which is more in line with the definition of density in physics. Isn’t density a value measured in units, so I prefer to call dpi pixel density, referred to as density, and density is still called density.

 

3. Conversion between units

1. Calculate dpi 

  For example, a machine with a 4-inch screen and a resolution of 480X800 can count its dpi.
  Because you don't know the side length, you can't calculate it separately. 4 is the diagonal length. Then use the Pythagorean theorem to calculate the diagonal pixels directly. Divide by 4, and the calculation is about dpi = 233 pixels/inch.
  Then the density is (233 px/inch) / (160 px/inch) = about 1.46

  By the way, android has only 3 dpi by default, low, medium and high, corresponding to 120, 160, 240. If there is no special setting, all dpi will be counted as these 3. For details, please refer to this post
  http:/ The default in /android.tgbus.com/Android/tutorial/201103/347176.shtml
  is 160.

2. Calculate dp and px

  When we write the layout, we must still know how many px are in 1 dp.

  The conversion formula is as follows: dp = (DPI/(160 pixels/inch)) px = density px

  Note that this is all with units. px is the unit, dp is the unit, and density has no unit.

  For convenience, assuming dpi is 240 pixels/inch, then density is 1.5

  Then it is dp=1.5px, note that this is with units, that is, device independent pixels = density pixels

  Then converted to numerical calculation, it should be the following formula

  PX = density * DP

That is 
  pixel value = density * device independent pixel value, please note that there is a value word here.

 3. Why standard dpi = 160

  (1) In Android Design [1], the dpi of mainstream devices is classified into four grades, 120 dpi, 160 dpi, 240 dpi, 320 dpi

  In actual development, we often need to convert these sizes to each other (for example, complete the design at a certain resolution first, then scale to other sizes for fine-tuning and output), generally according to the ratio between dpi, that is, 2:1.5:1 :0.75 to define the size of the elements in the interface.

  That is to say, if 160 dpi is used as the benchmark, as long as the DP of the size is a common multiple of 4, XHDPI is multiplied by 2, HDPI is multiplied by 1.5, and LDPI is multiplied by 0.75 to satisfy all sizes of integer pixels.

  But assuming that 240 dpi is used as the standard, it requires DP to be a common multiple of 3, multiply by 1.333 under XHDPI, multiply by 0.666 under MDPI, and divide by 2 under LDPI

  Benchmarking LDPI and XHDPI is more complicated, so choose 160 dpi

     (2) This is explained in Google's official documentation, because the first Android device (HTC's T-Mobile G1) was 160dpi .

 

4. Example Analysis

1. Screen size

  It is the size of the mobile phone screen we usually talk about, which is the diagonal length of the screen. Generally speaking, the size unit is inches.
  For example, the screen size of the iPhone 5S is 4 inches. Samsung Note3 is 5.7 inches.

  

figure 1

2. Pixel (pixel)

  Imagine zooming in on the screen and zooming in again, yes! The small dots or squares you see are pixels.

figure 2

3. Resolution

  Refers to the number of pixels in the vertical and horizontal directions on the screen.
  For example, the resolution of iPhone5S is 1136*640; the resolution of Samsung Note3 is 1920*1080;

image 3 

4.dpi

  It is the abbreviation of dot per inch, which is the number of pixels per inch, also called screen density. The larger the value, the clearer the screen.
  The dpi of the iPhone5S is 326; the dpi of the Samsung Note3 is 386

Figure 4

5.dip

  It is the abbreviation of Density independent pixel, which refers to the pixel in the abstract sense. It depends on the screen density of the device.

  It is a unit in Android, dip and dp are the same.

Google's official description is this:
  

Density Independent Pixels (dp)

When defining UI layouts, a virtual pixel unit should be used to represent layout dimensions or positions in a density-independent manner.

A density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density the system assumes for a "medium" density screen. At runtime, the system transparently handles any expansion of dp units according to the actual screen density. Converting dp units to screen pixels is simple: px=dp(dpi/160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. When defining your application's UI, you should always use dp units to ensure that the UI is displayed correctly on screens with different densities.

  That is, on a 160dpi screen, 1dip=1px.
  It is related to the screen density. If the screen density is large, 1dip represents more px. For example, on a 320dpi screen, 1dip=2px.

Why do we better use dip instead of px when laying out?

   It is because there are many mobile phones with different screen densities in this world. What is the screen density? dpi is the number of pixels per unit length.

  Imagine if these phones are the same size and the screen densities are very different, does it mean that one phone has few pixels horizontally and the other phone has many pixels horizontally? Then when we draw the same number of pix, it shows

  Wouldn't the length shown be different?

  For example, in the two mobile phones in the figure below, if a Button with a length of 2px is set at the same time, it will be displayed smaller on a mobile phone with a higher screen density.

  The 2dip-length Buttons set at the same time display the same size on the two mobile phones.

 

Figure 5

  So if you use px as the unit in the App layout, then your App will appear strange phenomena on various devices. 

  Let's take a look at the effect on the emulator. I defined two Buttons, using px and dip as units respectively.

  The layout file is written like this

copy code
<Button android:layout_width="100px"
    android:layout_height="100px"
    android:text="@string/str_button1"/>
 
    <Button android:layout_width="100dip"
    android:layout_height="100dip"
    android:text="@string/str_button1"/>
copy code

The interface displayed is as follows:

  

Image 6 

  getResources().getDisplayMetrics().densityDpi is the screen density.
  getResources().getDisplayMetrics().density can also be understood as how many px is equivalent to 1dip.
  The dpi above is 240, 1dip=1.5px
  , you see, the Button of 100dip is 1.5 times as long as the Button of 100px.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326169213&siteId=291194637