Differences between DIP, DP, SP, PT, and PX in Android

Source: Internet
Author: User
1. Overview

Past,ProgramUsers usually design the computer user interface in pixels. For example, the image size is 80x32 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, we can solve this problem by developing a program using a measurement unit unrelated to the resolution. Android Application Development supports different measurement units.

2. Meaning of measurement units

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.

DP: dip is the same

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.

In (INCHES): the unit of length.
Mm (mm): the unit of length.

3. Conversion Formula of measurement units

In the android source code package typedvalue. Java, we can look at the following functions:

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;

}

This function converts 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: screen Density

Density_default: The default value is 160.

 

4. Screen density:The number of display points per inch is different from the resolution.

Android has the following screens:

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

Remarks

Currently, the default Android low = 120; medium = 160; high = 240

5. To sum up

 Based on PX = dip * density/160, when the screen density is 160, PX = dip
According to Google's suggestion, it is best to use SP as the unit for the font size of textview, and check the source code of textview. Android uses SP as the font size unit by default. Units that use dip as other elements.
URL:Http://greatverve.cnblogs.com/archive/2011/12/27/Android-dip-dp-sp-pt-px.html

Related Article

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.