Android display units DP, dip, PX, SP

Source: Internet
Author: User

DP==DIP: device independent pixels. (device independent pixels)

Dip is an aged DP, retired, and can be seen in older code. At present, Google recommends DP, in our development of the Android program, in addition to the font, are pushed (strong) recommended (System) use DP.

  

SP: proportional pixels. (scaled pixels)

SP is generally used to handle fonts, Android has a thing called the big font, that thing is specifically tuned sp. So all about the font units, push (strong) Recommended (System) use SP.

PX: pixels (pixels)

PX, the physical point above the mobile phone, is a physical category, so it is not recommended when writing an Android program.

Android phone density: Low density (120dpi), medium density (160dpi), high density (240dpi), ultra high Density (320dpi), ultra high Density (480dpi)

According to Google's coding specifications: LDPI, MDPI, hdpi, xhdpi, xxhdpi

This does not take into account the size of the screen, because the same density of mobile phones, the size may be much different, then you will require the chart according to the size of the phone to varying degrees of scaling and stretching to fit the screen size. This is why we push (strong) recommend the use of DP, DP will be based on the density of the cell phone to fit the size, which ensures the same density of mobile phones, only need a photo.

  

Calculate the density of the phone: density =√ (length 2 + width 2)/diagonal length where the diagonal length is counted in inches if equal to 164 equals 160.

  

At 160dpi, i.e. mdpi, 1DP = = 1px ==1sp; (General DP does not compare with SP, it is equal only at 160dpi)

At 320dpi, i.e. xhpdi, 1DP = = 2px

At 480dpi, i.e. xxhpdi, 1DP = = 3px

We can know 1DP = (current dpi/160dpi) px.

In reality, generally as a developer, you can find UX or UI Yaotu when you need to calculate a few times, otherwise it is easy to cause misunderstanding.

In the code, however, you can use the following code to convert the DP px, as well as the PX and SP conversions (note that it is not SP and DP because it is meaningless).

  

[Java] View plaincopy
Import Android.content.Context;
/** * DP, SP conversion to PX Tool class * * @author Fxsky 2012.11.12 * */public class Displayutil { /** * Convert PX value to dip or DP value, ensure size unchanged * * @param pxvalue * @param scale * (Attribute density in Displaymetrics Class) * @return */public static int Px2dip (context context, Floa T pxvalue) {final float scale = context.getresources (). Getdisplaymetrics (). density; return (int) (Pxvalue/scale + 0.5f); /** * Convert dip or DP value to PX value, ensure size unchanged * * @param dipvalue * @param scale * (attribute density in Displaymetrics Class) * @return * * public static int dip2px (context Co ntext, float dipvalue) {final float scale = context.getresources (). Getdisplaymetrics (). density; return (int) (Dipvalue * scale + 0.5f); }/** * Converts PX value to SP value, keeping text size unchanged * * @param pxvalue * @param fontscale * (Attribute scaleddensity in Displaymetrics Class) * @return */public static int PX2SP (context context, float Pxvalue) {final float FONTSC Ale = Context.getresources (). Getdisplaymetrics (). scaleddensity; return (int) (Pxvalue/fontscale + 0.5f); /** * Convert SP value to PX value, keep text size unchanged * * @param spvalue * @param fontscale * (Attribute scaleddensity in Displaymetrics Class) * @return * * public static int sp2px (Contex T context, float Spvalue) {final float Fontscale = context.getresources (). Getdisplaymetrics (). scaleddensity; return (int) (Spvalue * fontscale + 0.5f); } }

  

Android display units DP, dip, PX, SP

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.