Differences in DP, DPI, PX in Android Development (RPM)

Source: Internet
Author: User

I. Basic CONCEPT-DP: Wirelessly relative Size-dpi: (dot per inch) pixels per inch How much-px: Pixel point ii. detailed description 1, px and dpi-px:

The usual "1920x1080" is just the number of pixels, which is 1920pxx1080px, which represents 1920 pixels on the phone's height and 1080 pixels in width.

-DPI:

To determine the display of the phone screen is good or bad, but also to consider the width of the screen (inches), that is, DPI is the number of pixels per inch to evaluate the display of the screen. (otherwise if the phone resolution is 1920x1080, but the screen is dozens of inch, that will not be very good, even if you can see small blocks of pixels, it will affect the visual effect more.) )

2, DP (1), how to understand DP is good?
    • In fact, DP is to allow developers to set the length of the different screen (resolution/size is DPI) to obtain a different number of pixels (px). For example: I set a control length of 1DP, then on 160dpi the control length is 1px, on the 240dpi screen, the length of the control is 1*240/160=1.5 pixels.
    • That is, DP changes the number of pixels in the length of the control along with different screens.
    • The official narrative about DP is that when the screen has 160 pixels per inch (i.e. 160dpi), the DP is equivalent to PX. What if it's 240 pixels per inch? 1dp->1*240/160=1.5px, that is, 1DP and 1.5px are equivalent.
    • In fact, remember thatDP will eventually be sized to the number of pixels, because only the number of pixels is the most intuitive.
(2), the method of calculating DP
    • Generally speaking, we write programs almost useless to calculate DP, is directly in their own sense of writing. DP is calculated here just for the better understanding of DP, and can be calculated if someone wants to set the size of the control precisely.
    • Let me draw a picture directly:
    • The calculation of DP is often done by programmers, is the process of creation, the process of the classification. But the writing process will always have two processes, one is to write, one is to return to read, this reading is used to convert the DP.
(3), DP-px-(the most common method)
    • Already see an example of the following (note: Equals is equivalent, is equal to the understanding, not directly equal): Android If the phone is 160px per inch, that is, the phone is 160dpi, then 1DP can be understood as 1px, if the cell phone is 240dpi, then 1dp=240/ PX=1.5PX,
      From the above you can see that the DP-to PX is only related to the DPI of the screen.
    • For Android, ultimately we can read or measure the PX, so finally we will be DP into PX good understanding. I summed up the formula for DP to PX as follows:
    • Formula: DP value x (current dpi/160dpi) = = Number of current pixels
(4), mobile phone screen DP Maximum is how much?
  • So the question is, how much DP does a mobile screen have, can I set it to 100000DP? In fact, this is what I think, and I'm slowly speaking.
  • If the width of the control is now set to 160DP, what are the pixels of the different dpi phones?

    You can see that the resulting pixel value is the same as the DPI value, meaning that no matter what screen 160DP, it is equivalent to 1 inches.
  • So to get the longest full-screen DP, it is equal to 160* width (or high inch length)
    As the third 1080*1920 example, 160*3=480DP, so 0-480DP is a wide DP range. Because 480*360/160=1080 pixels, exactly equal to the maximum number of pixels wide.
  • 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.

    Update 20140701: Is the screen width of all Android phones a fixed value (such as 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.

    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 Practices:

    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:

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

    Finally, an Android UI design reference diagram is recommended: Android design Cheat Sheet

    Resources:

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

Differences in DP, DPI, PX in Android Development (RPM)

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.