在分析三个方法以前, 先梳理一下 ViewRootImpl.performTraversals, 若是不清楚该方法的做用的话, 请先阅读这篇博文 https://www.jianshu.com/p/2aac4e679549bash
/**
* ViewRootImpl.performTraversals
*/
public void performTraversals() {
// 1. 经过 mLayoutRequested 和 (!mStopped || mReportNextDraw) 构造 layoutRequested 变量
boolean layoutRequested = mLayoutRequested && (!mStopped || mReportNextDraw);
// 2. 判断 window 是否须要改变, 若 layoutRequested 为 false, windowShouldResize 也为 false
boolean windowShouldResize = layoutRequested && windowSizeMayChange
&& ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight())
|| (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
frame.width() < desiredWindowWidth && frame.width() != mWidth)
|| (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
frame.height() < desiredWindowHeight && frame.height() != mHeight));
// 3. 其中一个知足便可进入这个 if 语句
if (mFirst || windowShouldResize || insetsChanged ||
viewVisibilityChanged || params != null || mForceNextWindowRelayout) {
/// 3.1 判断 View 的宽高是否有改变, 没有的话, 不会调用 performMeasure
if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
|| mHeight != host.getMeasuredHeight() || contentInsetsChanged ||
updatedConfiguration) {
int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
// 3.1.1 执行 performMeasure
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
// 3.1.2 改变了 layoutRequested 的值
layoutRequested = true;
}
}
// 4. 若执行了 performMeasure, 通常状况下也会执行 performLayout
final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw);
if (didLayout) {
performLayout(lp, mWidth, mHeight);
}
// 5. 判断是否取消了绘制
boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || !isViewVisible;
if (!cancelDraw && !newSurface) {
if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
for (int i = 0; i < mPendingTransitions.size(); ++i) {
mPendingTransitions.get(i).startChangingAnimations();
}
mPendingTransitions.clear();
}
// 5.1 知足条件则执行 performDraw()
performDraw();
} else {
if (isViewVisible) {
// Try again
scheduleTraversals();
} else if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
for (int i = 0; i < mPendingTransitions.size(); ++i) {
mPendingTransitions.get(i).endChangingAnimations();
}
mPendingTransitions.clear();
}
}
// Traversals 结束
mIsInTraversal = false;
}
复制代码
梳理了 ViewRootImpl.performTraversals 以后, 能够看到对于 View 绘制三大流程中的前两个,app
这里大胆猜测一下 View.requestLayout 时 mLayoutRequested 这个 Flag 会变动为 true, 接下来带着问题来看看 View.requestLayout 的过程ide
private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
int desiredWindowHeight) {
/******************performLayout 的第一阶段*******************/
mLayoutRequested = false;
mScrollMayChange = true;
// layout 执行标记位
mInLayout = true;
final View host = mView;//mView 为 DecorView
if (host == null) return;
try {
// 1.1 执行第一次 layout
host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
// layout结束标记位
mInLayout = false;
/******************performLayout 的第二阶段*******************/
// 检查在进行 layout 的过程当中, 是否有 View 调用了 requestLayout 方法
int numViewsRequestingLayout = mLayoutRequesters.size();
if (numViewsRequestingLayout > 0) {
// mLayoutRequesters 就是在 mInLayout = true 的过程当中 requestLayout 的 view 集合
// 在 ViewRootImpl.requestLayoutDuringLayout 代码中有所体现
ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, false);
if (validLayoutRequesters != null) {
// 2.1 将 mHandlingLayoutInLayoutRequest 标记为 true
mHandlingLayoutInLayoutRequest = true;
// 处理新的布局请求
int numValidRequests = validLayoutRequesters.size();
for (int i = 0; i < numValidRequests; ++i) {
final View view = validLayoutRequesters.get(i);
// 在第一次 layout 执行结束后, 运行第二次 layout 请求
view.requestLayout();
}
// 2.2 执行二次 Measure
measureHierarchy(host, lp, mView.getContext().getResources(),
desiredWindowWidth, desiredWindowHeight);
// 2.3 执行二次 layout
mInLayout = true;
host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
// 2.4 将 mHandlingLayoutInLayoutRequest 标记为 false
mHandlingLayoutInLayoutRequest = false;
// 2.5 第三次检查 view 的 requestLayout 请求
validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true);
if (validLayoutRequesters != null) {
final ArrayList<View> finalRequesters = validLayoutRequesters;
getRunQueue().post(new Runnable() {
@Override
public void run() {
int numValidRequests = finalRequesters.size();
for (int i = 0; i < numValidRequests; ++i) {
final View view = finalRequesters.get(i);
// 2.5.1 在第二次 layout 的过程当中, 将请求 post 出去
view.requestLayout();
}
}
});
}
}
}
}
// 2.5 第二次 layout 结束的标志
mInLayout = false;
}
复制代码
能够看到在 performLayout 的过程当中,布局
/**
* View.requestLayout
*/
public void requestLayout() {
if (mMeasureCache != null) mMeasureCache.clear();
if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
ViewRootImpl viewRoot = getViewRootImpl();
// 1. 判断当前 ViewRootImpl 是否正在 Layout
if (viewRoot != null && viewRoot.isInLayout()) {// isInLayout() 为 true 的条件是 mInLayout = true
// 1.1 判断当前 View 是否能够在 ViewRootImpl 正在进行 Layout 时, 继续执行发起 requestLayout
if (!viewRoot.requestLayoutDuringLayout(this)) {
// 返回 false , 则说明不容许, 则直接让这次 View.requestLayout() return
return;
}
}
// 2 将本身的状态标记为正在 requestLayout
mAttachInfo.mViewRequestingLayout = this;
}
// 3. 给当前 View 打上标记 PFLAG_FORCE_LAYOUT | PFLAG_INVALIDATED
mPrivateFlags |= PFLAG_FORCE_LAYOUT;
mPrivateFlags |= PFLAG_INVALIDATED;
// 4. 尝试调用父容器的 requestLayout
if (mParent != null && !mParent.isLayoutRequested()) {
// 4.1 调用 ViewParent 的 requestLayout, 即调用父容器的 requestLayout
// ViewGroup 中并无重写 requestLayout 方法, 即还会调用到 View 的 requestLayout 方法中去
// 此方法会回溯到 当前 Window 的 ViewRootImpl 中的 requestLayout 中去
mParent.requestLayout();
}
// 5. 执行到这里, 说明当前 View 的 requestLayout 已经完成, 将 mAttachInfo.mViewRequestingLayout 置空
if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
mAttachInfo.mViewRequestingLayout = null;
}
}
/**
* ViewRootImpl.requestLayoutDuringLayout
*/
boolean requestLayoutDuringLayout(final View view) {
// 1.1.1 说明传入的 view 即为 ViewRootImpl, 由于只有它的 mParent 为null
if (view.mParent == null || view.mAttachInfo == null) {
// Would not normally trigger another layout, so just let it pass through as usual
return true;
}
// 1.1.2 将请求 Layout 的 View 添加到 ViewRootImpl 中维护的集合 mLayoutRequesters 中
if (!mLayoutRequesters.contains(view)) {
mLayoutRequesters.add(view);
}
if (!mHandlingLayoutInLayoutRequest) {
// 1.1.3 mHandlingLayoutInLayoutRequest 为 false
// 说明当前 ViewRootImpl 的 performLayout() 没有进行 second layout, 它将会在第二次 layout 中执行
return true;
} else {
// 1.1.4 mHandlingLayoutInLayoutRequest 为 true
// 说明当前 ViewRootImpl 的 performLayout() 正在进行 second layout, 此时 view 的 requestLayout 会被 post 到下一帧
// return false; 由上面代码可知 View 的 requestLayout() 请求会被直接 return;
return false;
}
}
/**
* View.isLayoutRequested
*/
public boolean isLayoutRequested() {
// 当 View 进行过 requestLayout() 时, mPrivateFlags |= PFLAG_FORCE_LAYOUT;
// 则会返回 true
return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
}
/**
* ViewRootImpl.requestLayout
* View.requestLayout() 最终的走向
*/
@Override
public void requestLayout() {
if (!mHandlingLayoutInLayoutRequest) {
checkThread();
// 将当前 mLayoutRequested 标记为 true, 该 Flag 会直接影响到 performTraversals 是否执行 measure 和 layout
mLayoutRequested = true;
// 这里又从新开启了 View 绘制的三大流程
scheduleTraversals();
}
}
复制代码
从上述代码可知 View.requestLayout 方法主要作了如下事情post
View.invalidate 方法会调用到 ViewGroup.invalidateChild 中ui
/**
* ViewGroup.invalidateChild
*/
public final void invalidateChild(View child, final Rect dirty) {
final AttachInfo attachInfo = mAttachInfo;
// child 的 parent 指定为自身
ViewParent parent = this;
if (attachInfo != null) {
do {
View view = null;
if (parent instanceof View) {
view = (View) parent;
}
// 会递归的调用 parent 的 invalidateChildInParent 方法
parent = parent.invalidateChildInParent(location, dirty);
} while (parent != null);
}
}
复制代码
能够看到 View.invalidate 方法会递归的调用 parent.invalidateChildInParent 方法, 直至回溯到 ViewRootImpl 中, 与 requestLayout 一模一样, ViewRootImpl 重写了 invalidateChildInParent 方法, 接下来看看, 它作了什么this
/**
* ViewRootImpl.invalidateChildInParent
*/
public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
checkThread();
if (dirty == null) {
// 1. 直接调用了 invalidate 方法
invalidate();
return null;
} else if (dirty.isEmpty() && !mIsAnimating) {
return null;
}
if (mCurScrollY != 0 || mTranslator != null) {
mTempRect.set(dirty);
dirty = mTempRect;
if (mCurScrollY != 0) {
dirty.offset(0, -mCurScrollY);
}
if (mTranslator != null) {
mTranslator.translateRectInAppWindowToScreen(dirty);
}
if (mAttachInfo.mScalingRequired) {
dirty.inset(-1, -1);
}
}
// 2.调用了 invalidateRectOnScreen 方法, 刷新区域内的视图
invalidateRectOnScreen(dirty);
return null;
}
/**
* ViewRootImpl.invalidate
*/
void invalidate() {
mDirty.set(0, 0, mWidth, mHeight);
if (!mWillDrawSoon) {
// 看到了最熟悉的 scheduleTraversals
scheduleTraversals();
}
}
/**
* ViewRootImpl.invalidateRectOnScreen
*/
private void invalidateRectOnScreen(Rect dirty) {
final Rect localDirty = mDirty;
if (!localDirty.isEmpty() && !localDirty.contains(dirty)) {
mAttachInfo.mSetIgnoreDirtyState = true;
mAttachInfo.mIgnoreDirtyState = true;
}
localDirty.union(dirty.left, dirty.top, dirty.right, dirty.bottom);
final float appScale = mAttachInfo.mApplicationScale;
final boolean intersected = localDirty.intersect(0, 0,
(int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
if (!intersected) {
localDirty.setEmpty();
}
if (!mWillDrawSoon && (intersected || mIsAnimating)) {
// 看到了最熟悉的 scheduleTraversals
scheduleTraversals();
}
}
复制代码
看到 ViewRootImpl 中 invalidateChildInParent 最终都回调了 scheduleTraversals 方法, 开启了 View 绘制的三大流程spa
/**
* View.postInvalidate
*/
public void postInvalidate() {
postInvalidateDelayed(0);
}
/**
* View.postInvalidateDelayed
*/
public void postInvalidateDelayed(long delayMilliseconds) {
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
// 到 ViewRootImpl 中分发
attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
}
}
复制代码
能够看到 View 调用 postInvalidate 时, 最终会流入 ViewRootImpl 中, 接下里看看 ViewRootImpl 作了哪些操做线程
/**
* ViewRootImpl.dispatchInvalidateDelayed
*/
public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
mHandler.sendMessageDelayed(msg, delayMilliseconds);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INVALIDATE:
// 在 mHandler 绑定的线程中调用了 View 的 invalidate
((View) msg.obj).invalidate();
break;
}
复制代码
能够看到 View.postInvalidate 本质上仍是调用了 View.invalidate(), 它在调用以前会加入消息队列, 投递到 Handler 建立线程去执行, 也就是在非 UI 线程咱们想从新绘制时, 咱们能够采用 postInvalidate 这种方式code
不得不叹服SDK开发者的技巧, 其中有不少细节都没有兼顾到, 只是为了厘清 requestLayout, invalidate, postInvalidate 这三者的工做流程, 与部分细节, 可能有不少不到位的地方, 但愿可以批评指出