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

Source: Internet
Author: User

1. 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)
2. DP: Very simple, and dip is the same.
3. Px:pixels (pixels), different devices different display display effect is the same, this is absolute pixels, is how much will never change.
4. sp:scaled pixels (enlarge pixels). Mainly used for font display of best for textsize.

Note: According to Google's recommendation, pixels uniformly use the dip, the font unified use SPFor example, the difference between PX and dip:px pixels, if using PX, will be used to draw the actual pixel, than a bar, with a line length of 240px, in the 480 wide simulator is half the screen width, and in the 320 wide simulator is 2/3 of the screen width. Dip, that is, the height of the screen is divided into 480 points, the width 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.
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);
       }   We often set the length of the container when we are in the layout of the page, but it is annoying to use which unit is the length. There are several types of description size areas that are supported in Android. PX (pixels)--Pixels: Different devices display the same effect, generally we hvga represent 320x480 pixels, this use more. Dip (device independent pixels)--equipment independent pixels: This is related to device hardware, general OH we recommend this for support WCGA, HVGA, and QVGA, and do not depend on pixels. Equivalent to DP. SP (scaled pixels-best for text size)--proportional pixels. PT (points)--lb: 1pt = 1/72 inch in (inches)--inch mm (millimeters)-mm what is the difference between PX and dip? Dip! = px is mainly for different devices in the density is 160 device, such as the default simulator created with Avdmanager, dip and PX is the same but if the same code, switch to different density devices, For example, to a density is 240 of the device, dip and PX reflects the size is not the same PX no matter what kind of device is so long, but the dip will vary according to the device WQVGA screen density=120; QVGA screen density=120; HVGA screen density=160; WVGA screen density=240; The density value represents the number of display points per inch, and the resolution is two concepts. When the screen density=240 using the hdpi tag resource when the screen density=160, use the resources of the MDPI tag when the screen density=120, using the ldpi tag resource. Resources that are not tagged are shared in a variety of resolution scenarios. Use the unit dip as much as possible in the layout, and use less px conversion formula Pixs = dips * (densitydpi/160). Dips= (pixs*160)/densitydpi How to get the screen resolution and screen density of the device: Device displaymetrics DM =getresources () with a resolution of 480*800 and a density of 240. Getdisplaymetrics (); dm.densitydpi=240 dm.widthpixels=480 DM. heightpixels=800

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

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.