Difference between dip, dp, sp, pt and px in Android, androiddip

Source: Internet
Author: User

Difference between dip, dp, sp, pt and px in Android, androiddip

1. Overview

In the past, programmers often designed computer user interfaces 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. The screen density is different from the resolution.

Android has the following screens:

Screen

Tyep

Width

Pixels

Height

Pixels

Dimensions

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.
(

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.