"Android Notes" pixel unit px, DP, SP differences and understanding

Source: Internet
Author: User

    • 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 (images from Android Developer Guide, same as below).

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

    • 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 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 will find the physical size of the picture "almost", which is the effect of using DP as unit, see.

is the screen width of all Android phones a fixed value (e.g. 320DP) measured in DP? The answer is no, if you write a program to draw a horizontal line width equal to 320DP, run on different phones, you will find that some phones are shorter than the phone screen, some are longer than the screen, on the tablet and mobile phone compared to the difference is more obvious.

    • Dip

  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.

    • 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 on the screen when the divider:

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

Attached to an Android UI design reference map: Android designs Cheat Sheet

Reference Original: http://www.cnblogs.com/bjzhanghao/archive/2012/11/06/2757300.html#

"Android Notes" pixel unit px, DP, SP differences and understanding

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.