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