Android pixel differences and comparisons between units DP, SP, PX, PT

Source: Internet
Author: User

directory Structure

    • Directory structure
    • Objective
    • Unit of Measure meaning
    • Conversion rules

Preface

In the past, programmers usually design a computer user interface in pixels. For example, the picture size is 80x32 pixels. The problem with this approach is that if you run the program on a new monitor with a higher pixel count per inch, the user interface icon will appear small. In some extreme cases, the user interface is too small to see the content. Because we should use resolution-independent units of measure in the development of Android applications.

Android supports various units of measurement such as DP, SP, PX, PT, and the following are some of their concepts and differences.

Unit of measure meaning
Dip (DP)
device independent pixels (unit independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA,HVGA and QVGA recommended use this, do not rely on pixels.
Px
pixels (pixels) different devices display the same effect.
Sp
scaled pixels (enlarged pixels) is primarily used for font display (best for TEXTSIZE).
Inch
inches, units of length.
Pt
Point
, which is a standard length unit, 1pt = 1/72 inches, for printing.
Conversion Rules

The conversion rules here are mainly about PX, DP, SP conversion rules.
From the perspective of UI designers and development engineers:

PX (pixels) is used by the UI designer in Photoshop and is also displayed on the phone screen.
DP is the size unit used by the development engineer when writing layout, the SP is the font size unit for writing layout, and the DP and SP are always 1:1 relationships.

So, when the UI designer gives, the markup unit is a DP or SP, and the development engineer can write the layout XML file directly. However, if the UI designer is given a pixel (px), the hard-pressed development engineer will have to convert it himself.

Before I know the specific conversion rules, let me introduce a concept, pixel density.
If you have a phone with a physical size of 1.5 inches * 2 inches and a screen resolution of 240*320, 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, that is, the number of pixels. This value is the same for both landscape and portrait, because most phone screens use a square pixel point.

Because different cell phones or tablets have different pixel densities, and developers are unlikely to develop the same application for each cell phone with different pixel densities, Android itself is thinking about the six pixel densities that the Android system itself defines:

Low (120DPI), Medium (160DPI), High (240DPI), ultra-high (320DPI), ultra-high (480DPI)

Because of the 160dpi pixel density: px = DP, when at other pixel densities, the PXE and DP conversion rules are: d P=(INT)(Px/1.5+0.5)
Because, in the low, medium, high, ultra-high, ultra-high pixel density, the conversion coefficients of DP to PX are:

Low (0.75), Medium (1), High (1.5), ultra-high (2), ultra-high (3)

With this conversion factor, assuming that the UI designer gives you a 200px pixel width on the ultra-high pixel density (XH) phone, the DP you write in the XML should be:100DP.
The code conversion methods for DP and PX are as follows:

 Public Static int dptopx(Context context,floatDpvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Dpvalue * scale +0.5f);} Public Static int PXTODP(Context context,floatPxvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Pxvalue/scale +0.5f);}

Android pixel differences and comparisons between units DP, SP, PX, PT

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.