Parsing dip,dp,px,sp and screen density in Android _android

Source: Internet
Author: User
1. Dip:device independent pixels (device independent pixel).Different devices have different display effects, this is related to the device hardware, we generally support the WVGA, HVGA and QVGA recommended to use this, do not rely on pixels.

Here to pay special attention to the dip and screen density, and screen density and specific hardware, hardware settings are incorrect, may cause dip can not be normal display. In the screen density of 160 of the display, 1dip=1px, sometimes your screen resolution is very large, such as 480*800, but the screen density is not set correctly, such as or 160, then all the use of DIP will show the exception, the basic is shown too small.
the conversion of dip:
Dip (value) = (int) (PX (value)/1.5 + 0.5)
2. DP: Very simple, and dip is the same.
3. Px:pixels (pixel),
Different devices different display effects are the same, this is absolute pixel, is how much will always be how much will not change.
4. sp:scaled pixels (magnified pixel).Mainly used for font display best for textsize.
Note: According to Google's recommendation, Pixel Unified use dip, Font unified use SP
For An example, differentiate between PX and dip:
PX is pixel, if using PX, will use the actual pixel painting, than a bar, drawing a length of 240px horizontal line, in the 480-wide simulator is half the screen width, and in the 320-wide simulator to see is 2/3 screen wide.
And dip, is the screen of the high divided into 480 points, wide divided into 320 points. For example, you make a 160dip horizontal line, whether you are in 320 or 480 of the simulator, is half the length of the screen.
Copy Code code as follows:

public static int dip2px (context context, float Dipvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Dipvalue * scale + 0.5f);
}

public static int Px2dip (context context, float Pxvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}

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.