美文网首页
今日头条屏幕适配

今日头条屏幕适配

作者: 小龍蛋 | 来源:发表于2019-06-25 18:28 被阅读0次

    参考:https://www.jianshu.com/p/4aa23d69d481

    原文地址:https://mp.weixin.qq.com/s/d9QCoBP6kV9VSWvVldVVwA
    public class DensityUtils {
        private static float sOriginalDensity;
        private static float sOriginalScaledDensity;
        private static int sOriginalDensityDpi;
        private static final int sTargetWidthDp = 360;
        private static float sCompatDensity;
        private static float sCompatScaledDensity;
    
        public static void init(Application application){
            final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
            sOriginalDensity = appDisplayMetrics.density;
            sOriginalScaledDensity = appDisplayMetrics.scaledDensity;
            sOriginalDensityDpi = appDisplayMetrics.densityDpi;
            application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    
                    if(activity instanceof CameraIDActivity || activity instanceof CameraIDHandHoldActivity){
                        setsOriginalDensity(activity,application);
                    }else{
                        setCustomDensity(activity,application);
                    }
                }
    
                @Override
                public void onActivityStarted(Activity activity) {
                }
    
                @Override
                public void onActivityResumed(Activity activity) {
                }
    
                @Override
                public void onActivityPaused(Activity activity) {
                }
    
                @Override
                public void onActivityStopped(Activity activity) {
                }
    
                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                }
    
                @Override
                public void onActivityDestroyed(Activity activity) {
                }
            });
        }
    
        /**
         * 今日头条适配
         * @param activity
         * @param application
         */
        private static void setCustomDensity(Activity activity, Application application) {
            final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
    
            if (sCompatDensity == 0) {
                sCompatDensity = appDisplayMetrics.density;
                sCompatScaledDensity = appDisplayMetrics.scaledDensity;
                application.registerComponentCallbacks(new ComponentCallbacks() {
                    @Override
                    public void onConfigurationChanged(Configuration newConfig) {
                        if (newConfig != null && newConfig.fontScale > 0) {
                            sCompatScaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
                        }
                    }
    
                    @Override
                    public void onLowMemory() {
                    }
                });
            }
            final float targetDensity = appDisplayMetrics.widthPixels / sTargetWidthDp;
            final float targetScaleDensity = targetDensity * (sCompatScaledDensity / sCompatDensity);
            final int targetDensityDpi = (int) (160 * targetDensity);
            appDisplayMetrics.density = targetDensity;
            appDisplayMetrics.scaledDensity = targetScaleDensity;
            appDisplayMetrics.densityDpi = targetDensityDpi;
    
    
            DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
            activityDisplayMetrics.density = targetDensity;
            activityDisplayMetrics.scaledDensity = targetScaleDensity;
            activityDisplayMetrics.densityDpi = targetDensityDpi;
        }
    
        /**
         * 还原原始Density
         * @param activity
         * @param application
         */
        public static void setsOriginalDensity(Activity activity, Application application) {
            final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
            appDisplayMetrics.density = sOriginalDensity;
            appDisplayMetrics.scaledDensity = sOriginalScaledDensity;
            appDisplayMetrics.densityDpi = sOriginalDensityDpi;
    
            DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
            activityDisplayMetrics.density = sOriginalDensity;
            activityDisplayMetrics.scaledDensity = sOriginalScaledDensity;
            activityDisplayMetrics.densityDpi = sOriginalDensityDpi;
        }
    }
    

    相关文章

      网友评论

          本文标题:今日头条屏幕适配

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