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);
        }
    
    }
  • 相关阅读:
    selenium笔记
    gbase笔记
    oracle向gbase数据迁移须知
    字符串表达式的计算
    Struts2的安装
    JSP+MySQL中文乱码
    HTML:<input type="text">文本框不可编辑的方式
    HTML中使用<input>添加的按钮打开一个链接
    AS3.0:给图片添加滤镜模糊与斜角效果
    SQLServer 2008以上误操作数据库恢复方法—日志尾部备份
  • 原文地址:https://www.cnblogs.com/hsji/p/5147830.html
Copyright © 2011-2022 走看看