• 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);
        }
    
    }

  • 相关阅读:
    trie树模型
    计算机网络概念
    [luogu]1042乒乓球 (模拟)
    [IPUOJ10705]最大连通块 (dfs)
    IPUOJ10701 有障碍的八皇后
    【紫书学习笔记】
    纪念正式写博客的第一天
    Bzoj 1997 [Hnoi2010]Planar题解
    Bzoj 1925 [Sdoi2010]地精部落 题解
    Bzoj 2839 集合计数 题解
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7101861.html
Copyright © 2020-2023  润新知