The difference between PX, DP and SP, and conversion

Source: Internet
Author: User

1, px

That is, 1px represents a physical pixel point on the screen.

2, DP

Dip:device independent pixels (device independent pixels), DP and dip, different devices have different display effect, generally in order to support WVGA, HVGA and QVGA recommended use this, do not rely on pixels.

3. SP

Sp:scale-independent pixels (scale independent pixels), an abstract pixel that is independent of scaling, is similar to DP, but the only difference is that the Android system allows users to customize the size of text (small, normal, large, oversized, etc.), when the text size is "normal" 1sp=1dp=0.00625inch (inches), when the text size is "large" or "oversized", 1sp>1dp=0.00625inch

4, DPI

Dpi:dots per inch (dots or pixels per inch more understandable), determined by the device, is written dead and can be obtained by context.getresources (). Getdisplaymetrics (). densitydpi. Of course, the following methods can also be calculated:

dpi= transverse resolution/transverse inches = Portrait Resolution/Portrait inch number

List of screen densities defined by Google:

    • ldpi (Low) ~120dpi
    • mdpi (Medium) ~160dpi
    • hdpi (high) ~240dpi
    • xhdpi (Extra-high) ~320dpi
    • xxhdpi (Extra-extra-high) ~480dpi
    • xxxhdpi (Extra-extra-extra-high) ~640dpi

__________________________________________________________________________________

DP and PX

px=dp* (dpi/160)

dp=px/(dpi/160)

SP and px

px=sp* (dpi/160)

sp=px/(dpi/160)

_____________________________________________________________________________

Get the Displaymetrics object method:

Displaymetrics dm=New  displaymetrics (); // get Displaymetrics object method one // dm=context.getresources (). Getdisplaymetrics (); // get Displaymetrics object method two (Activity) context). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

The unit of measure gets the code:

 Public Static floatApplydimension (intUnitfloatvalue, Displaymetrics metrics) { Switch(unit) { Casecomplex_unit_px:returnvalue; CaseComplex_unit_dip:returnValue *metrics.density; Casecomplex_unit_sp:returnValue *metrics.scaleddensity; Casecomplex_unit_pt:returnValue * metrics.xdpi * (1.0f/72);  Casecomplex_unit_in:returnValue *metrics.xdpi; Casecomplex_unit_mm:returnValue * metrics.xdpi * (1.0f/25.4f); } return0; }

Reference from:

Supporting multiple Screens

An explanation of the differences between dip, DP, SP, PT and PX in Android

PX vs. DP, SP conversion formula?

[Android Quiz] px, DP, and SP, what are the differences between these units?

Get screen information with Displaymetrics

Android Visual spec-spacing specification and text spec unit conversion (DIP, SP, px)

The difference between PX, DP and SP, and conversion

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.