Dip, DP, PX, SP, and screen density in Android

Source: Internet
Author: User

1. dip: Device Independent pixels (device independent pixel ). different devices have different display effects. This is related to the hardware of the device. We recommend that you use this function to support WVGA, hvga, and qvga without pixels.
Note that the dip is related to the screen density, and the screen density is related to the specific hardware. Incorrect hardware settings may cause the dip to fail to be properly displayed. On a display with a screen density of 160, 1dip = 1px, sometimes your screen resolution may be large, such as 480*800, but the screen density is not properly set, for example, 160, when dip is used, an exception is displayed, which is usually too small.
Dip conversion:
Dip (value) = (INT) (PX (value)/1.5 + 0.5)
2. DP: Very simple. It is the same as dip.
3. Px: pixels (pixels). Different devices have the same display effect on different display screens. This is an absolute pixel, and the value will never change.
4. SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in the font.

Note: according to Google's recommendation, dip is used for pixels and SP is used for fonts.

For example, the difference between PX and dip is as follows:

PX is a pixel. If PX is used, it will be drawn using the actual pixel. For example, draw a horizontal line with a length of PX, in a 480-width simulator, the screen width is half the screen width, and in a 320-width simulator, the screen width is 2/3.

DIP is to divide the screen's high score into 480 points and the width into 320 points. For example, if you build a 320 dip horizontal line, whether you are on a 480 or simulator, it is half the screen length.

Public static int dip2px (context, float dipvalue ){
Final float scale = context. getresources (). getdisplaymetrics (). density;
Return (INT) (dipvalue * scale + 0.5f );
}
Public static int px2dip (context, float pxvalue ){
Final float scale = context. getresources (). getdisplaymetrics (). density;
Return (INT) (pxvalue/scale + 0.5f );
}

Category: Android

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.