Comparison of pixel units DP, PX, PT, SP in Android

Source: Internet
Author: User

DP (DIP): device independent pixels (unit independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels.

Px:pixels (pixels). Different devices show the same effect, generally we hvga represent 320x480 pixels, this use more.

Pt:point, is a standard length unit, 1pt=1/72 inches, for the printing industry, very simple to use;

sp:scaled pixels (enlarge pixels). Mainly used for font display of best for textsize.

Thus, according to Google's suggestion, TextView's font size is best to use SP to do units, and view the source of TextView know that Android by default using the SP as the font size units.

Also, the Google code inside the units used in the PX as the default unit.


APK resource Bundle, when the screen density=240 use the hdpi tag resource

Resources that use MDPI tags when the screen is density=160

When the screen density=120, use the ldpi tag resource.

1DP = 1px on a monitor 160 dots per inch.


Here are several different units that convert each other.


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.getresource (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}
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.getresource (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}

Here's how to get the resolution:

In an activity's OnCreate method, write the following code:
Displaymetrics metric = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);
int width = metric.widthpixels; Screen width (pixels)
int height = metric.heightpixels; Screen height (pixels)
float density = metric.density; Screen Density (0.75/1.0/1.5)
int densitydpi = metric.densitydpi; Screen density dpi (120/160/240)



In Android, 1pt is roughly equal to 2.22SP.

above for reference, if the UI can provide the best design in SP, and if there is no concept of SP in the design, the developer can also take approximate values by appropriate conversion.

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 the user's font size preference.


This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1707665

Comparison of pixel units DP, PX, PT, SP in 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.