性能优化

【适配】Android的一种px适配方式

2018-09-12  本文已影响69人  dlihasa

原理上还是分析源码中的尺寸处理,不多说,直接上代码。

核心工具类

/**
 * 多屏幕适配工具
 * 不采用android系统的适配规则,采用自己的适配规则
 * 将drawable放入drawable-nohdpi中,距离和字体大小采用px来做
 * 标准是基于主流屏幕1920x1080
 * Created by dl on 2017/6/28.
 */
public class SupportMultiScreenUtil {

    public static final int BASE_SCREEN_WIDTH = 1080;
    public static final int BASE_SCREEN_HEIGHT = 1920;
    public static final float BASE_SCREEN_WIDTH_FLOAT = 1080F;
    public static final float BASE_SCREEN_HEIGHT_FLOAT = 1920F;
    public static float scale = 1.0F;

    public SupportMultiScreenUtil() {

    }

    public static void init(Context context) {
        Resources resources=context.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        int widthPixels = displayMetrics.widthPixels;
        scale = (float)widthPixels / BASE_SCREEN_WIDTH_FLOAT;
    }


    public static void scale(View view) {
        if(null != view) {
            if(view instanceof ViewGroup) {
                scaleViewGroup((ViewGroup)view);
            } else {
                scaleView(view);
            }
        }
    }

    private static void scaleView(View view) {
        Object isScale = view.getTag(R.id.is_scale_size_tag);
        if (!(isScale instanceof Boolean) || !((Boolean) isScale).booleanValue()) {
            if (view instanceof TextView) {
                scaleTextView((TextView) view);
            } else {
                scaleViewSize(view);
            }
            view.setTag(R.id.is_scale_size_tag, Boolean.valueOf(true));
        }
    }



    private static void scaleViewGroup(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); ++i) {
            View view = viewGroup.getChildAt(i);
            if (view instanceof ViewGroup) {
                scaleViewGroup((ViewGroup) view);
            }
            scaleView(view);
        }
    }

    public static void scaleViewSize(View view) {
        if (null != view) {
            int paddingLeft = getScaleValue(view.getPaddingLeft());
            int paddingTop = getScaleValue(view.getPaddingTop());
            int paddingRight = getScaleValue(view.getPaddingRight());
            int paddingBottom = getScaleValue(view.getPaddingBottom());
            view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (null != layoutParams) {
                if (layoutParams.width > 0) {
                    layoutParams.width = getScaleValue(layoutParams.width);
                }
                if (layoutParams.height > 0) {
                    layoutParams.height = getScaleValue(layoutParams.height);
                }
                if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
                    int topMargin = getScaleValue(marginLayoutParams.topMargin);
                    int leftMargin = getScaleValue(marginLayoutParams.leftMargin);
                    int bottomMargin = getScaleValue(marginLayoutParams.bottomMargin);
                    int rightMargin = getScaleValue(marginLayoutParams.rightMargin);
                    marginLayoutParams.topMargin = topMargin;
                    marginLayoutParams.leftMargin = leftMargin;
                    marginLayoutParams.bottomMargin = bottomMargin;
                    marginLayoutParams.rightMargin = rightMargin;
                }
            }
            view.setLayoutParams(layoutParams);
        }
    }

    private static void setTextViewCompoundDrawables(TextView textView, Drawable leftDrawable, Drawable topDrawable, Drawable rightDrawable, Drawable bottomDrawable) {
        if(null != leftDrawable) {
            scaleDrawableBounds(leftDrawable);
        }
        if(null != rightDrawable) {
            scaleDrawableBounds(rightDrawable);
        }
        if(null != topDrawable) {
            scaleDrawableBounds(topDrawable);
        }
        if(null != bottomDrawable) {
            scaleDrawableBounds(bottomDrawable);
        }
        textView.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
    }

    public static Drawable scaleDrawableBounds(Drawable drawable) {
        int right=getScaleValue(drawable.getIntrinsicWidth());
        int bottom=getScaleValue(drawable.getIntrinsicHeight());
        drawable.setBounds(0, 0, right, bottom);
        return drawable;
    }

    public static void scaleTextView(TextView textView) {
        if (null != textView) {
            scaleViewSize(textView);
            Object isScale = textView.getTag(R.id.is_scale_font_tag);
            if (!(isScale instanceof Boolean) || !((Boolean) isScale).booleanValue()) {
                float size = textView.getTextSize();
                size *= scale;
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
            }
            Drawable[] drawables = textView.getCompoundDrawables();
            Drawable leftDrawable = drawables[0];
            Drawable topDrawable = drawables[1];
            Drawable rightDrawable = drawables[2];
            Drawable bottomDrawable = drawables[3];
            setTextViewCompoundDrawables(textView, leftDrawable, topDrawable, rightDrawable, bottomDrawable);
            int compoundDrawablePadding = getScaleValue(textView.getCompoundDrawablePadding());
            textView.setCompoundDrawablePadding(compoundDrawablePadding);
        }
    }

    public static int getScaleValue(int value) {
        return value <= 4?value:(int) Math.ceil((double)(scale * (float)value));
    }

}

上述代码和头条团队的方式,都是在寻求一个适配所有机型尺寸的比例:

头条团队选择根据当前设备的尺寸和设计图所给出的尺寸dp来修改系统的density,从而得到根据设计图中的标注设置的尺寸和效果图(也就是标注图)是一样的。
这种方法修改了当前设备本应用中系统的density,所以不论是activity也好,还是在activity中的fragment、dialog、popupwindow都是根据修改后的density来进行dp转换px的。

上述代码则是根据当前设备的宽度和设计图的宽度得出一个缩放比例,每次动态的去测量所有的view的相关尺寸,并将代码中设置的设计图标注的尺寸根据缩放比例调整成适合本机的尺寸来达到和效果图(设计图)效果一致。
这种方法是每加载一个布局都需要去拿到本布局的view做缩放处理,不会影响全局的尺寸大小。

说一说上述代码的使用方法(主要就是三点)

0、首先,将drawable放入drawable-nohdpi中,距离和字体大小采用px来做

1、在application中初始化工具类

SupportMultiScreenUtil.init(context);//在application中调用,进行工具初始化

2、在加载布局的地方使用如下代码:

SupportMultiScreenUtil.scale(rootView);//每个布局view都要这样做才会去根据当前设备对设计图中的尺寸进行缩放
上一篇下一篇

猜你喜欢

热点阅读