ANDROID:PX, DP, and SP are the differences between these units

Source: Internet
Author: User

Tangled for a long time, PX, DP, dip, SP What is the difference? Entanglements sometimes use DP and DPI to feel no difference in such problems.

PX (pixel):

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

PX units are not recommended, as the same 100px images may differ in actual size displayed on different phones.

DP(device independent pixels (unit independent pixels)):

This is the most common but also the most difficult to understand the size of units. It is closely related to pixel density, so let's first explain what pixel density is. Given a cell phone with a physical size of 1.5 inches x2 inches and a screen resolution of 240x320, we can calculate the number of pixels per inch on the screen of this phone as 240/1.5=160dpi (landscape) or 320/2=160dpi (portrait), 160DPI is the pixel density of this phone, and the pixel density unit dpi 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'll find that the physical size of the picture is "almost", which is the effect of using DP as a unit.

Dip (device independent pixels (unit 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.

To illustrate the difference between PX and DP:px is a pixel, if using PX, will use the actual pixel painting, compared to a bar, with a length of 240px of the horizontal line, in the 480 wide simulator is half the screen width, and in the 320 wide simulator is 2/3 of the screen width. dip, that is, the height of the screen is divided into 480 points, the width divided into 320 points. For example, you make a 160dp horizontal line, whether you are in 320 or 480 of the simulator, is half the length of the screen. SP(scaled pixels (enlarge pixels)):

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.

There are a few smaller units of size:

Mm:

i.e. mm;

In

i.e. inches, 1 inches = 2.54 cm (approx);

Pt:

1pt=1/72 inches = 0.035 cm;

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

ANDROID:PX, DP, and SP 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.