Android工具类--常用单位转换类

来源:互联网 发布:牛贝微信淘宝客安装 编辑:程序博客网 时间:2024/05/18 17:42

常用单位的转换

package com.duanlian;import android.content.Context;import android.util.TypedValue;/** * 常用单位转换的辅助类 */public class DensityUtils{private DensityUtils(){/* cannot be instantiated */throw new UnsupportedOperationException("cannot be instantiated");}/** * dp转px */public static int dp2px(Context context, float dpVal){return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal, context.getResources().getDisplayMetrics());}/** * sp转px */public static int sp2px(Context context, float spVal){return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal, context.getResources().getDisplayMetrics());}/** * px转dp */public static float px2dp(Context context, float pxVal){final float scale = context.getResources().getDisplayMetrics().density;return (pxVal / scale);}/** * px转sp */public static float px2sp(Context context, float pxVal){return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);}}


0 0
原创粉丝点击