Android explanation DP,PX,PT,SP Unit

Source: Internet
Author: User

1.DP (DIP): Different devices have different display effect, this is related to the device hardware, generally we support WVGA,HVGA and QVGA to the sword Use this, it is not dependent on pixels

2.px:pixels (pixels), different devices display the same effect, generally we hvga represent 320x480 pixels, this use more

3.pt:point, is a standard length unit, 1pt=1/72 inches, for printing, very easy to use

4.sp:scaled pixels (enlarged pixels), mainly used for font display best for TEXTSIZE

In the APK's resource bundle, use the hdpi tag when the screen density=240

When the screen desity=160, the resource using the MDPI tag

Resources that use ldoi tags when the screen is density=120

On a 160-point monitor, 1dp=1px

5. The following is the conversion of the different units in the focus:

1  Public Static intDIP2PX (Context context,floatdipvalue) {2 Final floatScale =context.getresources (). Getdisplaymetrics (). density;3 return(int) (Dipvalue * scale + 0.5f);4 }5  Public Static intPx2dip (Context context,floatpxvalue) {6 Final floatScale =Context.getresource (). Getdisplaymetrics (). density;7 return(int) (Pxvalue/scale + 0.5f);8 }9  Public Static intDIP2PX (Context context,floatdipvalue) {Ten Final floatScale =context.getresources (). Getdisplaymetrics (). density; One return(int) (Dipvalue * scale + 0.5f); A } -  Public Static intPx2dip (Context context,floatpxvalue) { - Final floatScale =Context.getresource (). Getdisplaymetrics (). density; the return(int) (Pxvalue/scale + 0.5f); -}

6. Here's how to get the resolution:

1     //In an activity's OnCreate method, write the following code:2Displaymetrics metric =Newdisplaymetrics ();3 Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metric);4         intwidth = metric.widthpixels;//screen width (pixels)5         intHeight = metric.heightpixels;//screen height (pixels)6         floatdensity = metric.density;//screen Density (0.75/1.0/1.5)7         intdensitydpi = metric.densitydpi;//screen density dpi (120/160/240)

The 7.Android supports all of the following units.
PX (pixels): The point on the screen.
In (inches): unit of length.
MM (mm): unit of length.
PT (lb): 1/72 inches.
DP (density-independent pixels): An abstract unit based on screen density. 1DP = 1px on a monitor 160 dots per inch.
Dip: Same as DP, more for android/ophone example.
SP (scale independent pixels): Similar to DP, but can be scaled based on user's font size preference

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.