美文网首页移动开发安卓资源收集Android技术知识
Android字典(三)-- Android手机系统屏幕相关

Android字典(三)-- Android手机系统屏幕相关

作者: androidjp | 来源:发表于2016-09-22 13:15 被阅读74次

    一、Android屏幕知识

    Android项目中的drawable文件夹可分为(4K分辨率不普及、暂时不管):

    |drawable-ldpi | drawable-mdpi | drawable-hdpi | drawable-xhdpi | drawable-xxhdpi | drawable-xxxhdpi
    ---|---|---|---|---|---|----
    对应 | ldpi | mdpi | hdpi | xhdpi | xxhdpi | xxxhdpi【2K】
    屏幕DPI值 | 120 | 160 | 240 | 320 | 480 | 640
    屏幕分辨率 | 240x320 | 320x480 | 480x800 | 720x1280 | 1080x1920 | 1440x2560
    相对比例 | 3 | 4 | 6 | 8 | 12 | 16
    1dp等于 | 0.75px | 1px(基准) | 1.5px | 2px | 3px | 4px

    二、开发常用的屏幕DP和PX、SP和PX的相互转换工具类

    import android.app.Activity;
    import android.util.DisplayMetrics;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 屏幕显示相关工具类
     * (px ,sp, dp 等转换)
     * Created by JP on 2016/4/7.
     */
    public class DisplayUtil {
    
        private static Map<ScreenEnum,Integer> screenMap = new HashMap<>();
    
        /**
         * 将px值转换为dip或dp值,保证尺寸大小不变
         *
         * @param pxValue
         * @return
         */
        public static int px2dip(float pxValue, Activity activity) {
    
            return (int) (pxValue / getScreenMsg(activity).get(ScreenEnum.Density) + 0.5f);
        }
        /**
         * 将dip或dp值转换为px值,保证尺寸大小不变
         * @return
         */
        public static int dip2px(float dipValue, Activity activity) {
            return (int) (dipValue * getScreenMsg(activity).get(ScreenEnum.Density) + 0.5f);
        }
        /**
         * 将px值转换为sp值,保证文字大小不变
         *
         * @return
         */
        public static int px2sp(float pxValue, Activity activity) {
            return (int) (pxValue / getScreenMsg(activity).get(ScreenEnum.ScaledDensity) + 0.5f);
        }
        /**
         * 将sp值转换为px值,保证文字大小不变
         * @return
         */
        public static int sp2px(float spValue, Activity activity) {
            return (int) (spValue * getScreenMsg(activity).get(ScreenEnum.ScaledDensity) + 0.5f);
        }
        /**
         * 获取屏幕尺寸等信息
         */
        public static Map<ScreenEnum,Integer> getScreenMsg(Activity activity){
            DisplayMetrics metric = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            int width = metric.widthPixels;
            int height = metric.heightPixels;
            float density = metric.density;///屏幕密度(0.75, 1.0 . 1.5)
            int densityDpi = metric.densityDpi;///屏幕密度DPI(120/160/240/320/480)
            float scaledDensity = metric.scaledDensity;
            if (screenMap==null) screenMap = new HashMap<>();
    
            screenMap.clear();
            screenMap.put(ScreenEnum.Width,width);
            screenMap.put(ScreenEnum.Height,height);
            screenMap.put(ScreenEnum.Density,(int)density);
            screenMap.put(ScreenEnum.DendityDpi,densityDpi);
            screenMap.put(ScreenEnum.ScaledDensity, (int)scaledDensity);
            return screenMap;
        }
    
        enum ScreenEnum{
            Width,Height,Density,DendityDpi,ScaledDensity
        }
    }
    

    相关文章

      网友评论

        本文标题:Android字典(三)-- Android手机系统屏幕相关

        本文链接:https://www.haomeiwen.com/subject/prolettx.html