The following is a tool class that provides a way to convert between DP, SP, and PX.
Import android.content.context;/** * DP, SP convert to PX tool class <br> * <a href= "Http://blog.csdn.net/ouyang_peng" > CSDN ouyangpeng</a> */public class Displayutil {/** * turns from px (pixel) units to DP */public static int Px2dip (CONTEXT) based on the resolution of the phone Context, float Pxvalue) {final float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (pxvalue/s Cale + 0.5f);} /** * Change from DP unit to PX (pixel) */public static int dip2px (context context, float dipvalue) According to the resolution of the phone {final float scale = Contex T.getresources (). Getdisplaymetrics (). Density;return (int) (Dipvalue * scale + 0.5f);} /** * Convert PX value to SP value, keep text size constant */public static int px2sp (context context, float Pxvalue) {final float Fontscale = context.getre Sources (). Getdisplaymetrics (). Scaleddensity;return (int) (Pxvalue/fontscale + 0.5f);} /** * Convert SP value to PX value, keep text size constant */public static int sp2px (context context, float Spvalue) {final float Fontscale = context.getre Sources (). Getdisplaymetrics (). Scaleddensity;return (int) (Spvalue * fontscale + 0.5f);}}
====================================================================================
Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng
====================================================================================
Copyright NOTICE: This article for Bo Master original article, welcome reprint, reprint please indicate source Http://blog.csdn.net/ouyang_peng
My Android Advanced tour------>android about DP (DIP), SP turn PX Tool class