zoukankan      html  css  js  c++  java
  • Density

    package com.k1.frame.utils;
    
    import android.content.Context;
    import android.util.DisplayMetrics;
    
    public class Density {
    
        public static final float DEFAULT_SCALE = 2;
    
        private static float scale = 1.0f;
        private static float fontScale = 1.0f;
        private static int statusBarHeight = 40;
        private static int screenWidth = 720;
        private static int screenHeight = 1280;
    
        public static void init (Context context) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            scale = dm.density;
            fontScale = dm.scaledDensity;
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels;
            statusBarHeight = getStatusBarHeight(context);
        }
    
        private static int getStatusBarHeight(Context context) {
            int result = 0;
            int resourceId = context.getResources().getIdentifier(
                    "status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId);
            }
            return result;
        }
        
        public static int statusBarHeight() {
            return statusBarHeight;
        }
    
        public static int screenWidth() {
            return screenWidth;
        }
        
        public static int screenHeight() {
            return screenHeight;
        }
    
        public static int dip2px(float dpValue) {
            return (int) (dpValue * scale + 0.5f);
        }
    
        public static int px2dip(float pxValue) {
            return (int) (pxValue / scale + 0.5f);
        }
    
        public static int px2sp(float pxValue) {
            return (int) (pxValue / fontScale + 0.5f);
        }
    
        public static int sp2px(float spValue) {
            return (int) (spValue * fontScale + 0.5f);
        }
    }
  • 相关阅读:
    第二冲刺阶段个人博客
    单元测试
    软件工程个人总结
    梦断代码阅读笔记03
    梦断编码阅读笔记02
    梦断代码阅读笔记01
    padding margin
    MVC模式在Java Web应用程序中的实例分析
    小问题
    MVC理解
  • 原文地址:https://www.cnblogs.com/g-sheng/p/5406520.html
Copyright © 2011-2022 走看看