PX, DP, SP used in Android development

Source: Internet
Author: User

first introduce these units:

px : pixels (pixels), corresponding to the actual pixel point on the screen.

Dip :d evice independent pixels, density independent pixels, based on the abstract unit of screen density. On a 160-point-per-inch display,

1DP = 1px, which is 1:1 relationship. (DP is DIP)

sp : scaled pixels (enlarge pixels). Pixels independent of density and font scaling. Mainly used for font display of best for textsize.

Because the Android device screen clutter, a variety of pattern width and height of a lot of not, plus a variety of cottage. When a set of layout is developed, the painful thing comes and the screen fits.

How can I ensure that layout is displayed properly at different resolutions? Let's briefly analyze some of the most commonly used units in Android.


PX (pixels) is the unit used by the UI designer to cut graphs in PS, and the display unit is also available on the phone screen.

DP is the unit of dimension used when developing layout in Android.

The SP is the font size unit that is used when developing layout, and when you set the font unit to SP, the font is automatically stretched to fit with the screen size.


Android supports five different dpi modes: ldpi, MDPI, hdpi, xhdpi, xxhdpi

In general, the phone resolution is matched to the DPI mode that is running, that is, the different DPI folders under Res are loaded according to the size of the screen.

For example, the phone screen of HVGA (320x480 pixels) is generally around 3.5 inches, running in MDPI mode (with the exception of the wonderful machine)

(This is ROM-controlled and the app cannot be changed). When running under MDPI,1dp=1px: That is, the designer in PS defines an item height of 48px, development will define the item high 48dp;photoshop 14px Large font, development will be defined as 14SP.


For a WVGA (480x800) phone, it is generally run in hdpi mode. When running in hdpi mode,

1dp=1.5px: That is, the designer in the PS definition of an item high 72px, the development will define the item high 48DP;

21px large font in Photoshop, development is defined as 14SP.


So, when your app needs to fit in multiple DPI modes (for example, MDPI and hdpi), if you're designing (or higher) under WVGA, you need to multiply each number by 3,
All numbers (/3*2) are converted to DP when transduction labeled, so the same set of layout developed can be used in two different DPI modes instead of writing two sets of layout.

Proportion of these:
The relationship between MDPI and hdpi is 2:3.
The relationship between MDPI and xhdpi is 1:2.
The relationship between ldpi and MDPI is 3:4.
For example, my machine red rice, 720 * 1280 screen size, is running under the xhdpi.
One exception, some of the more cottage pad may be a 7-inch screen, the resolution of WVGA (480*800) run under MDPI.
Related Article

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.