Convert between DP and PX in Android

Source: Internet
Author: User

In the XML layout file, we can either set the PX or set the DP (or DIP). In general, we will choose to use DP, which will ensure that the layout of different screen resolutions on the machine is consistent. But in the code, how to deal with it? Many of the methods in the control provide only a method for setting PX, such as setpadding, and do not provide a way to set the DP. At this point, if you need to set the DP, it is necessary to convert the DP into PX. The following is an application class that facilitates the conversion between PX and DP.

Description

Dip:device independent pixels (device 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. It is important to note that dip is related to screen density and that the screen density is related to specific hardware and that the hardware settings are incorrect and may cause the dip to not display properly. On the screen density of 160 display, 1dip=1px, sometimes your screen resolution is very large, such as 480*800, but the screen density is not set correctly for example or 160, then all the use of dip will show the anomaly, the basic display is too small. Dip Conversion: dip (value) = (int) (PX (value)/1.5 + 0.5) DP: Very simple, and dip is the same. Px:pixels (pixels), different devices different display display effect is the same, this is absolute pixels, is how much will never change. sp:scaled pixels (enlarge pixels). Mainly used for font display of best for textsize.

"' Javaimport android.content.Context;

public class Densityutil {

/**  * 根据手机的分辨率从 dp 的单位 转成为 px(像素)  */  public static int dip2px(Context context, float dpValue) {      final float scale = context.getResources().getDisplayMetrics().density;      return (int) (dpValue * scale + 0.5f);  }  /**  * 根据手机的分辨率从 px(像素) 的单位 转成为 dp  */  public static int px2dip(Context context, float pxValue) {      final float scale = context.getResources().getDisplayMetrics().density;      return (int) (pxValue / scale + 0.5f);  }  

}
```

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.