工具类-dp转化

来源:互联网 发布:磁力在线播放 源码 编辑:程序博客网 时间:2024/06/08 07:57
public class DensityUtils {     // dp2px     public static int dp2px(Context context, float dp) {           float density = context.getResources().getDisplayMetrics().density ;           // +0.5f为了四舍五入           return (int ) (dp * density + 0.5f);     }     // px2dp     public static float px2dp(Context context, int px) {           float density = context.getResources().getDisplayMetrics().density ;           // +0.5f为了四舍五入           return px / density;     }     // dp2px     protected int dp2px(int dpVal, Context context) {           return (int ) TypedValue.applyDimension(TypedValue. COMPLEX_UNIT_DIP,                   dpVal, context.getResources().getDisplayMetrics());     }     // sp2px     protected int sp2px(int spVal, Context context) {           return (int) TypedValue.applyDimension(TypedValue. COMPLEX_UNIT_SP ,                   spVal, context.getResources().getDisplayMetrics());     }}
原创粉丝点击