FrameLayout源码笔记

FrameLayout:帧布局
布局效果:child通过层叠的效果叠加在布局中,布局的大小由child的大小或者背景图片大小、前景图片大小决定。

FrameLayout对child的操作相对较少,所以代码主要体现在:

  1. onMeasure
  2. onLayout

onMeasure分析:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();//获取Children的数目
​
        final boolean measureMatchParentChildren =//是否需要测量MatchParent属性的Children
                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);
            if (mMeasureAllChildren || child.getVisibility() != GONE) {
                //一个是自身处于非GONE状态下,一个是设置了mMeasureAllChildren=true 
                //测量,最终调用
                //child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                //获取最大值
                maxWidth = Math.max(maxWidth,
                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                //合并child的视图状态
                childState = combineMeasuredStates(childState, child.getMeasuredState());
                if (measureMatchParentChildren) {//把math_parent的View加入集合
                    if (lp.width == LayoutParams.MATCH_PARENT ||
                            lp.height == LayoutParams.MATCH_PARENT) {
                        mMatchParentChildren.add(child);
                    }
                }
            }
        }
​
        // Account for padding too 加上padding
        maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
        maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();
​
        // Check against our minimum height and width //检测是否大于最小宽高
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
​
        // Check against our foreground's minimum height and width
        final Drawable drawable = getForeground();
        if (drawable != null) {//检测前景图片的最小宽高
            maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
            maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
        }

​
       //通过resolveSizeAndState获取需要的MeasureSpec 
       setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));
​
        count = mMatchParentChildren.size();
        if (count > 1) {
            for (int i = 0; i < count; i++) {//重新测量match_parent的View
                final View child = mMatchParentChildren.get(i);
                final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
​
                final int childWidthMeasureSpec;
                //为match_parent,通过makeMeasureSpec获取measureSpec为
                //精准模式+layout宽度减去内边距和外边距
                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 {
                    //通过getChildMeasureSpec获取measureSpec
                    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);
            }
        }
    }

onMeasure()方法里面主要的设计思路:

  1. 测量childView的宽高和状态,获取最大宽高
  2. 调用setMeasuredDimension(..)设置自身的宽高
  3. 重新测量childView中含有math_parent的view(采用FrameLayout的getMeasureWidht/getMeasureHeigh计算)
  4. child.measure(..)

FrameLayout的onLayout中的代码:

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        layoutChildren(left, top, right, bottom, false /* no force left gravity */);
   }

可以看到是调用了layoutChildren(..)进行布局的排版

void layoutChildren(int left, int top, int right, int bottom,
                                  boolean forceLeftGravity) {
        final int count = getChildCount();//获取child个数
​
        final int parentLeft = getPaddingLeftWithForeground();//布局的左边坐标
        //布局的右边坐标
        final int parentRight = right - left - getPaddingRightWithForeground();
​
        final int parentTop = getPaddingTopWithForeground();
        final int parentBottom = bottom - top - getPaddingBottomWithForeground();
​
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
​
                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();
​
                int childLeft;
                int childTop;
​
                int gravity = lp.gravity;
                if (gravity == -1) {
                    gravity = DEFAULT_CHILD_GRAVITY;
                }
​
                final int layoutDirection = getLayoutDirection();//布局方向,水平方向用到(左<->右)
                final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
                final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
​
                switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {//水平
                    case Gravity.CENTER_HORIZONTAL:
                        childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
                        lp.leftMargin - lp.rightMargin;
                        break;
                    case Gravity.RIGHT:
                        if (!forceLeftGravity) {
                            childLeft = parentRight - width - lp.rightMargin;
                            break;
                        }
                    case Gravity.LEFT:
                    default:
                        childLeft = parentLeft + lp.leftMargin;
                }
​
                switch (verticalGravity) {
                    case Gravity.TOP:
                        childTop = parentTop + lp.topMargin;
                        break;
                    case Gravity.CENTER_VERTICAL:
                        childTop = parentTop + (parentBottom - parentTop - height) / 2 +
                        lp.topMargin - lp.bottomMargin;
                        break;
                    case Gravity.BOTTOM:
                        childTop = parentBottom - height - lp.bottomMargin;
                        break;
                    default:
                        childTop = parentTop + lp.topMargin;
                }
​
             //上面通过gravity的值计算出child的4边   
             child.layout(childLeft, childTop, childLeft + width, childTop + height);
            }
        }
    }

onLayout(..)主要是对child的排版,主要思路是:

  1. 获取parent的左上右下。
  2. 根据gravity的值(水平还需要获取布局方向)计算出child需要的左上右下坐标
  3. 调用child的layout方法进行排版

##总结

  • FrameLayout是一个相对简单的布局,也是自定义ViewGroup继承最多的一个。
  • 主要注意的是onMeasure和onLayout方法。
  • onMeaure要考虑children把控件撑大的情况
  • onLayout要考虑gravity和Direction(方向)

END

– Nowy

– 2017.04.12

分享到