当前位置:   article > 正文

精通Android自定义View(十五)invalidate方法和postInvalidate方法_android开发invalidate()和postinvalidate()

android开发invalidate()和postinvalidate()

 

1 概述

invalidate方法和postInvalidate方法都是用于进行View的刷新,invalidate方法应用在UI线程中,而postInvalidate方法应用在非UI线程中,用于将线程切换到UI线程,postInvalidate方法最后调用的也是invalidate方法。

 

2 postInvalidate方法源码分析

  1. @UiThread
  2. public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {
  3. ...
  4. public void postInvalidate() {
  5. postInvalidateDelayed(0);
  6. }
  7. public void postInvalidate(int left, int top, int right, int bottom) {
  8. postInvalidateDelayed(0, left, top, right, bottom);
  9. }
  10. public void postInvalidateDelayed(long delayMilliseconds) {
  11. final AttachInfo attachInfo = mAttachInfo;
  12. if (attachInfo != null) {
  13. attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
  14. }
  15. }
  16. ...
  17. }

postInvalidate方法最后调用了ViewRootImpl的dispatchInvalidateDelayed方法

  1. //ViewRootImpl.class
  2. final ViewRootHandler mHandler = new ViewRootHandler();
  3. public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
  4. Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
  5. mHandler.sendMessageDelayed(msg, delayMilliseconds);
  6. }

ViewRootImpl中的dispatchInvalidateDelayed方法就是像ViewRootHandler发送了一个MSG_INVALIDATE消息,ViewRootHandler是ViewRootImpl中的一个内部Handler类。

  1. final class ViewRootHandler extends Handler {
  2. @Override
  3. public String getMessageName(Message message) {
  4. switch (message.what) {
  5. case MSG_INVALIDATE:
  6. return "MSG_INVALIDATE";
  7. ...
  8. }
  9. return super.getMessageName(message);
  10. }
  11. ...
  12. @Override
  13. public void handleMessage(Message msg) {
  14. switch (msg.what) {
  15. case MSG_INVALIDATE:
  16. ((View) msg.obj).invalidate();
  17. break;
  18. ...
  19. }
  20. }
  21. }

我们可以看到ViewRootHandler中对于MSG_INVALIDATE消息的处理就是调用的View的invalidate方法。

ViewRootImpl是在UI线程的,所以postInvalidate方法的作用就是将非UI线程的刷新操作切换到UI线程,以便在UI线程中调用invalidate方法刷新View。所以如果我们自定义的View本身就是在UI线程,没有用到多线程的话,直接用invalidate方法来刷新View就可以了

3 invalidate方法刷新View的调用过程分析

当我们调用View的invalidate方法后,View会去不断向上调用父布局的绘制方法并在这个过程中计算需要重绘的区域,最终调用过程会走到ViewRootImpl中,调用的是ViewRootImpl的performTraversals执行重绘操作。

原码查看分析:

  1. //View.class
  2. @UiThread
  3. public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {
  4. ...
  5. public void invalidate() {
  6. invalidate(true);
  7. }
  8. //invalidateCache为true表示全部重绘
  9. void invalidate(boolean invalidateCache) {
  10. invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
  11. }
  12. void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
  13. boolean fullInvalidate) {
  14. ...
  15. final AttachInfo ai = mAttachInfo;
  16. final ViewParent p = mParent;
  17. if (p != null && ai != null && l < r && t < b) {
  18. final Rect damage = ai.mTmpInvalRect;
  19. damage.set(l, t, r, b);
  20. //调用父类的invalidateChild方法
  21. p.invalidateChild(this, damage);
  22. }
  23. ...
  24. }
  25. }
  26. }

invalidateInternal方法中通过调用View的父布局invalidateChild方法来请求重绘,其中的damage变量表示的是需要进行重绘的区域,后面在一系列的调用过程中会不断根据父布局来调整这个绘制区域。

 

ViewGroup中的invalidateChild方法:

  1. @UiThread
  2. public abstract class ViewGroup extends View implements ViewParent, ViewManager {
  3. @Override
  4. public final void invalidateChild(View child, final Rect dirty) {
  5. ViewParent parent = this;
  6. final AttachInfo attachInfo = mAttachInfo;
  7. if (attachInfo != null) {
  8. ...
  9. //这里省略了一些重新计算绘制区域的逻辑
  10. //这是一个从当前的布局View向上不断遍历当前布局View的父布局,最后遍历到ViewRootImpl的循环
  11. do {
  12. View view = null;
  13. if (parent instanceof View) {
  14. view = (View) parent;
  15. }
  16. if (drawAnimation) {
  17. if (view != null) {
  18. view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
  19. } else if (parent instanceof ViewRootImpl) {
  20. ((ViewRootImpl) parent).mIsAnimating = true;
  21. }
  22. }
  23. ...
  24. //这里调用的是父布局的invalidateChildInParent方法
  25. parent = parent.invalidateChildInParent(location, dirty);
  26. ...
  27. } while (parent != null);
  28. }
  29. }
  30. @Override
  31. public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
  32. if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN ||
  33. (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
  34. if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
  35. FLAG_OPTIMIZE_INVALIDATE) {
  36. ...
  37. //这里也是一些计算绘制区域的内容
  38. return mParent;
  39. } else {
  40. mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID;
  41. ...
  42. //这里也是一些计算绘制区域的内容
  43. return mParent;
  44. }
  45. }
  46. return null;
  47. }
  48. }

