Differences and usage of PX, dip, and SP

Source: Internet
Author: User

I. Overview

We often set the container length during page layout, but we are annoyed by which unit of length should be used.

The following types of description areas are supported in Android.

Px (pixels)-pixels: different devices have the same display effect. Generally, hvga represents 320x480 pixels, which is usually used.

Dip (device independent pixels) -- device independent pixel: This is related to the device hardware. Generally, we recommend that you use this parameter to support wcga, hvga, and qvga, instead of pixels. Equivalent to DP.

SP (scaled pixels-best for text size) -- pixels with proportions.

Pt (points) -- LB: 1pt = 1/72 inch

In (INCHES) -- inch

Mm (millimeters) -- Mm

Ii. Details

SP because it is a magnified pixel, it is mainly used for font display. According to Google's suggestion, it is best to use SP as the unit for textview font size, the source code of textview shows that android uses the horizontal font size unit by default.

Px and dip are the most commonly used in Android. But what is the difference between the two?

In the hvga screen density = 160; qvga screen density = 120; WVGA screen density = 240; wqvga screen density = 120 density value indicates the number of display points per inch, resolution and resolution are two concepts. For details about the screen resolution of different density values, take the WVGA (density = 240) of Dip * 800dip as an example.

When 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

When 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

When 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, when the screen density is 240, the resources using the hdpi label

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.

**************************************** **************************************** ******************

Reference the description of DIP, DP, PX, SP, and other units on Android

This article references the description of DIP, DP, PX, SP, and other units on Android from shixin20051108.

[Dip: Device Independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware of the device. We recommend that you use this function to support WVGA, hvga, and qvga without pixels.
Px: pixels (pixels). Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.

PT: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;

SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts.

======================================

Conversion (using SP and Pt as an example)

View the source code of textview and other classes. We can see that:

Case complex_unit_px:

Return value;

Case complex_unit_sp:

Return Value * metrics. scaleddensity;

Case complex_unit_pt:

Return Value * metrics. xdpi * (1.0f/72 );

--------------------------

Scaleddensity = density_device/(float) density_default;

Xdpi = density_device;

--------------------------

Density_default = density_medium = 160;

========================================================== ====

Therefore, if PT and SP take the same value of 1, the coefficient between 1pt and 1sp can be set to X,

1 * density_device/72 = x * 1 * density_device/160 = & gt;

X = 160/72 = 2.2222

That is to say, in Android, 1pt is roughly equal to 2.22sp

========================================================== ======================================

What are dip and SP?

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.

The measurement units irrelevant to resolution can solve this problem. Android supports all of the following units.
Px (pixel): the point on the screen.

In (INCHES): the unit of length.

Mm (mm): the unit of length.

Pt (lbs): 1/72 inch.

DP (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.
Dip: it is the same as DP and is mostly used in Android/ophone examples.

SP (pixels irrelevant to the scale): similar to DP, but can be scaled based on the user's font size preferences.
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.

**************************************** *****************************
Dip: Device Independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware of the device. We recommend that you use this function to support WVGA, hvga, and qvga without pixels.
Px: pixels (pixels). Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.
PT: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;
SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts. According to Google's suggestion

It is best to use SP as the unit of font size, and check the source code of textview. We can see that android uses SP as the font size unit by default.

In Android, 1pt is about equal to 2.22sp and above for reference. If the UI can provide the best design in SP units, if the design does not have the concept of SP, then, the developer can take an approximate value by appropriate conversion.

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.

The measurement units irrelevant to resolution can solve this problem. Android supports all of the following units.
Px (pixel): the point on the screen.

In (INCHES): the unit of length.

Mm (mm): the unit of length.

Pt (lbs): 1/72 inch.

DP (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.
Dip: it is the same as DP and is mostly used in Android/ophone examples.

SP (pixels irrelevant to the scale): similar to DP, but can be scaled based on the user's font size preferences.

Resolution: the number of points on the screen, such as 800x480. It is the display unit of the software, in PX. The density (density) value indicates the number of display points per inch, and the resolution is two concepts. 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.

Generally, Android uses dip to set the length and width, and SP to set the font size. the screen density is 160, 1dp = 1px = 1dip, 1pt = 160/72 SP 1pt = 1/ 72 inch. when the screen density is 240, 1dp = 1dip = 1.5px.
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.

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.