[关闭]
@ZeroGeek 2015-08-27T12:47:58.000000Z 字数 2021 阅读 684

Invalidate()与postInvaliate()区别

基础知识


Invalidate()

直接看看View类中的源码:

  1. public void invalidate() {
  2. invalidate(true);
  3. }
  4. void invalidate(boolean invalidateCache) {
  5. invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
  6. }
  7. void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
  8. boolean fullInvalidate) {
  9. if (mGhostView != null) {
  10. mGhostView.invalidate(true);
  11. return;
  12. }
  13. if (skipInvalidate()) {
  14. return;
  15. }
  16. if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)
  17. || (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID)
  18. || (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED
  19. || (fullInvalidate && isOpaque() != mLastIsOpaque)) {
  20. if (fullInvalidate) {
  21. mLastIsOpaque = isOpaque();
  22. mPrivateFlags &= ~PFLAG_DRAWN;
  23. }
  24. mPrivateFlags |= PFLAG_DIRTY;
  25. if (invalidateCache) {
  26. mPrivateFlags |= PFLAG_INVALIDATED;
  27. mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
  28. }
  29. // Propagate the damage rectangle to the parent view.
  30. final AttachInfo ai = mAttachInfo;
  31. final ViewParent p = mParent;
  32. if (p != null && ai != null && l < r && t < b) {
  33. final Rect damage = ai.mTmpInvalRect;
  34. damage.set(l, t, r, b);
  35. p.invalidateChild(this, damage);
  36. }
  37. // Damage the entire projection receiver, if necessary.
  38. if (mBackground != null && mBackground.isProjected()) {
  39. final View receiver = getProjectionReceiver();
  40. if (receiver != null) {
  41. receiver.damageInParent();
  42. }
  43. }
  44. // Damage the entire IsolatedZVolume receiving this view's shadow.
  45. if (isHardwareAccelerated() && getZ() != 0) {
  46. damageShadowReceiver();
  47. }
  48. }
  49. }

postInvaliate()

  1. public void postInvalidate() {
  2. postInvalidateDelayed(0);
  3. }
  4. public void postInvalidateDelayed(long delayMilliseconds) {
  5. // We try only with the AttachInfo because there's no point in invalidating
  6. // if we are not attached to our window
  7. final AttachInfo attachInfo = mAttachInfo;
  8. if (attachInfo != null) {
  9. attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
  10. }
  11. }

public final class ViewRootImpl implements ViewParent,
View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks

  1. public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
  2. Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
  3. mHandler.sendMessageDelayed(msg, delayMilliseconds);
  4. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注