赞
踩
invalidate方法和postInvalidate方法都是用于进行View的刷新,invalidate方法应用在UI线程中,而postInvalidate方法应用在非UI线程中,用于将线程切换到UI线程,postInvalidate方法最后调用的也是invalidate方法。
- @UiThread
- public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {
-
- ...
-
- public void postInvalidate() {
- postInvalidateDelayed(0);
- }
-
- public void postInvalidate(int left, int top, int right, int bottom) {
- postInvalidateDelayed(0, left, top, right, bottom);
- }
-
- public void postInvalidateDelayed(long delayMilliseconds) {
- final AttachInfo attachInfo = mAttachInfo;
- if (attachInfo != null) {
- attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
- }
- }
- ...
- }

postInvalidate方法最后调用了ViewRootImpl的dispatchInvalidateDelayed方法
- //ViewRootImpl.class
-
- final ViewRootHandler mHandler = new ViewRootHandler();
-
- public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
- Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
- mHandler.sendMessageDelayed(msg, delayMilliseconds);
- }
ViewRootImpl中的dispatchInvalidateDelayed方法就是像ViewRootHandler发送了一个MSG_INVALIDATE消息,ViewRootHandler是ViewRootImpl中的一个内部Handler类。
- final class ViewRootHandler extends Handler {
- @Override
- public String getMessageName(Message message) {
- switch (message.what) {
- case MSG_INVALIDATE:
- return "MSG_INVALIDATE";
- ...
- }
- return super.getMessageName(message);
- }
-
- ...
-
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_INVALIDATE:
- ((View) msg.obj).invalidate();
- break;
- ...
- }
- }
- }

我们可以看到ViewRootHandler中对于MSG_INVALIDATE消息的处理就是调用的View的invalidate方法。
ViewRootImpl是在UI线程的,所以postInvalidate方法的作用就是将非UI线程的刷新操作切换到UI线程,以便在UI线程中调用invalidate方法刷新View。所以如果我们自定义的View本身就是在UI线程,没有用到多线程的话,直接用invalidate方法来刷新View就可以了
当我们调用View的invalidate方法后,View会去不断向上调用父布局的绘制方法并在这个过程中计算需要重绘的区域,最终调用过程会走到ViewRootImpl中,调用的是ViewRootImpl的performTraversals执行重绘操作。
原码查看分析:
- //View.class
- @UiThread
- public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {
-
- ...
-
- public void invalidate() {
- invalidate(true);
- }
-
- //invalidateCache为true表示全部重绘
- void invalidate(boolean invalidateCache) {
- invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
- }
-
- void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
- boolean fullInvalidate) {
-
- ...
-
- 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);
- //调用父类的invalidateChild方法
- p.invalidateChild(this, damage);
- }
-
- ...
- }
- }
- }

invalidateInternal方法中通过调用View的父布局invalidateChild方法来请求重绘,其中的damage变量表示的是需要进行重绘的区域,后面在一系列的调用过程中会不断根据父布局来调整这个绘制区域。
ViewGroup中的invalidateChild方法:
- @UiThread
- public abstract class ViewGroup extends View implements ViewParent, ViewManager {
-
- @Override
- public final void invalidateChild(View child, final Rect dirty) {
- ViewParent parent = this;
-
- final AttachInfo attachInfo = mAttachInfo;
- if (attachInfo != null) {
- ...
- //这里省略了一些重新计算绘制区域的逻辑
-
- //这是一个从当前的布局View向上不断遍历当前布局View的父布局,最后遍历到ViewRootImpl的循环
- do {
- View view = null;
- if (parent instanceof View) {
- view = (View) parent;
- }
-
- if (drawAnimation) {
- if (view != null) {
- view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
- } else if (parent instanceof ViewRootImpl) {
- ((ViewRootImpl) parent).mIsAnimating = true;
- }
- }
-
- ...
-
- //这里调用的是父布局的invalidateChildInParent方法
- parent = parent.invalidateChildInParent(location, dirty);
- ...
- } while (parent != null);
- }
- }
-
- @Override
- public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
- if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
- (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
- if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
- FLAG_OPTIMIZE_INVALIDATE) {
- ...
- //这里也是一些计算绘制区域的内容
-
- return mParent;
-
- } else {
- mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID;
-
- ...
- //这里也是一些计算绘制区域的内容
-
- return mParent;
- }
- }
-
- return null;
- }
- }

在ViewGroup的invalidateChild方法中有一个循环,循环里面会一直调用父布局的invalidateChildInParent方法,而View和ViewGroup的最终父布局都是ViewRootImpl。
所以View中的invalidateInternal方法和ViewGroup中的invalidateChild方法最后殊途同归,都会调用到ViewRootImpl中的方法。
- public final class ViewRootImpl implements ViewParent,View.AttachInfo.Callbacks, ThreadedRenderer.HardwareDrawCallbacks {
-
- //如果View没有父布局,那invalidateInternal方法就会调用这个方法
- @Override
- public void invalidateChild(View child, Rect dirty) {
- invalidateChildInParent(null, dirty);
- }
-
- //ViewGroup的invalidateChild方法最后会调用到这里
- @Override
- public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
- checkThread();
-
- //如果dirty为null就表示要重绘当前ViewRootImpl指示的整个区域
- if (dirty == null) {
- invalidate();
- return null;
- //如果dirty为empty则表示经过计算需要重绘的区域不需要绘制
- } 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);
- }
- }
-
- invalidateRectOnScreen(dirty);
-
- return null;
- }
-
- private void invalidateRectOnScreen(Rect dirty) {
- final Rect localDirty = mDirty;
- ...
- if (!mWillDrawSoon && (intersected || mIsAnimating)) {
- //调用scheduleTraversals方法进行绘制
- scheduleTraversals();
- }
- }
-
- //绘制整个ViewRootImpl区域
- void invalidate() {
- mDirty.set(0, 0, mWidth, mHeight);
- if (!mWillDrawSoon) {
- //调用scheduleTraversals方法进行绘制
- scheduleTraversals();
- }
- }
- }

可以看到在ViewRootImpl中最后都会调用scheduleTraversals方法进行绘制。
- //ViewRootImpl.class
- void scheduleTraversals() {
- if (!mTraversalScheduled) {
- mTraversalScheduled = true;
- mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
- //关键在这里!!!
- mChoreographer.postCallback(
- Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
- if (!mUnbufferedInputDispatch) {
- scheduleConsumeBatchedInput();
- }
- notifyRendererOfFramePending();
- pokeDrawLockIfNeeded();
- }
- }
scheduleTraversals方法中调用了mChoreographer.postCallback方法
Choreoprapher类的作用是编排输入事件、动画事件和绘制事件的执行,它的postCallback方法的作用就是将要执行的事件放入内部的一个队列中,最后会执行传入的Runnable,这里也就是mTraversalRunnable.
- //ViewRootImpl.class
- final class TraversalRunnable implements Runnable {
- @Override
- public void run() {
- doTraversal();
- }
- }
- final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
-
- void doTraversal() {
- if (mTraversalScheduled) {
- mTraversalScheduled = false;
- mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
-
- if (mProfile) {
- Debug.startMethodTracing("ViewAncestor");
- }
-
- //找到我们的performTraversals方法来,这里就是开始绘制View的地方啦!
- performTraversals();
-
- if (mProfile) {
- Debug.stopMethodTracing();
- mProfile = false;
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。