美文网首页
Android工具类之 dip px sp相互转换

Android工具类之 dip px sp相互转换

作者: 我是你森哥哥 | 来源:发表于2017-06-25 23:38 被阅读0次

    public class DensityUtil {
        public static final float getHeightInPx(Context context) {
            final float height = context.getResources().getDisplayMetrics().heightPixels;
            return height;
        }
    
        public static final float getWidthInPx(Context context) {
            final float width = context.getResources().getDisplayMetrics().widthPixels;
            return width;
        }
    
        public static final int getHeightInDp(Context context) {
            final float height = context.getResources().getDisplayMetrics().heightPixels;
            int heightInDp = px2dip(context, height);
            return heightInDp;
        }
    
        public static final int getWidthInDp(Context context) {
            final float height = context.getResources().getDisplayMetrics().heightPixels;
            int widthInDp = px2dip(context, height);
            return widthInDp;
        }
    
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        public static int px2dip(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / scale + 0.5f);
        }
    
        public static int px2sp(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / scale + 0.5f);
        }
    
        public static int sp2px(Context context, float spValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (spValue * scale + 0.5f);
        }
    
    

    相关文章

      网友评论

          本文标题:Android工具类之 dip px sp相互转换

          本文链接:https://www.haomeiwen.com/subject/xuqdcxtx.html