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);
        }
    
    }
  • 相关阅读:
    C# SendKeys用法
    Winform的高DPI问题
    CefSharp在高DPI的屏幕上出现黑边(winform)
    CefSharp支持flash
    CeSharp支持MP4
    C#加密解密总览
    Eclipse 调试Bug之使用断点的七大技巧
    详解Eclipse断点
    怎样编写高质量的java代码
    Quartz任务调度基本使用
  • 原文地址:https://www.cnblogs.com/hsji/p/5147830.html
Copyright © 2011-2022 走看看