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);
        }
    
    }
  • 相关阅读:
    微服务下的持续集成-Jenkins自动化部署GitHub项目
    JDK新特性-Lambda表达式的神操作
    ActiveMQ详细入门教程系列(一)
    程序员必须了解的知识点——你搞懂mysql索引机制了吗?
    面试之后,扼腕叹息。
    哎,这让人抠脑壳的 LFU。
    延迟消息的五种实现方案
    Java实现Kafka生产者和消费者的示例
    Pytorch训练时显存分配过程探究
    Python命令行参数定义及注意事项
  • 原文地址:https://www.cnblogs.com/hsji/p/5147830.html
Copyright © 2011-2022 走看看