Conversion and difference of DP, SP, and PX in Android.

Source: Internet
Author: User

In the APK Resource Package

When the screen density is 240, resources that use the hdpi label use the ldpi label when the screen density is 160 and the resources that use the mdpi label use the ldpi label when the screen density is 120. Resources without any tags are shared in various resolutions. Use the unit dip as much as possible during layout, and use less PX

 

Formula for converting DP to PX:

Pixs = dips * (densitydpi/160).

DIPS = (pixs * 160)/densitydpi

 

/**
* Convert the cell phone resolution from Dp to PX (pixel)
*/
Public static int dip2px (context, float dpvalue ){
Final float scale = context. getresources (). getdisplaymetrics (). density;
Return (INT) (dpvalue * scale + 0.5f );
}

 

DP and PX conversion methods:

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. getresource (). getdisplaymetrics (). density; Return (INT) (pxvalue/scale + 0.5f );}

 

 

Term

Explanation

PX

(pixel)

different devices have the same display effect. The "same" indicates that the number of pixels remains unchanged. For example, if the specified UI length is PX, the UI length is PX regardless of the resolution. It is precisely because of this that the UI is magnified and distorted on a small resolution device, and scaled down on a large resolution device.

screen size

(screen size)

generally, the screen size of a mobile phone is 1.6 inch, 1.9 inch, or 2.2 inch, it refers to the diagonal line length, rather than the cell phone area. We can obtain the width and length of the mobile phone based on the hook theorem. Of course, there is still an area.

resolution

(resolution)

indicates the number of pixels in the vertical and horizontal directions of the mobile phone screen. For example, if the resolution is 480*320, the device has 480 pixels vertically and 320 pixels horizontally.

DPI

(dots per inch

pixel density)

indicates the number of pixels per inch. For example, 160 DPI indicates that the distance from each inch on the mobile phone is pixels in the horizontal or vertical direction. Assume that the device resolution is 320*240, the screen length is 2 inch-1.5 inch-320/2, DPI = 240/1 =. 5 = 160

note: this value corresponds to the value of the densitydpi attribute in the displaymetrics class

density

(density)

indicates the number of pixels per square inch.

density = resolution/screen size

note: in the displaymetrics class, the density value is DPI/160, which can be used for conversion between PX and dip

dip

(device-independent pixel, device independent pixel)

same as DP, which can be used as the unit of length, different devices have different display effects . This is related to the hardware of the device. We recommend that you use this feature to support WVGA, hvga, and qvga, without relying on pixels. The corresponding formula of dip and specific pixel values is dip value = device density/160 * pixel value , which can be seen in DPI (pixel density) 1px = 1dip

sp

(scaledpixels

zoom in pixels)

Best for textsize ). According to Google's suggestion, it is best to use SP as the unit for the font size of textview, and check the source code of textview. Android uses SP as the font size unit by default.

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.