PX, DP, and SP, what are the differences between these units?

Source: Internet
Author: User

Dp

This is the most common but also the most difficult to understand the size of units. It is closely related to "pixel density", so

First, let's explain what pixel density is. If you have a cell phone with a physical size of 1.5 inches x2 inches and a screen resolution of 240x320, we can calculate the screen on this phone,

The number of pixels per inch is 240/1.5=160dpi (landscape) or 320/2=160dpi (portrait), 160dpi is the pixel density of the phone, and the unit dpi of pixel density is the abbreviation for dots per inch, which is the number of pixels.

This value is the same for both landscape and portrait, because most phone screens use a square pixel point.

Different cell phones/tablets may have different pixel densities, such as the same as 4-inch mobile phones, with 480x320 resolution also has 800x480 resolution, the former pixel density is relatively low.

The Android system defines four pixel densities: low (120DPI), Medium (160DPI), High (240dpi), and Superelevation (320DPI), and their corresponding DP-to-PX coefficients are 0.75, 1, 1.5, and 2, which are multiplied by the DP length as the number of pixels.

For example, the interface has a length of "80dp" picture, then it is actually displayed as 80x1.5=120px on the 240dpi mobile phone, on the 320dpi phone actually displayed as 80x2=160px.

If you compare these two phones together, you will find the physical size of the picture "almost", which is the effect of using DP as unit

Px:

That is, the pixel, 1px represents a physical pixel on the screen;

PX units are not recommended, as the same 100px picture, the actual size displayed on different phones may be different, as shown in

Dip

Density independent pixels, device independent pixels.

Exactly the same as DP, except the name is different. The dip was used in earlier versions of Android, and the DP name was later recommended for unification with SP.

For example a machine, screen 4 inch, resolution 480x800, his DPI can count it.


Because do not know the side length, certainly cannot separate calculates, 4 is the diagonal length, that directly uses the Pythagorean theorem to calculate the diagonal pixel, divides 4, calculates is probably dpi = 233 pixels/inch.

Then density is (233 px/inch)/(Px/inch) = 1.46 or so

Incidentally, Android default only 3 Dpi,low, medium and High, corresponding to 120, 160, 240, if there is no special settings, all DPI will be counted as these 3, specifically refer to this post

Http://android.tgbus.com/Android/tutorial/201103/347176.shtml

The default is 160.

Sp:

Abstract pixels independent of scale (Scale-independent Pixel).

The SP and DP are similar but the only difference is that the Android system allows users to customize text size (small, normal, large, oversized, and so on), 1sp=1dp=0.00625 inches when the text size is "normal", and when the text size is "large" or "oversized", 1sp>1dp= 0.00625 inches.

Similar to what we did after adjusting the font size in Windows-the window size is constant, only the text size changes.

best practice, the size of the text is used in SP units, non-text dimensions are used DP units .

For example textsize= "16sp", layout_width= "60DP", occasionally need to use PX units, such as the need to draw a thin line of separation lines on the screen

Pixel Conversion

When we write the layout, we must still know how many px the 1 DP actually has.

The conversion formula is as follows: DP = (dpi/(160 megapixels/inch)) px = density px

Note that these are all with units. PX is unit, DP is unit, density no unit.

For convenience, assuming DPI is 240 pixels per inch, then density is 1.5

So that's dp=1.5px, note that this is a unit, which is device-independent pixels = density pixels

Then the conversion to the numerical calculation, it should be the following formula

PX = density * DP

That is
Pixel value = density * device-independent pixel value, note that there is a value word here.

Why Standard DPI = 160

(1) Android Design [1] in the mainstream device DPI into four grades, three dpi, the DPI, the DPI, the

In actual development, we often need to convert these dimensions to each other (such as the first design at a certain resolution, and then zoom to other resizing after the output), generally according to the ratio of DPI is 2:1.5:1:0.75 to the elements in the interface to the dimension definition.

That is, if the size of the DP is 4 common multiple, XHDPI multiplied by the 2,hdpi under the 1.5,ldpi multiplied by 0.75 can satisfy all the dimensions are integer pixel.

But assuming a standard of up to 3 dpi, that requires DP to be common multiple, XHDPI multiplied by 1.333,mdpi under 0.666, ldpi divided by 2

The ldpi and XHDPI are more complex, so select the DPI

(2) This is explained in Google's official documentation, as the first Android device (HTC's T-mobile G1) is 160dpi.

Why should we use dip in the layout, not with PX?

Because there are many different screen densities in this world, what is the screen density? is the DPI, which is the number of pixels in the unit length.

Imagine, if the size of these phones, the screen density is very different, it is not said that a mobile phone in the horizontal direction of the pixel is very small, the other mobile phone in the horizontal direction of a lot of pixels? When we draw the same number of PIX, it shows

The length of the show will not be the same?

For example, the figure below two mobile phones, while setting the 2px length of the button, in the screen density of the mobile phone will be shown relatively small.

While setting the 2dip length button, the size shown on two phones is the same.

So if you use the PX as the unit in the app layout, then your app runs on every device and there's a weird phenomenon.

To look at the effect on the emulator, I defined two buttons, respectively, using PX and dip units.

This is what the layout file says.

<button android:layout_width="100px"Android:layout_height="100px"Android:text="@string/str_button1"/> <button android:layout_width="100dip"Android:layout_height="100dip"Android:text="@string/str_button1"/>

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.