Description and difference of DIP, DP, PX, SP, and other units

Source: Internet
Author: User

Author: Fan Xiaowei

Dip: Device independentpixels, device independent pixel, an abstract unit based on density:


The display effects of different devices are related to the hardware of the device. We recommend that you use this function to support WVGA, hvga, and qvga without pixels.

Programs Developed on Android will run on mobile phones with different resolutions. In order to make the appearance of the program not significantly different, we introduced the concept of dip. For example, a rectangle of 10x10 dip is defined. on a screen with a resolution of DPI, for example, G1, it is exactly 10x10 pixels. For a 240 DPI screen, it is 15x15 pixels. The conversion formula is pixs = dips * (density/160). density is the screen resolution.

Note that the dip is related to the screen density, and the screen density is related to the specific hardware. Incorrect hardware settings may cause the dip to fail to be properly displayed. On a display with a screen density of 160, 1dip = 1px, sometimes your screen resolution may be large, such as 480*800, but the screen density is not properly set, for example, 160, when dip is used, an exception is displayed, which is usually too small.

 

DP: density-independent pixels:

It is the same as dip. An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.

 

Px: pixels, pixels:

The dots on the screen show the same effect on different display screens of different devices. This is an absolute pixel, and the value will never change.

 

SP: scaled pixels-bestfor text size, amplifying pixels, pixels irrelevant to the scale:

It mainly deals with the font size, similar to DP, but can be scaled based on the user's font size preferences, mainly used to display the best for text size in the font.

In the past, programmers often designed computer user interfaces in pixels. For example, define a form field with a width of 300 pixels. the spacing between columns is 5 pixels, And the Icon size is 16 × 16 pixels. The problem with this processing is that if you run the program on a new display with a dot (DPI) higher per inch, the user interface will look small. In some cases, the user interface may be small to difficult to see the content.

Therefore, according to Google's suggestion, it is best to use SP as the unit of textview font size, and to view the textview source code, Android uses SP as the font size unit by default.

According to Google's recommendation, dip is used for pixels and SP is used for fonts.

 

The following is an overview of the relationship between DIP and PX:

Qvga screen density = 120 qvga (240*320)

Hvga screen density = 160 hvga (320*480)

WVGA screen density = 240 WVGA (480*800)

Wqvga screen density = 120 wqvga (240*400)

The value of density indicates the number of display points per inch, and resolution.

 

For the screen resolution information of different density values, take the WVGA (density = 240) of 480dip * 800dip as an example:

Density = 120

The actual screen resolution is 240px * 400px (two points correspond to one resolution)

The height of the status bar is 19px or 25dip.

The screen width is 400px or 800dip, and the working area height is 211px or 480dip.

Screen width: Px or dip, working area Height: 381px or 775dip

 

Density = 160

The actual screen resolution is 320px * 533px (three points correspond to two resolutions)

The height of the status bar is 25px or 25dip.

The screen width is 533px or 800dip, and the working area height is 295px or 480dip.

Screen width: 320px or 480dip, working area Height: 508px or 775dip

 

Density = 240

The actual screen resolution is 480px * 800px (one point for one resolution)

The height of the status bar and title bar is 38px or 25dip.

The screen width is PX or dip, and the working area height is PX or dip.

Screen width 480px or 480dip, working area height 762px or 775dip

 

In the APK Resource Package

Resources that use the hdpi label when the screen density is 240

Resources that use the mdpi label when the screen density is 160

Ldpi tag resources are used when the screen density is 120.

Resources without any tags are shared in various resolutions.

When layout, use the unit dip as much as possible, and use less PX.

 

Formula for converting DP to PX:

Pixs = dips * (densitydpi/160)

DIPS = (pixs * 160)/densitydpi

 

DP and PX conversion methods:

Public static intDiptopx (contextcontext,FloatDipvalue)
{

Final floatScale = context. getresources (). getdisplaymetrics (). density;

Return(Int) (Dipvalue * scale + 0.5f );

}

 

Public static intPxtodip (contextcontext,FloatPxvalue)
{

Final floatScale = context. getresources (). getdisplaymetrics (). density;

Return(Int) (Pxvalue/scale + 0.5f );

}

 

To enable normal display of the current and future display types on the user interface, we recommend that you always use SP as the unit of text size and dip as the unit of other elements. Of course, you can also consider using a vector image instead of a bitmap. If
Ui can provide the best design in SP units. If there is no sp concept in the design, the developer can also take an approximate value through appropriate conversion.

The measurement units irrelevant to resolution can solve this problem. Android supports all of the following units.

Other units that are not commonly used:

PT: Point, LB, is a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use; in Android, 1pt is about equal to 2.22sp.

In: inches, in: length unit.

MM: millimeters, MM: length unit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.