public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素). * * @param dpValue dpValue * @return px */ public static int dp2px(float dpValue) { return (int) Math.ceil(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, dpValue, Resources.getSystem().getDisplayMetrics())); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp. * * @param pxValue pxValue * @return dp */ public static int px2dp(float pxValue) { return (int) Math.ceil(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pxValue, Resources.getSystem().getDisplayMetrics())); } /** * sp转像素 . * * @param spValue . * @return . */ public static int sp2px(float spValue) { final float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } }
dp和px的转化(dp2px,px2dp)
最新推荐文章于 2024-07-12 16:26:30 发布