Android 布局性能 FrameLayout

Read The Fucking Source Code

引言

选择布局肯定要考虑性能的优略对比。

源码版本(Android Q — API 29)

1 FrameLayout onMeasure

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();

        //只要FrameLayout的布局中存在wrap_content属性(只有它不是EXACTLY模式)
        //那么就要对设置了match_parent的子View,可能进行第二次测量。至于为什么这样?我陷入了沉思。
        final boolean measureMatchParentChildren =
                MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
                MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
        mMatchParentChildren.clear();

        int maxHeight = 0;
        int maxWidth = 0;
        int childState = 0;

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            //mMeasureAllChildren xml可配置项,默认为false。主要是作用是:是否对GONE的子View进行测量。
            //所以,这个逻辑就变为了遍历每一个不为GONE的子view
            if (mMeasureAllChildren || child.getVisibility() != GONE) {
                //ViewGroup中的方法,去掉FrameLayout的左右padding和子View的左右margin,去计算出子View的测量宽度
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                //找到并保存子View中最大的宽/高,为什么呢?这就是FrameLayout的特性。
                //当FrameLayout的属性是wrap_content时后,他的宽高受限于子View的最大宽高。
                //大家都知道带有wrap_content属性(AT_MOST)的视图,后续不进行的测量约束(onMeasure重定义),默认是match_parent效果。
                //举例:wrap_content的FrameLayout包含一个wrap_content的View,那么FrameLayout就是满屏大小。
                //举例:wrap_content的FrameLayout包含了一个200宽高的View,那么FrameLayout也是200宽高。
                maxWidth = Math.max(maxWidth,
                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                //子视图的策略状态合并到FrameLayout的策略状态。
                childState = combineMeasuredStates(childState, child.getMeasuredState());
                //对有MATCH_PARENT的子View,进行缓存保存。
                if (measureMatchParentChildren) {
                    if (lp.width == LayoutParams.MATCH_PARENT ||
                            lp.height == LayoutParams.MATCH_PARENT) {
                        mMatchParentChildren.add(child);
                    }
                }
            }
        }

        // 子视图的最大宽高要加上panding(要考虑前景的配置等)信息,才是FrameLayout的宽高。
        maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
        maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();

        // 检查我们的最小高度和宽度,取最大的
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

        // 检查前景的最小高度和宽度,存在则对比,取最大的
        final Drawable drawable = getForeground();
        if (drawable != null) {
            maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
            maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
        }

        //设置FrameLayout的测量宽高,遵循什么原则呢?
        //如果FrameLayout不是wrap_content,那么他的大小就是设置的大小(比如200),或者满屏(充满父容器)大小(match_parent)。
        //如果FrameLayout是wrap_content,那么如果满屏(父容器)宽/高大于等于最大的子视图宽/高,则设置为最大的子视图宽高,否则设置为满屏(父容器)宽/高。
        //总之,一句话,取maxWidth(遍历子视图,以及检查父视图的配置,想要的最大的宽度)和widthMeasureSpec(允许的最大父视图的宽度)最小的那个。
        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));

        //子View中设置了match_parent属性的个数
        count = mMatchParentChildren.size();
        //当有match_parent的子View的个数大于1。至于为什么这样?我再次陷入了沉思。
        if (count > 1) {
            //遍历每个子视图,对于match_parent的属性,获取MeasureSpec的属性区别对待。个人感觉没啥区别,我又再次陷入沉思。。。
            for (int i = 0; i < count; i++) {
                final View child = mMatchParentChildren.get(i);
                final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

                final int childWidthMeasureSpec;
                if (lp.width == LayoutParams.MATCH_PARENT) {
                    final int width = Math.max(0, getMeasuredWidth()
                            - getPaddingLeftWithForeground() - getPaddingRightWithForeground()
                            - lp.leftMargin - lp.rightMargin);
                    childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                            width, MeasureSpec.EXACTLY);
                } else {
                    childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                            getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
                            lp.leftMargin + lp.rightMargin,
                            lp.width);
                }

                final int childHeightMeasureSpec;
                if (lp.height == LayoutParams.MATCH_PARENT) {
                    final int height = Math.max(0, getMeasuredHeight()
                            - getPaddingTopWithForeground() - getPaddingBottomWithForeground()
                            - lp.topMargin - lp.bottomMargin);
                    childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                            height, MeasureSpec.EXACTLY);
                } else {
                    childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                            getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
                            lp.topMargin + lp.bottomMargin,
                            lp.height);
                }

                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }
    }
复制代码

2 汇总小结

  • FrameLayout 的布局测量简单高效(不考虑特殊场景),只测量一遍。
  • FrameLayout 在特殊场景下(当FrameLayout根布局存在 wrap_content,且子 view(有 match_parent 属性)的个数大于一个)需要测量两遍。
  • FrameLayout 中特有的 foreground 属性功能,可以实现点击状态的前景色,也可以像 DecorView 中的windowContentOverlay 设置的闪屏页。

3 问题思考

Framelayout 简单高效的布局性能特点,被广泛应用。

  • 比如:DecorView的content跟布局就是FrameLayout。
  • FrameLayout 如何避免测量两次?

 使用FrameLayout时,尽量不要满足测量两次的条件。子View如果要满屏(填充父容器),尽量使用wrap_content属性代替match_parent属性。

小编的扩展链接

《Android 视图模块 全家桶》

猜你喜欢

转载自juejin.im/post/6980196615273316382