美文网首页
一种极低成本的Android屏幕适配方式

一种极低成本的Android屏幕适配方式

作者: 随心随性_0a25 | 来源:发表于2018-06-25 15:11 被阅读0次

    https://github.com/feisher/ScreenAdapter

    https://github.com/yatoooon/AndroidScreenAdaptation

    https://github.com/Firedamp/Rudeness

    来自 https://mp.weixin.qq.com/s/d9QCoBP6kV9VSWvVldVVwA

    private static float sNonCompatDensity;

    private static  float sNonCompatScaleDensity;

    private static void setCustomDensity(Activity activity,final Application application) {

    final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();

    if (sNonCompatDensity ==0) {

    sNonCompatDensity = appDisplayMetrics.density;

    sNonCompatScaleDensity = appDisplayMetrics.scaledDensity;

    application.registerComponentCallbacks(new ComponentCallbacks() {

    @Override

                    public void onConfigurationChanged(Configuration newConfig) {

    sNonCompatScaleDensity =application.getResources().getDisplayMetrics().scaledDensity;

    }

    @Override

                    public void onLowMemory() {

    }

    });

    }

    //屏幕宽 的像素除以360 获得Density的值

            final float targetDensity = (float)appDisplayMetrics.widthPixels /380;

    final int targetDensityDpi = (int) (targetDensity *160);

    final float targetScaledDensity = (int) (targetDensity * (sNonCompatScaleDensity /sNonCompatDensity));

    appDisplayMetrics.density = targetDensity;

    appDisplayMetrics.scaledDensity = targetScaledDensity;

    appDisplayMetrics.densityDpi = targetDensityDpi;

    //这里把scaledDensity的值 等于获得Density的值(会有bug 下面会解决)

            final DisplayMetrics activityDisplayMetrics = application.getResources().getDisplayMetrics();

    activityDisplayMetrics.density = targetDensity;

    activityDisplayMetrics.scaledDensity = targetScaledDensity;

    activityDisplayMetrics.densityDpi = targetDensityDpi;

    }

    相关文章

      网友评论

          本文标题:一种极低成本的Android屏幕适配方式

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