提醒:下面源码来自SDK里Android-34版本
一、requestLayout
点击查看requestLayout官网文档
1.1 requestLayout方法源码
/*** Call this when something has changed which has invalidated the* layout of this view. This will schedule a layout pass of the view* tree. This should not be called while the view hierarchy is currently in a layout* pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the* end of the current layout pass (and then layout will run again) or after the current* frame is drawn and the next layout occurs.** <p>Subclasses which override this method should call the superclass method to* handle possible request-during-layout errors correctly.</p>* 上面翻译* 当某些东西发生改变使。无效时调用这个* 这个视图的布局。这将安排视图的布局传递树。* 当视图层次结构当前处于布局时,不应该调用这个函数* pass ({@link #isInLayout()})。如果正在进行布局,* 请求可能会在结束当前布局传递(然后布局将再次运行)* 或在当前绘制框架,并发生下一个布局。* * 重写此方法的子类应该调用父类方法正确处理布局期间可能出现的请求错误。*/@CallSuperpublic void requestLayout() {if (isRelayoutTracingEnabled()) {Trace.instantForTrack(TRACE_TAG_APP, "requestLayoutTracing",mTracingStrings.classSimpleName);printStackStrace(mTracingStrings.requestLayoutStacktracePrefix);}if (mMeasureCache != null) mMeasureCache.clear();if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {// Only trigger request-during-layout logic if this is the view requesting it,// not the views in its parent hierarchyViewRootImpl viewRoot = getViewRootImpl();if (viewRoot != null && viewRoot.isInLayout()) {if (!viewRoot.requestLayoutDuringLayout(this)) {return;}}mAttachInfo.mViewRequestingLayout = this;}mPrivateFlags |= PFLAG_FORCE_LAYOUT;mPrivateFlags |= PFLAG_INVALIDATED;if (mParent != null && !mParent.isLayoutRequested()) {mParent.requestLayout();}if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {mAttachInfo.mViewRequestingLayout = null;}}
1.2 requestLayoutDuringLayout方法源码
/*** Called by {@link android.view.View#requestLayout()} if the view hierarchy is currently* undergoing a layout pass. requestLayout() should not generally be called during layout,* unless the container hierarchy knows what it is doing (i.e., it is fine as long as* all children in that container hierarchy are measured and laid out at the end of the layout* pass for that container). If requestLayout() is called anyway, we handle it correctly* by registering all requesters during a frame as it proceeds. At the end of the frame,* we check all of those views to see if any still have pending layout requests, which* indicates that they were not correctly handled by their container hierarchy. If that is* the case, we clear all such flags in the tree, to remove the buggy flag state that leads* to blank containers, and force a second request/measure/layout pass in this frame. If* more requestLayout() calls are received during that second layout pass, we post those* requests to the next frame to avoid possible infinite loops.** <p>The return value from this method indicates whether the request should proceed* (if it is a request during the first layout pass) or should be skipped and posted to the* next frame (if it is a request during the second layout pass).</p>** @param view the view that requested the layout.** @return true if request should proceed, false otherwise.*/boolean requestLayoutDuringLayout(final View view) {if (view.mParent == null || view.mAttachInfo == null) {// Would not normally trigger another layout, so just let it pass through as usualreturn true;}if (!mLayoutRequesters.contains(view)) {mLayoutRequesters.add(view);}if (!mHandlingLayoutInLayoutRequest) {// Let the request proceed normally; it will be processed in a second layout pass// if necessaryreturn true;} else {// Don't let the request proceed during the second layout pass.// It will post to the next frame instead.return false;}}
注释翻译:
由{@link android.view. view# requestLayout()}调用,如果视图层次是当前的正在进行布局检查。requestLayout()通常 不应该在布局期间调用,
除非容器层次结构知道它在做什么(即,只要
该容器层次结构中的所有子节点都在布局的末尾进行测量和布局
传递该容器)。如果requestLayout()被调用,我们将正确地处理它
通过在帧进行时注册所有请求者。在画面的最后,
我们检查所有这些视图,看看是否还有待处理的布局请求
指示它们没有被其容器层次结构正确处理。如果是这样的话
在这种情况下,我们清除树中所有这样的旗帜,以移除导致的有bug的旗帜状态
空白容器,并强制在此框架中通过第二个请求/测量/布局。如果
在第二次布局过程中接收到更多的requestLayout()调用,我们发布它们
请求转到下一帧,以避免可能的无限循环。
此方法的返回值指示请求是否应该继续
(如果它是第一次布局传递期间的请求)或应该跳过并发布到
下一帧(如果它是第二个布局传递期间的请求)。
查看请求布局的视图。
如果请求应该继续,则返回true,否则返回false。
requestLayoutDuringLayout方法源码-图文分析
1.3 总结
- 当视图层次结构当前处于布局时,不应该调用requestLayout方法
- 会调用measure方法和layout方法
二、invalidate
2.1 invalidate方法源码
/*** Invalidate the whole view. If the view is visible,* {@link #onDraw(android.graphics.Canvas)} will be called at some point in* the future.* <p>* This must be called from a UI thread. To call from a non-UI thread, call* {@link #postInvalidate()}.*/public void invalidate() {invalidate(true);}
2.2 invalidate(boolean invalidateCache)方法源码
/*** This is where the invalidate() work actually happens. A full invalidate()* causes the drawing cache to be invalidated, but this function can be* called with invalidateCache set to false to skip that invalidation step* for cases that do not need it (for example, a component that remains at* the same dimensions with the same content).** @param invalidateCache Whether the drawing cache for this view should be* invalidated as well. This is usually true for a full* invalidate, but may be set to false if the View's contents or* dimensions have not changed.* @hide*/@UnsupportedAppUsagepublic void invalidate(boolean invalidateCache) {invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);}
2.3 invalidateInternal方法源码
void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,boolean fullInvalidate) {if (mGhostView != null) {mGhostView.invalidate(true);return;}if (skipInvalidate()) {return;}// Reset content capture cachesmPrivateFlags4 &= ~PFLAG4_CONTENT_CAPTURE_IMPORTANCE_MASK;mContentCaptureSessionCached = false;if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)|| (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID)|| (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED|| (fullInvalidate && isOpaque() != mLastIsOpaque)) {if (fullInvalidate) {mLastIsOpaque = isOpaque();mPrivateFlags &= ~PFLAG_DRAWN;}mPrivateFlags |= PFLAG_DIRTY;if (invalidateCache) {mPrivateFlags |= PFLAG_INVALIDATED;mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;}// Propagate the damage rectangle to the parent view.final AttachInfo ai = mAttachInfo;final ViewParent p = mParent;if (p != null && ai != null && l < r && t < b) {final Rect damage = ai.mTmpInvalRect;damage.set(l, t, r, b);p.invalidateChild(this, damage);}// Damage the entire projection receiver, if necessary.if (mBackground != null && mBackground.isProjected()) {final View receiver = getProjectionReceiver();if (receiver != null) {receiver.damageInParent();}}}}
2.4 isOpaque方法源码
/*** Indicates whether this View is opaque. An opaque View guarantees that it will* draw all the pixels overlapping its bounds using a fully opaque color.** Subclasses of View should override this method whenever possible to indicate* whether an instance is opaque. Opaque Views are treated in a special way by* the View hierarchy, possibly allowing it to perform optimizations during* invalidate/draw passes.** @return True if this View is guaranteed to be fully opaque, false otherwise.*/@ViewDebug.ExportedProperty(category = "drawing")public boolean isOpaque() {return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&getFinalAlpha() >= 1.0f;}
指示此视图是否不透明。一个不透明的视图保证它会这样做
使用完全不透明的颜色绘制与边界重叠的所有像素。
View的子类应该尽可能地覆盖这个方法
实例是否不透明。以一种特殊的方式处理不透明视图
视图层次结构,可能允许它执行优化期间
失效/画传递。
@return True如果这个视图保证是完全不透明的,否则返回false。
2.5 总结
- 必须在主线程调用invalidate方法
- invalidate方法会调用onDraw方法绘制新的内容
三、postInvalidate
3.1 postInvalidate方法源码
/*** <p>Cause an invalidate to happen on a subsequent cycle through the event loop.* Use this to invalidate the View from a non-UI thread.</p>** <p>This method can be invoked from outside of the UI thread* only when this View is attached to a window.</p>** @see #invalidate()* @see #postInvalidateDelayed(long)*/public void postInvalidate() {postInvalidateDelayed(0);}
3.2 postInvalidateDelayed方法源码
/*** <p>Cause an invalidate of the specified area to happen on a subsequent cycle* through the event loop. Use this to invalidate the View from a non-UI thread.</p>** <p>This method can be invoked from outside of the UI thread* only when this View is attached to a window.</p>** @param left The left coordinate of the rectangle to invalidate.* @param top The top coordinate of the rectangle to invalidate.* @param right The right coordinate of the rectangle to invalidate.* @param bottom The bottom coordinate of the rectangle to invalidate.** @see #invalidate(int, int, int, int)* @see #invalidate(Rect)* @see #postInvalidateDelayed(long, int, int, int, int)*/public void postInvalidate(int left, int top, int right, int bottom) {postInvalidateDelayed(0, left, top, right, bottom);}
3.3 postInvalidateDelayed(long delayMilliseconds)方法源码
/*** <p>Cause an invalidate to happen on a subsequent cycle through the event* loop. Waits for the specified amount of time.</p>** <p>This method can be invoked from outside of the UI thread* only when this View is attached to a window.</p>** @param delayMilliseconds the duration in milliseconds to delay the* invalidation by** @see #invalidate()* @see #postInvalidate()*/public void postInvalidateDelayed(long delayMilliseconds) {// We try only with the AttachInfo because there's no point in invalidating// if we are not attached to our windowfinal AttachInfo attachInfo = mAttachInfo;if (attachInfo != null) {attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);}}
3.4 postInvalidateDelayed(long delayMilliseconds, int left, int top, int right, int bottom) 方法源码
/*** <p>Cause an invalidate of the specified area to happen on a subsequent cycle* through the event loop. Waits for the specified amount of time.</p>** <p>This method can be invoked from outside of the UI thread* only when this View is attached to a window.</p>** @param delayMilliseconds the duration in milliseconds to delay the* invalidation by* @param left The left coordinate of the rectangle to invalidate.* @param top The top coordinate of the rectangle to invalidate.* @param right The right coordinate of the rectangle to invalidate.* @param bottom The bottom coordinate of the rectangle to invalidate.** @see #invalidate(int, int, int, int)* @see #invalidate(Rect)* @see #postInvalidate(int, int, int, int)*/public void postInvalidateDelayed(long delayMilliseconds, int left, int top,int right, int bottom) {// We try only with the AttachInfo because there's no point in invalidating// if we are not attached to our windowfinal AttachInfo attachInfo = mAttachInfo;if (attachInfo != null) {final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.obtain();info.target = this;info.left = left;info.top = top;info.right = right;info.bottom = bottom;attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);}}
dispatchInvalidateDelayed方法
handleMessageImpl方法接收MSG_INVALIDATE消息
3.5 总结
- 可以在非UI线程或UI线程中调用postInvalidate
- 当调用postInvalidate()方法时,会调用dispatchInvalidateDelayed通过Handler将重绘任务添加到主线程的消息队列中。在UI线程的消息循环中,当处理到该消息时,会触发invalidate()方法来进行实际的重绘操作。