Px, DP, dip, SP, In, mm, PT detailed analysis

Source: Internet
Author: User
Px, DP, dip, SP, In, mm, PT detailed analysis

Px :( pixels): the pixels on the screen. Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.

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

 

DP: an abstract unit based on density. If a screen is 160dpi, 1dp = 1px.

SP :( scaled pixels-best for text size), with a proportional pixel, similar to DP, but also scales according to the user's font size preference. Google recommends using SP as the unit of text, and dip for others.

 

In :( inches) inches

MM :( millimeters) mm

PT :( points), LB, 1/72 inch

 

 

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 details about the screen resolution of different density, 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)

 

Density = 160

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

 

Density = 240

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

 

In the APK Resource Package

Resources that use the xhdpi label when the screen density is 320

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.

Use the unit dip as much as possible during layout, 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 int dip2px (context, float dipvalue ){

Final float scale = context. getresources (). getdisplaymetrics (). density;

Return (INT) (dipvalue * scale + 0.5f );

}

 

Public static int px2dip (context, float pxvalue ){

Final float scale = context. getresource (). getdisplaymetrics (). density;

Return (INT) (pxvalue/scale + 0.5f );

}

 

How can I obtain the screen resolution and screen density of a device?

For example, if the resolution is 480*800 and the density is 240, the method is as follows:

Displaymetrics dm = getresources (). getdisplaymetrics ();

You can obtain the following information:

DM. densitydpi = 240

DM. widthpixels = 480

DM. heightpixels = 800

 

Glossary

Px (pixel)

Different devices have the same display effect. The "same" indicates that the number of pixels remains unchanged. For example, if the specified UI length is PX, the UI length is PX regardless of the resolution. It is precisely because of this that the UI is magnified and distorted on a small resolution device, and scaled down on a large resolution device.

Screen Size)

Generally, the screen size of a mobile phone, such as 1.6 inch, 1.9 inch, or 2.2 inch, refers to the diagonal line length, rather than the cell phone area. We can obtain the width and length of the mobile phone based on the hook theorem. Of course, there is still an area.

Resolution (resolution)

The number of pixels in the vertical and horizontal directions of the mobile phone screen. For example, if the resolution is 480*320, the device has 480 pixels vertically and 320 pixels horizontally.

DPI (dots per inch pixel density)

The number of records in each inch. For example, 160 DPI indicates that the distance from each inch on the mobile phone is pixels in the horizontal or vertical direction. Assume that the device resolution is 320*240, the screen length is 2 inch-1.5 inch-320/2, DPI = 240/1. 5 = 160

Note: This value corresponds to the value of the densitydpi attribute in the displaymetrics class.

Density (density)

The number of records per square inch.

Density = resolution/screen size

Note: In the displaymetrics class, the value of the attribute density is DPI/160, which can be used for mutual conversion between PX and dip.

Dip (device-independent pixel, device independent pixel)

Same as DP, it can be used as a unit of length. Different devices have different display effects. This is related to the hardware of the device. We recommend that you use this parameter to support WVGA, hvga, and qvga without pixels. The corresponding formula of dip and specific pixel values is dip value = device density/160 * pixel value. It can be seen that 1px = 1dip on a device with DPI (pixel density) of 160dpi

SP (scaledpixels pixel amplification)

It is mainly used for font display (best for textsize ). 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.

HD

HD is the standard for high-resolution digital TVs. The resolution is 1280px x 720px (720 p/720i)

Fhd

(Full FD), resolution: 1920px x 1080px, (1080 p/1080i)

Qhd

Quarter high definition (Full HD 1920x1/4 PX (area) with a resolution of 960x540px

In Android development, to adapt to the resource files under the preceding three resolutions, each resource folder name is:

Drawable-sw360dp-xhdpi (HD)

Fhd: drawable-sw540dp-xhdpi

Qhd: drawable-sw270dp-xhdpi

The SW <n> DP indicates that the layout file under this folder is loaded only when the minimum bandwidth of the device's short side is N. For example, the device is 960x540px resolution, then this APK installed on your device will load the layout file in the layout-sw480dp, note that manifest. xml also needs to be set.

Px, DP, dip, SP, In, mm, PT detailed analysis

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.