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);
        }
    
    }
  • 相关阅读:
    程序基址,X64Dbg软件常用调试技巧查找系统函数调用位置执行到指定位置断点
    #pragma的常用方法讲解,为什么有了条件编译符号“DEBUG”还要来个Debugger.IsAttached
    JDK17Src0.java.base
    nmon的安装和使用
    64位下的相对指令地址X86指令格式(操作码列和指令列解释)
    内存中的程序剖析
    Linux I/O 原理和 Zerocopy 技术全面揭秘
    Ubuntu命令行的垃圾箱,回收站
    SecureCRT密钥链接阿里云
    HTTP API 认证授权术
  • 原文地址:https://www.cnblogs.com/hsji/p/5147830.html
Copyright © 2011-2022 走看看