The difference between dip, DP, SP, PT, and PX in Android

Source: Internet
Author: User

1. Overview

In the past, programmers typically designed computer user interfaces in pixels. For example, the image size is 80x32 pixels. The problem with this approach is that if you run the program on a new monitor with a higher dots per inch (dpi), the user interface will appear small. In some cases, the user interface may be too small to see the content. This problem can be solved by developing the program in a resolution-independent unit of measurement. Android App development supports different units of measurement.

2. Meaning of unit of measure

Dip:device independent pixels (device independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels.

Dp:dip is the same.

Px:pixels (pixels). Different devices show the same effect, generally we hvga represent 320x480 pixels, this use more.

Pt:point, is a standard length unit, 1pt=1/72 inches, for the printing industry, very simple to use;
sp:scaled pixels (enlarge pixels). Mainly used for font display of best for textsize.

In (inches): unit of length.
MM (mm): unit of length.

3. Conversion formula of unit of measure

In the Android source package Typedvalue.java, we look at the following function:

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;

}

The function: convert units to pixels.

Metrics.density: The default value is Density_device/(float) density_default;

Metrics.scaleddensity: The default value is Density_device/(float) density_default;

METRICS.XDPI: The default value is Density_device;

Density_device: For screen density

Density_default: Default value is 160

4, screen density: Indicates how many display points per inch, and the resolution is two different concepts.

Android mainly has the following screens: the following table

Screen

Tyep

Width

Pixels

Height

Pixels

Size

Range (inches)

Screen density

QVGA

240

320

2.6-3.0

Low

WQVGA

240

400

3.2-3.5

Low

Fwqvga

240

432

3.5-3.8

Low

HVGA

320

480

3.0-3.5

Medium

WVGA

480

800

3.3-4.0

High

FWVGA

480

854

3.5-4.0

High

WVGA

480

800

4.8-5.5

Medium

FWVGA

480

854

5.0-5.8

Medium

Note

Currently Android default low=120; Medium =160; high = 240

5. In summary

According to PX = Dip * density/160, when the screen density is 160, px = Dip
According to Google's suggestion, TextView's font size is best to use SP to do units, and view the source of TextView know that Android by default using the SP as the font size units. The dip is used as the unit for other elements.
(Transferred from: http://greatverve.cnblogs.com/archive/2011/12/27/Android-dip-dp-sp-pt-px.html)

(The following transfers are from: http://www.cnblogs.com/fbsk/archive/2011/10/17/2215539.html)

1. Dip:device independent pixels (device independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommended use this, do not rely on pixels.
It is important to note that dip is related to screen density and that the screen density is related to specific hardware and that the hardware settings are incorrect and may cause the dip to not display properly. On the screen density of 160 display, 1dip=1px, sometimes your screen resolution is very large, such as 480*800, but the screen density is not set correctly for example or 160, then all the use of dip will show the anomaly, the basic display is too small.
Dip conversion:
Dip (value) = (int) (PX (value)/1.5 + 0.5)
2. DP: Very simple, and dip is the same.
3. Px:pixels (pixels), different devices different display display effect is the same, this is absolute pixels, is how much will never change.
4. sp:scaled pixels (enlarge pixels). Mainly used for font display of best for textsize.

Note: According to Google's recommendation, pixels uniformly use the dip, the font unified use SPFor example, the difference between PX and dip:px pixels, if using PX, will be used to draw the actual pixel, than a bar, with a line length of 240px, in the 480 wide simulator is half the screen width, and in the 320 wide simulator is 2/3 of the screen width. Dip, that is, the height of the screen is divided into 480 points, the width divided into 320 points. For example, you make a 160dip horizontal line, whether you are in 320 or 480 of the simulator, is half the length of the screen.
public static int dip2px (context context, float Dipvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Dipvalue * scale + 0.5f);
}

public static int Px2dip (context context, float Pxvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}

The difference between dip, DP, SP, PT, and PX in Android

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.