是Android的自定义View-绘制流程-布局

2021-02-03  本文已影响0人  redrain39

前言

performTraversals方法中,执行完measure之后,接下来就要开始进行layout的过程了。

源码分析

分析入口

performTraversals中有这么一个方法:

// 执行layout的方法,在measure之后
performLayout(lp, mWidth, mHeight);
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, int desiredWindowHeight) {
    // 核心方法,进行layout操作 --> 分析1.开始layout流程
    final View host = mView;
    host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());

    ···
}

分析1.开始layout流程

// 开始layout流程的地方
public void layout(int l, int t, int r, int b) {  
    // 当前视图的四个顶点
    int oldL = mLeft;  
    int oldT = mTop;  
    int oldB = mBottom;  
    int oldR = mRight;  
      
    
    // 即初始化四个顶点的值、判断当前View大小和位置是否发生了变化 & 返回 
    // 确定View的位置:setFrame() / setOpticalFrame()
    // 判断的基准:isLayoutModeOptical()
    boolean changed = isLayoutModeOptical(mParent) ?
            setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);

    // 若视图的大小 & 位置发生变化
    // 会重新确定该View所有的子View在父容器的位置:onLayout()
    if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {  
        // onLayout方法中存在View和ViewGroup之间的区别
        // 在View中是一个空实现,而ViewGroup中需要对子View进行布局计算 --> 分析2.响应布局操作
        onLayout(changed, l, t, r, b);  
        
        ···
}  

isLayoutModeOptical分析

// 判断是否是ViewGroup
public static boolean isLayoutModeOptical(Object o) {
    return o instanceof ViewGroup && ((ViewGroup) o).isLayoutModeOptical();
}

setFrame分析

protected boolean setFrame(int left, int top, int right, int bottom) {
    boolean changed = false;

    // 如果发生布局位置的变化,执行操作
    if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
        changed = true;

        int drawn = mPrivateFlags & PFLAG_DRAWN;

        // 判断尺寸是否发生改变
        int oldWidth = mRight - mLeft;
        int oldHeight = mBottom - mTop;
        int newWidth = right - left;
        int newHeight = bottom - top;
        boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);

        // 视图刷新
        invalidate(sizeChanged);

        // 同步布局位置数据
        mLeft = left;
        mTop = top;
        mRight = right;
        mBottom = bottom;
        mRenderNode.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);

        mPrivateFlags |= PFLAG_HAS_BOUNDS;
        
        // 调用尺寸变化的方法 --> 额外分析:尺寸变化
        if (sizeChanged) {
            sizeChange(newWidth, newHeight, oldWidth, oldHeight);
        }

        // 后续的尺寸变化操作
        if ((mViewFlags & VISIBILITY_MASK) == VISIBLE || mGhostView != null) {
            
            mPrivateFlags |= PFLAG_DRAWN;
            invalidate(sizeChanged);
            
            invalidateParentCaches();
        }

        mPrivateFlags |= drawn;

        mBackgroundSizeChanged = true;
        mDefaultFocusHighlightSizeChanged = true;
        if (mForegroundInfo != null) {
            mForegroundInfo.mBoundsChanged = true;
        }

        notifySubtreeAccessibilityStateChangedIfNeeded();
    }
    return changed;
}

额外分析:尺寸变化

private void sizeChange(int newWidth, int newHeight, int oldWidth, int oldHeight) {
    // 会调用onSizeChanged方法,在自定义View中可以复写此方法,在尺寸发生变化时进行响应
    onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
    
    ···
}

setOpticalFrame分析

private boolean setOpticalFrame(int left, int top, int right, int bottom) {

    Insets parentInsets = mParent instanceof View ?
            ((View) mParent).getOpticalInsets() : Insets.NONE;

    Insets childInsets = getOpticalInsets();

    // 内部实际上是调用setFrame()
    return setFrame(
            left   + parentInsets.left - childInsets.left,
            top    + parentInsets.top  - childInsets.top,
            right  + parentInsets.left + childInsets.right,
            bottom + parentInsets.top  + childInsets.bottom);
}

分析2.响应布局操作

View

// View的onLayout的内部是一个空实现
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

   // 参数说明
   // changed 当前View的大小和位置改变了 
   // left 左部位置
   // top 顶部位置
   // right 右部位置
   // bottom 底部位置
}  

ViewGroup

由于ViewGroup中的onLayout是一个抽象方法,由子类布局根据逻辑来进行计算的,这里以FrameLayout为例:

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) {
    // 获取子View的数量
    final int count = getChildCount();
    
    // 获取父布局的坐标位置
    final int parentLeft = getPaddingLeftWithForeground();
    final int parentRight = right - left - getPaddingRightWithForeground();
    final int parentTop = getPaddingTopWithForeground();
    final int parentBottom = bottom - top - getPaddingBottomWithForeground();

    // 遍历子View,进行位置的计算
    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;
            }

            // 子View进行位置计算
            child.layout(childLeft, childTop, childLeft + width, childTop + height);
        }
    }
}

总结

关于View的layout过程总体来讲可以分为两种:

View的Layout过程就只有单独的对自身位置的一个布局,而ViewGroup的Layout过程需要遍历子View进行整体布局。

在日常的开过程中,一般的会覆写自定义View的onLayout方法:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    // 可以拿到实际的宽高
}

当然如果需要进行完全的自定义也可以直接复写layout方法。

另外,还有那个额外的拓展onSizeChanged,在尺寸发生变化的时候,它会被调用。

上一篇 下一篇

猜你喜欢

热点阅读