在ViewGroup的invalidateChild方法中有一个循环,循环里面会一直调用父布局的invalidateChildInParent方法,而View和ViewGroup的最终父布局都是ViewRootImpl。

 

所以View中的invalidateInternal方法和ViewGroup中的invalidateChild方法最后殊途同归,都会调用到ViewRootImpl中的方法。

  1. public final class ViewRootImpl implements ViewParent,View.AttachInfo.Callbacks, ThreadedRenderer.HardwareDrawCallbacks {
  2. //如果View没有父布局,那invalidateInternal方法就会调用这个方法
  3. @Override
  4. public void invalidateChild(View child, Rect dirty) {
  5. invalidateChildInParent(null, dirty);
  6. }
  7. //ViewGroup的invalidateChild方法最后会调用到这里
  8. @Override
  9. public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
  10. checkThread();
  11.     //如果dirty为null就表示要重绘当前ViewRootImpl指示的整个区域
  12. if (dirty == null) {
  13. invalidate();
  14. return null;
  15. //如果dirty为empty则表示经过计算需要重绘的区域不需要绘制
  16. } else if (dirty.isEmpty() && !mIsAnimating) {
  17. return null;
  18. }
  19. if (mCurScrollY != 0 || mTranslator != null) {
  20. mTempRect.set(dirty);
  21. dirty = mTempRect;
  22. if (mCurScrollY != 0) {
  23. dirty.offset(0, -mCurScrollY);
  24. }
  25. if (mTranslator != null) {
  26. mTranslator.translateRectInAppWindowToScreen(dirty);
  27. }
  28. if (mAttachInfo.mScalingRequired) {
  29. dirty.inset(-1, -1);
  30. }
  31. }
  32. invalidateRectOnScreen(dirty);
  33. return null;
  34. }
  35. private void invalidateRectOnScreen(Rect dirty) {
  36. final Rect localDirty = mDirty;
  37. ...
  38. if (!mWillDrawSoon && (intersected || mIsAnimating)) {
  39. //调用scheduleTraversals方法进行绘制
  40. scheduleTraversals();
  41. }
  42. }
  43. //绘制整个ViewRootImpl区域
  44. void invalidate() {
  45. mDirty.set(0, 0, mWidth, mHeight);
  46. if (!mWillDrawSoon) {
  47. //调用scheduleTraversals方法进行绘制
  48. scheduleTraversals();
  49. }
  50. }
  51. }

可以看到在ViewRootImpl中最后都会调用scheduleTraversals方法进行绘制。

  1. //ViewRootImpl.class
  2. void scheduleTraversals() {
  3. if (!mTraversalScheduled) {
  4. mTraversalScheduled = true;
  5. mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
  6. //关键在这里!!!
  7. mChoreographer.postCallback(
  8. Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
  9. if (!mUnbufferedInputDispatch) {
  10. scheduleConsumeBatchedInput();
  11. }
  12. notifyRendererOfFramePending();
  13. pokeDrawLockIfNeeded();
  14. }
  15. }

scheduleTraversals方法中调用了mChoreographer.postCallback方法

Choreoprapher类的作用是编排输入事件、动画事件和绘制事件的执行,它的postCallback方法的作用就是将要执行的事件放入内部的一个队列中,最后会执行传入的Runnable,这里也就是mTraversalRunnable.

  1. //ViewRootImpl.class
  2. final class TraversalRunnable implements Runnable {
  3. @Override
  4. public void run() {
  5. doTraversal();
  6. }
  7. }
  8. final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
  9. void doTraversal() {
  10. if (mTraversalScheduled) {
  11. mTraversalScheduled = false;
  12. mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
  13. if (mProfile) {
  14. Debug.startMethodTracing("ViewAncestor");
  15. }
  16. //找到我们的performTraversals方法来,这里就是开始绘制View的地方啦!
  17. performTraversals();
  18. if (mProfile) {
  19. Debug.stopMethodTracing();
  20. mProfile = false;
  21. }
  22. }
  23. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/742987
推荐阅读
相关标签
  

闽ICP备14008679号