Px, DP, and sp. What are the differences between these units?

Source: Internet
Author: User

I believe that every new Android beginner will encounter this problem. I hope this post will not make you tangle any more.

Px:

That is, pixels. 1px indicates a physical pixel on the screen;

The unit of PX is not recommended because the actual size of a PX image may be different on different mobile phones, as shown in (the image is from the android developer guide, the same below ).

The occasional use of PX is that when you need to draw a 1 pixel table line or shadow line, other units such as DP will appear blurred.

DP:

This is the most common but hard to understand unit of size. It is closely related to "pixel density", so we will first explain what is pixel density. Suppose there is a mobile phone with a physical screen size of 1.5 inch x 2 inch and a screen resolution of 240x320, then we can calculate it on the screen of this mobile phone, the number of pixels per inch is 240/1. 5 = 160 DPI (horizontal) or 320/2 = 160 DPI (portrait). 160dpi is the pixel density of the mobile phone. The unit of pixel density DPI is the abbreviation of dots per inch, the number of pixels per inch. The horizontal and vertical values are the same, because most mobile phone screens use square pixels.

Different mobile phones/tablets may have different pixel density. For example, for a 4-inch mobile phone, x320 resolution and X resolution, the former has a relatively low pixel density. The Android system defines four pixel density types: low (120 DPI), medium (160 DPI), high (240 DPI), and ultra (320 DPI ), the coefficients from Dp to PX are 0.75, 1, 1.5, and 2 respectively. The coefficient multiplied by the DP length is the number of pixels. For example, if there is an image with a length of "80dp" on the interface, it is displayed as 80x1.5 = 120px on a DPI mobile phone, the actual display on the 320dpi mobile phone is 80x2 = 160px. If you compare the two mobile phones, you will find that the physical size of the image is "almost". This is the effect of using DP as the unit. See.

Update 20140701: Is the screen width of all Android phones fixed by DP (for example, 320dp? The answer is no. if you write a program to draw a horizontal line with the width equal to DP and run it on different mobile phones, you will find that the horizontal line on some mobile phones is shorter than the mobile phone screen, and some are longer than the screen, the differences between tablets and mobile phones are more obvious.

Dip:

It is exactly the same as DP, but the name is different. Dip was used in earlier Android versions. We recommend that you use the DP name for consistency with sp.

SP:

Scale-independent pixel ). SP is similar to DP, but the only difference is that the Android system allows users to customize the text size (small, normal, large, large, and so on ), when the text size is "normal", 1sp = 1dp = 0.00625 inch, and when the text size is "large" or "super large", 1sp> 1dp = 0.00625 inch. It is similar to the effect after we adjust the font size in Windows-the window size remains unchanged, only the text size changes.

There are also a few less used units:

MM:

That is, millimeters;

In:

That is, inch, 1 inch = 2.54 cm (about );

PT:

1pt = 1/ 72 inch = 0.035 cm;

Best Practices,All text sizes are SP units, and all non-text sizes are DP units.. For example, textsize = "16sp", layout_width = "60dp", and occasionally use PX units. For example, when you need to draw a thin separator line on the screen:

<View layout_width="match_parent" layout_height="1px"/>

Finally, we recommend a reference image for Android UI design: Android design cheat sheet.

References:

Difference of PX, DP, dip and SP in Android?
Supporting multiple screens
Displaymetrics

Px, DP, and sp. What are the differences between these units?

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.