zoukankan      html  css  js  c++  java
  • PixelUtils:像素转换工具

    /** 像素转换工具
     */
    public class PixelUtils {
    
        /**
         * The context.
         */
        private static Context mContext = CustomApplcation.getInstance();
    
        /**
         * dp转 px.
         *
         * @param value the value
         * @return the int
         */
        public static int dp2px(float value) {
            final float scale = mContext.getResources().getDisplayMetrics().densityDpi;
            return (int) (value * (scale / 160) + 0.5f);
        }
    
        /**
         * dp转 px.
         *
         * @param value   the value
         * @param context the context
         * @return the int
         */
        public static int dp2px(float value, Context context) {
            final float scale = context.getResources().getDisplayMetrics().densityDpi;
            return (int) (value * (scale / 160) + 0.5f);
        }
    
        /**
         * px转dp.
         *
         * @param value the value
         * @return the int
         */
        public static int px2dp(float value) {
            final float scale = mContext.getResources().getDisplayMetrics().densityDpi;
            return (int) ((value * 160) / scale + 0.5f);
        }
    
        /**
         * px转dp.
         *
         * @param value   the value
         * @param context the context
         * @return the int
         */
        public static int px2dp(float value, Context context) {
            final float scale = context.getResources().getDisplayMetrics().densityDpi;
            return (int) ((value * 160) / scale + 0.5f);
        }
    
        /**
         * sp转px.
         *
         * @param value the value
         * @return the int
         */
        public static int sp2px(float value) {
            Resources r;
            if (mContext == null) {
                r = Resources.getSystem();
            } else {
                r = mContext.getResources();
            }
            float spvalue = value * r.getDisplayMetrics().scaledDensity;
            return (int) (spvalue + 0.5f);
        }
    
        /**
         * sp转px.
         *
         * @param value   the value
         * @param context the context
         * @return the int
         */
        public static int sp2px(float value, Context context) {
            Resources r;
            if (context == null) {
                r = Resources.getSystem();
            } else {
                r = context.getResources();
            }
            float spvalue = value * r.getDisplayMetrics().scaledDensity;
            return (int) (spvalue + 0.5f);
        }
    
        /**
         * px转sp.
         *
         * @param value the value
         * @return the int
         */
        public static int px2sp(float value) {
            final float scale = mContext.getResources().getDisplayMetrics().scaledDensity;
            return (int) (value / scale + 0.5f);
        }
    
        /**
         * px转sp.
         *
         * @param value   the value
         * @param context the context
         * @return the int
         */
        public static int px2sp(float value, Context context) {
            final float scale = context.getResources().getDisplayMetrics().scaledDensity;
            return (int) (value / scale + 0.5f);
        }
    
    }

  • 相关阅读:
    第8.13节 Python类中内置方法__repr__详解
    Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?
    Python中使用eval执行下面函数的结果怎么是字符串'10020'?
    第8.12节 Python类中使用__dict__定义实例变量和方法
    ThinkPHP---thinkphp拓展之空操作
    ThinkPHP---TP功能类之邮件
    ThinkPHP---案例--实现知识管理功能
    ThinkPHP---TP功能类之公文管理功能2----------继续完善
    ThinkPHP---TP拓展之获取IP信息
    ThinkPHP---layer插件
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7101861.html
Copyright © 2011-2022 走看看