zoukankan      html  css  js  c++  java
  • DensityUtil

    /**
     *  屏幕的尺寸及不同单位之间的转换
     * Created by travis on 2016/4/18.
     */
    
    public class DensityUtil {
    
        /**
         * 屏幕的高度(px)
         * @param context
         * @return
         */
        public static final float getHeightInPx(Context context) {
            final float height = context.getResources().getDisplayMetrics().heightPixels;
            return height;
        }
    
        /**
         * 屏幕的宽度(px)
         * @param context
         * @return
         */
        public static final float getWidthInPx(Context context) {
            final float width = context.getResources().getDisplayMetrics().widthPixels;
            return width;
        }
    
        /**
         * 屏幕的高度(dp)
         * @param context
         * @return
         */
        public static final int getHeightInDp(Context context) {
            final float height = context.getResources().getDisplayMetrics().heightPixels;
            int heightInDp = px2dip(context, height);
            return heightInDp;
        }
    
        /**
         * 屏幕的宽度(dp)
         * @param context
         * @return
         */
        public static final int getWidthInDp(Context context) {
            final float height = context.getResources().getDisplayMetrics().heightPixels;
            int widthInDp = px2dip(context, height);
            return widthInDp;
        }
    
        /**
         * dp to px
         * @param context
         * @param dpValue
         * @return
         */
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        /**
         * px to dp
         * @param context
         * @param pxValue
         * @return
         */
        public static int px2dip(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / scale + 0.5f);
        }
    
        /**
         * px to sp
         * @param context
         * @param pxValue
         * @return
         */
        public static int px2sp(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / scale + 0.5f);
        }
    
        /**
         * sp to px
         * @param context
         * @param spValue
         * @return
         */
        public static int sp2px(Context context, float spValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (spValue * scale + 0.5f);
        }
    
    }
  • 相关阅读:
    L84
    L83
    T57
    T56
    Listen 82
    Listen81
    PyQt(Python+Qt)学习随笔:QListView的isWrapping属性
    PyQt(Python+Qt)学习随笔:QListView的movement属性
    PyQt(Python+Qt)学习随笔:QListView的gridSize属性
    第15.20节 PyQt(Python+Qt)入门学习:QColumnView的作用及开发中对应Model的使用
  • 原文地址:https://www.cnblogs.com/hsji/p/5147830.html
Copyright © 2011-2022 走看看