1.LDPI-----240x320-----Density 120
MDPI-----320x480-----Density 160
HDPI-----480x800-----Density 240
XHDPI-----720x1280-----Density 320
XXHDPI-----1080x1920-----Density 480
2.MDPI-----1dp=1px
HDPI-----1dp=1.5px
XHDPI-----1dp=2px
XXHDPI-----1dp=3px
Ldpi:mdpi:hdpi:xhdpi:xxhdpi=3:4:6:8:12
3. Unit conversion Tool Class
Packagecom.zzw.Qunyinzghuan3;ImportAndroid.content.Context;/*** Created by ZZW on 2016/7/12. * Description:*/ Public classDisplayutil {/*** Replace PX with DPI or DP value to ensure size is unchanged * *@paramContext *@paramPxvalue *@return */ Public Static intPx2dip (Context context,floatpxvalue) { Final floatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Pxvalue/scale + 0.5f); } /*** Change the dip to PX to ensure the size is unchanged * *@paramContext *@paramDipvalue *@return */ Public Static intDIP2PX (Context context,floatdipvalue) { Final floatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Dipvalue * scale + 0.5f); } /*** Replace the PX value with the SP value to ensure the text is not changed * *@paramContext *@paramPxvalue *@return */ Public Static intPX2SP (Context context,floatpxvalue) { Final floatFontscale =context.getresources (). Getdisplaymetrics (). scaleddensity; return(int) (Pxvalue/fontscale + 0.5f); } /*** Replace the SP value with the PX value to ensure the text is not changed * *@paramContext *@paramSpvalue *@return */ Public Static intSP2PX (Context context,floatspvalue) { Final floatFontscale =context.getresources (). Getdisplaymetrics (). scaleddensity; return(int) (Spvalue * Fontscale + 0.5f); }}Displayutil
The system provides a method for converting to PX:
/*** Replace the SP value with the PX value to ensure the text is not changed * *@paramContext *@paramSpvalue *@return */ Public Static intSP2PX (Context context,floatspvalue) { return(int) typedvalue.applydimension (typedvalue.complex_unit_sp, Spvalue, Context.getresources (). GetDisplayMetrics ()); } /*** Change the dip to PX to ensure the size is unchanged * *@paramContext *@paramDipvalue *@return */ Public Static intDIP2PX (Context context,floatdipvalue) { return(int) typedvalue.applydimension (Typedvalue.complex_unit_dip, Dipvalue, Context.getresources (). GetDisplayMetrics ( )); }system2px
Android phone size related p107-p110