What is the difference between “px”, “dip”, “dp” a

2018-12-30 23:02发布

What is the difference between Android units of measure?

  • px
  • dip
  • dp
  • sp

30条回答
梦寄多情
2楼-- · 2018-12-30 23:27

Screen Size in Android is grouped into categories small, medium, large, extra large, double-extra and triple-extra. Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch (dpi). Screen density is grouped as low, medium, high and extra high. Resolution is the total number of pixels in the screen.

  • dp: Density Independent Pixel, it varies based on screen density . In 160 dpi screen, 1 dp = 1 pixel. Except for font size, use dp always.
  • dip: dip == dp. In earlier Android versions dip was used and later changed to dp.
  • sp: Scale Independent Pixel, scaled based on user’s font size preference. Fonts should use sp.
  • px: our usual standard pixel which maps to the screen pixel.
  • in: inches, with respect to the physical screen size.
  • mm: millimeters, with respect to the physical screen size.
  • pt: 1/72 of an inch, with respect to the physical screen size.

Formula for Conversion between Units

 px = dp * (dpi / 160)

dp to px in device

Following example may help understand better. The scaling occurs based on bucket size of 120(ldpi), 160(mdpi), 240(hdpi), 320(xhdpi), 480(xxhdpi) and 640(xxxhdpi). The Google suggested ratio for designing is 3:4:6:8:12 for ldpi:mdpi:hdpi:xhdpi:xxhdpi

A 150px X 150px image will occupy,

  • 150 dp X 150 dp screen space in mdpi
  • 100 dp X 100 dp screen space in hdpi
  • 75 dp X 75 dp screen space in xhdpi

You may use the following DPI calculator to fix your image sizes and other dimensions when you wish to have an uniform UI design in all Android devices.

DPI Calculator in Java

/*
Program output
LDPI: 165.0 X 60.0
MDPI: 220.0 X 80.0
HDPI: 330.0 X 120.0
XHDPI: 440.0 X 160.0
XXHDPI: 660.0 X 240.0
XXXHDPI: 880.0 X 320.0
*/


public class DPICalculator {

private final float LDPI = 120;
private final float MDPI = 160;
private final float HDPI = 240;
private final float XHDPI = 320;
private final float XXHDPI = 480;
private final float XXXHDPI = 640;    

private float forDeviceDensity;
private float width;
private float height;

public DPICalculator(float forDeviceDensity, float width, float height){
    this.forDeviceDensity = forDeviceDensity;
    this.width = width;
    this.height = height;
}

public static void main(String... args) {
    DPICalculator dpiCalculator = new DPICalculator(240,330,120);
    dpiCalculator.calculateDPI();
}


private float getPx(float dp, float value) {
    float px = dp * (value / forDeviceDensity );        
    return px;
}

private void calculateDPI() {

    float ldpiW = getPx(LDPI,width);        
    float ldpiH =  getPx(LDPI,height);
    float mdpiW = getPx(MDPI,width);        
    float mdpiH =  getPx(MDPI,height);        
    float hdpiW = getPx(HDPI,width);        
    float hdpiH =  getPx(HDPI,height);       
    float xdpiW = getPx(XHDPI,width);        
    float xdpiH =  getPx(XHDPI,height);
    float xxdpiW = getPx(XXHDPI,width);        
    float xxdpiH =  getPx(XXHDPI,height);
    float xxxdpiW = getPx(XXXHDPI,width);        
    float xxxdpiH =  getPx(XXXHDPI,height);

    System.out.println("LDPI: " + ldpiW + " X " + ldpiH);
    System.out.println("MDPI: " + mdpiW + " X " + mdpiH);
    System.out.println("HDPI: " + hdpiW + " X " + hdpiH);
    System.out.println("XHDPI: " + xdpiW + " X " + xdpiH);
    System.out.println("XXHDPI: " + xxdpiW + " X " + xxdpiH);
    System.out.println("XXXHDPI: " + xxxdpiW + " X " + xxxdpiH);        
   }
}

More Information refer following link.

http://javapapers.com/android/difference-between-dp-dip-sp-px-in-mm-pt-in-android/

查看更多
像晚风撩人
3楼-- · 2018-12-30 23:28

You can see the difference between px and dp from the below picture, and you can also find that the px and dp could not guarantee the same physical sizes on the different screens.

enter image description here

查看更多
低头抚发
4楼-- · 2018-12-30 23:30

I have calculated the formula below to make the conversions dpi to dp and sp enter image description here

查看更多
余生无你
5楼-- · 2018-12-30 23:31

I want to provide an easy way to understand dp. In fact, I think dp is the easiest one to understand. dp is just a physical length unit. It's of the same dimension as mm or inch. It's just convenient for us to write 50dp, 60dp rather than 50/160 inch or 60/160 inch, because one dp is just 1/160 inch whatever the screen size or resolution is.

The only problem is that, the android dpi of some screens are not accurate. For example, a screen classified to 160dpi may have 170dpi indeed. So the computation result of dp is fuzzy. It should be approximately the same as 1/160 inch.

查看更多
人间绝色
6楼-- · 2018-12-30 23:32

px

Pixels - corresponds to actual pixels on the screen.

dp or dip

Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen.

Use of dp:

Density independence - Your application achieves “density independence” when it preserves the physical size (from the user’s point of view) of user interface elements when displayed on screens with different densities. (ie) The image should look the same size (not enlarged or shrinked) in different types of screens.

sp

Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference.

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

查看更多
人间绝色
7楼-- · 2018-12-30 23:33

Screen size in Android is grouped into categories ldpi, mdpi, hdpi, xhdpi, xxhdpi and xxxhdpi. Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch (dpi).

PX(Pixels):

  • our usual standard pixel which maps to the screen pixel. px is meant for absolute pixels. This is used if you want to give in terms of absolute pixels for width or height. Not recommended.

DP/DIP(Density pixels / Density independent pixels):

  • dip == dp. In earlier Android versions dip was used and later changed to dp. This is alternative of px.

  • Generally we never use px because it is absolute value. If you use px to set width or height, and if that application is being downloaded into different screen sized devices, then that view will not stretch as per the screen original size.

  • dp is highly recommended to use in place of px. Use dp if you want to mention width and height to grow & shrink dynamically based on screen sizes.

  • if we give dp/dip, android will automatically calculate the pixel size on the basis of 160 pixel sized screen.

SP(Scale independent pixels):

  • scaled based on user’s font size preference. Fonts should use sp.

  • when mentioning the font sizes to fit for various screen sizes, use sp. This is similar to dp.Use sp especially for font sizes to grow & shrink dynamically based on screen sizes

Android Documentation says:

when specifying dimensions, always use either dp or sp units. A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An sp is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size

查看更多
登录 后发表回答