Android|Android View 的绘制流程 03 - performLayout

Android View 的绘制流程 - 开篇 MeasureSpec
Android View 的绘制流程 01 - 前置流程
Android View 的绘制流程 02 - performMeasure
Android View 的绘制流程 03 - performLayout
Android View 的绘制流程 04 - performDraw
Android View 的绘制流程总结
上一章学习了 Measure 的过程. 这章开始学习 Layout 的流程.
3. ViewRootImpl.performTraversals() ViewRootImpl.java 1576 行

private void performTraversals() { ... if (...) { ... if (...) { if (...) { int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width); int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height); // Ask host how big it wants to be //测量 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); ... layoutRequested = true; } } } else { ... } final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw); if (didLayout) { //摆放 performLayout(lp, mWidth, mHeight); ... } ... boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || !isViewVisible; if (!cancelDraw && !newSurface) { ... //绘制 performDraw(); } else { ... } ... }

从 preformLayout 开始跟进.
mWidth 和 mHeight 是 window (也就是 PhoneWindow) 的高度和宽度.
lp 是 window 的 LayoutParams 值. lp.width 和 lp.height 默认都是 MATCH_PARENT,
进入到 preformLayout ()


3.1 performLayout() ViewRootImpl.java 2467行
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, int desiredWindowHeight) { ... final View host = mView; ... try { ... host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); ... } ... }

代码中把 mView 赋值给了 host, (mView = DecroView)
然后调用 decorView 的 layout 方法.
由于 decorView 是一个容器(FrameLayout),是 ViewGroup 的子类,所以跟踪代码的时候,实际上是先进入到 ViewGroup 类中的layout方法中


3.2 ViewGroup.layout() ViewGroup.java 6048行
@Override public final void layout(int l, int t, int r, int b) { ... super.layout(l, t, r, b); ... }

看到是一个 final 类型的方法, 说明 ViewGroup 的子类都无法重写这个方法.
接着调用了 super.layout 方法. ViewGroup 的父类是 View. 接下来进入 View 类中


3.3 View.layout() View.java 19571行
public void layout(int l, int t, int r, int b) { ... int oldL = mLeft; int oldT = mTop; int oldB = mBottom; int oldR = mRight; boolean changed = isLayoutModeOptical(mParent) ? setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b); ... if (...) { onLayout(changed, l, t, r, b); ... } ... }

官方对这个方法的翻译为
为视图及其所有子视图分配大小和位置
这是布局机制的第二阶段, (首先是测量). 在这个阶段, 每一个父布局都会调用它所有子布局的 layout 来定位它们. 通常是使用在测量阶段存储的 child 测量值.
派生类不应该重写该方法. 有子 view 的派生类(也就是容器类,父布局)应该重写 onLayout 方法。在重写的 onLayout 方法中,它们应该为每一个子 view 的 layout 方法.
参数依次为:Left、Top、Right、Bottom四个点相对父布局的位置。
注:
setFrame(l, t, r, b) 可以理解为给 mLeft , mTop, mRight, mBottom 这四个变量赋值, 然后基本就能确定当前 View 在父视图的位置了. 这几个值构成的矩形区域就是当前 View 显示的位置,这里的具体位置都是相对与父视图的位置。
接着调用了 onLayout 方法. 这和上一章我们学习的 measure 调用 onMeasure 好像有点类似. 跟进去.


3.4 View.onLayout() View.java 19631行
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { }

首先官方对这个方法的翻译为:
当该 View 要分配尺寸和位置给它的每一个子 View 时, 该方法会从 layout 方法中被调用.
带有子 View 的派生类 ( 就是上面说的容器, 父布局 ) 应该覆盖此方法,并在每个子 View 调用 layout。
看到这是一个空方法. 对于 View 来说, onLayout 只是一个空实现, 一般情况下我们自定义 View 的时候, 也不需要重载该方法.
但是对于 ViewGroup 来说是有用的 ,因为 layout 摆放过程就是父容器布局子 View 的过程. 所以说如果当前 View 是一个容器, 那么流程就会进入到 ViewGroup 的 onLayout 方法中.
但是发现 ViewGroup 中 onLayout 方法只是一个抽象类. 要求子类必须重写 onLayout 函数. 我们直接进入到 DecorView 中看 onLayout()


3.5 DecorView.onLayout() DecorView.java 757行
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); ... }

直接调用了父类的 onLayout. 因为 DecorView 继承自 FrameLayout. 所以继续跟进到 FrameLayout 的 onLayout 方法中.


3.6 FrameLayout.onLayout FrameLayout.java 260行
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { layoutChildren(left, top, right, bottom, false /* no force left gravity */); }void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) { final int count = getChildCount(); ... for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { ... child.layout(childLeft, childTop, childLeft + width, childTop + height); } } }

看到这里是不是觉得和 onMeasure 有点相似了. 同样的循环子 View. 不同的是这里循环子 View 调用的是子 View 的 layout 方法.
那么知道这个时候 View 是 DecorView, 他的子 View 是一个 LinearLayout, 也是一个父布局/容器. 那么就又会调用步骤 3.2, 接着调用 步骤3.3 然后在步骤 3.3 中调用 setFrame() 方法 摆放好自己. 接着就会调用 LinearLayout 的 onLayout 方法,摆放内部的子 View
.... 这样递归下去.
我们如果弄懂了上一章中 onMeasure 的流程, 那么这个 onLayout 就很容易理解了.
onMeasure 是递归到最后一层, 才开始测量, 然后一层一层的向上传递, 测量父 View.
onLayout 同样也是递归, 但是无论是 View, 还是 ViewGroup 都会先把自己摆放布局好, 才去摆放布局子 View .
【Android|Android View 的绘制流程 03 - performLayout】

其实理解了 onMeasure 流程后, 再来看 onLayout 流程, 就会觉得非常简单了, 还是建议大家先去看上一章的 onMeasure 流程.
好了, 关于 onLayout 就写到这里. 写一章开始学习 绘制的流程..
preforLayout 流程图

    推荐阅读