Conversion Formulas Between PX, In, mm, PT, DP, dip, and SP and screen density difference

Source: Internet
Author: User

Px: The pixel of the screen.
In: inches
MM: mm
PT: LB, 1/72 inch
DP: an abstract unit based on density. If a screen of DPI is displayed, 1dp = 1px.
Dip: equivalent to DP
SP: similar to DP, but it also scales according to the user's font size preference.
We recommend that you use SP as the unit of text, and dip for others.

The following is an overview of the relationship between DIP and PX:
 

The following figure shows the resolution information density, which is the screen density.

 

Name Resolution Screen Density
Qvga 320*240 120
Wqvga400 400*240 120
Wqvga432 432*240 120
Hvga 640*480 160
Wsvga 1024*600 160
Wxga800 1280*800 160
Wvga800 800*480 240
Wvga854 854*480 240
Wxga720 1280*720 320

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.

The following uses several real machines to deepen the cognition of related concepts.

  Samsung i5801 HTC wildfire S (G13) HTC sensation (G14) Meizu M9
Resolution 240*400 320*480 540*960 640*960
Screen Size 3.2 inch 3.2 inch 4.3 inch 3.5 inch
Screen Density 145.77 180.27 256.15 329.65
DPI obtained by displaymetrics 120 160 240 320

 

The yellow part is the screen density calculated using the formula above, and the green part is the DPI obtained using displaymetrics.

There are two reasons for the inconsistency between the two data.

1. The screen size parameter provided by the vendor is not too precise

2. The density defined in displaymetrics is only 120, 160, 213, 240, and 320.

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

Here, we can push back the screen size from another angle. Taking meizu M9 as an example, through displaymetrics, we can get that both xdpi and ydpi are 325.12.

In this way, the screen width is 640/325. 12 = 1.9685 inch, and the screen height is 960/325. 12 = 2.9527 inch.

The diagonal line size is 3.5487 inch, calculated using the hook theorem.

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

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

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

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 );
}

 

 

Term

Explanation

Px

(Pixel pixels)

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

(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, which can be used as the unit of length,Different devices have different display effectsThis is related to the hardware of the device. We recommend using this to support WVGA, hvga, and qvga without pixels. The corresponding formula for dip and specific pixel values isDip value = device density/160 * pixel value, We can see that 1px = 1dip on a device with a DPI (pixel density) of 160dpi

SP

(Scaledpixels

Zoom in pixels)

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.

 

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.