invalidate、postInvalidat、 postInvalidateOnAnimation 区别

来源:互联网 发布:数据库怎么删除表 编辑:程序博客网 时间:2024/06/03 18:02
1.先看postInvalidate方法执行过程
[code]    public void postInvalidate() {        postInvalidateDelayed(0);    }    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 window        final AttachInfo attachInfo = mAttachInfo;        if (attachInfo != null) {            attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);        }    }

[code]public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {        Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);        mHandler.sendMessageDelayed(msg, delayMilliseconds);    }

从代码中看出,调用postInvalidate方法后,会用mHandler发出一条延迟消息MSG_INVALIDATE,这里延时为0(没有主动调用postInvalidateDelayed的话)。 
再看mHandler的执行代码:
[code]        @Override        public void handleMessage(Message msg) {            switch (msg.what) {            case MSG_INVALIDATE:                ((View) msg.obj).invalidate();                break;                ...

代码中处理消息时view.invalidate方法是立即执行的(排除其它原因:排队、阻塞、CPU抢占等)。 
也就是说postInvalidate()实际将invalidate任务加入队列中,理想民的 不考虑排队之类的情况,可以看成是立即执行的。
2.再来看postInvalidateOnAnimation的执行过程
[code]public void postInvalidateOnAnimation() {        // We try only with the AttachInfo because there's no point in invalidating        // if we are not attached to our window        final AttachInfo attachInfo = mAttachInfo;        if (attachInfo != null) {            attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);        }    }public void dispatchInvalidateOnAnimation(View view) {        mInvalidateOnAnimationRunnable.addView(view);    }

这里先把view加入到了某个集合中–mInvalidateOnAnimationRunnable。 
这里的mInvalidateOnAnimationRunnable是InvalidateOnAnimationRunnable的实例化对象。 
再接着看InvalidateOnAnimationRunnable中添加view之后做了什么:
[code] final class InvalidateOnAnimationRunnable implements Runnable {        private boolean mPosted;        private final ArrayList<View> mViews = new ArrayList<View>();        private final ArrayList<AttachInfo.InvalidateInfo> mViewRects =                new ArrayList<AttachInfo.InvalidateInfo>();        private View[] mTempViews;        private AttachInfo.InvalidateInfo[] mTempViewRects;        public void addView(View view) {            synchronized (this) {                mViews.add(view);                postIfNeededLocked();            }        }        ...        @Override        public void run(){            final int viewCount;            final int viewRectCount;            synchronized (this) {                mPosted = false;            //执行view.invalidate            ...            }        }        ...        private void postIfNeededLocked() {            if (!mPosted) {                mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null);                mPosted = true;            }        }

仅贴了关键代码做为分析。 
我们看到InvalidateOnAnimationRunnable 本质上是一个Runnable ,run()方法中实际执行view.invalidate方法,也就是说现在的问题是run()怎么执行的,什么时候执行的?! 
这里将InvalidateOnAnimationRunnable 做为参数传给了mChoreographer对象,也就是这里的逻辑就完成了,剩下的invalidate任务交给了mChoreographer。 
其中mPosted为true时表示已经通知了mChoreographer来处理刷新的任务,但任务还没有真正执行,也就在有新的view刷新任务添加时,不再重复通知了。
Choreographer逻辑很多,我找到了最终的执行代码:
[code]void doCallbacks(int callbackType, long frameTimeNanos) {        CallbackRecord callbacks;        synchronized (mLock) {            // We use "now" to determine when callbacks become due because it's possible            // for earlier processing phases in a frame to post callbacks that should run            // in a following phase, such as an input event that causes an animation to start.            final long now = SystemClock.uptimeMillis();            callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(now);            if (callbacks == null) {                return;            }            mCallbacksRunning = true;        }        try {            for (CallbackRecord c = callbacks; c != null; c = c.next) {                if (DEBUG) {                    Log.d(TAG, "RunCallback: type=" + callbackType                            + ", action=" + c.action + ", token=" + c.token                            + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime));                }                c.run(frameTimeNanos);            }        } finally {            synchronized (mLock) {                mCallbacksRunning = false;                do {                    final CallbackRecord next = callbacks.next;                    recycleCallbackLocked(callbacks);                    callbacks = next;                } while (callbacks != null);            }        }    }

c.run(frameTimeNanos);这里也就是对应InvalidateOnAnimationRunnable 的run()方法。 
c是CallbackRecord的实例,CallbackRecord里拥有InvalidateOnAnimationRunnable的实例,即下面代码中的action;
[code]private static final class CallbackRecord {        public CallbackRecord next;        public long dueTime;        public Object action; // Runnable or FrameCallback        public Object token;        public void run(long frameTimeNanos) {            if (token == FRAME_CALLBACK_TOKEN) {                ((FrameCallback)action).doFrame(frameTimeNanos);            } else {                ((Runnable)action).run();            }        }    }

c.run(frameTimeNanos);中有个时间参数,但 ((Runnable)action).run();是没有用到的,可以不关心。 
所以现在找定义执行该函数时间的地方。 
doCallbacks被doFrame方法调用,doFrame方法执行是在handleMessage中:
[code]private final class FrameHandler extends Handler {        public FrameHandler(Looper looper) {            super(looper);        }        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case MSG_DO_FRAME:                    doFrame(System.nanoTime(), 0);                    break;                case MSG_DO_SCHEDULE_VSYNC:                    doScheduleVsync();                    break;                case MSG_DO_SCHEDULE_CALLBACK:                    doScheduleCallback(msg.arg1);                    break;            }        }    }

而发送MSG_DO_FRAME消息的代码为:
[code]//上一帧的时间+delay(sFrameDelay=10)与当前时间取最大值,单位为msfinal long nextFrameTime = Math.max(                        mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now);                if (DEBUG) {                    Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms.");                }                Message msg = mHandler.obtainMessage(MSG_DO_FRAME);                msg.setAsynchronous(true);                mHandler.sendMessageAtTime(msg, nextFrameTime);

那么执行时间就由nextFrameTime来决定了。也就是从当前时间开始的10ms内(理论情况)。 
这里是没有使用Vsync同步机制的情况,使用Vsync会增加一些另外的操作,但是最终执行逻辑是一样的,详细的分析可参考Android系统Choreographer机制实现过程

总结一下:postInvalidate方法是将任务添加到队列中排队后立即执行的,而postInvalidateOnAnimation 依赖上一帧动画的的执行时间,因为动画的刷新是存在一个频率的,直到下一帧动画的时间才会真正执行刷新操作。

android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。     Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。     Android程序中可以使用的界面刷新方法有两种,分别是利用Handler和利用postInvalidate()来实现在线程中刷新界面。 利用invalidate()刷新界面     实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新;而在线程中通过sendMessage发送界面更新消息。Java代码  // 在onCreate()中开启线程       new  Thread( new  GameThread()).start();、      // 实例化一个handler       Handler myHandler = new  Handler() {          // 接收到消息后处理           public   void  handleMessage(Message msg) {              switch  (msg.what) {              case  Activity01.REFRESH:                  mGameView.invalidate(); // 刷新界面                   break ;              }              super .handleMessage(msg);          }      };      class  GameThread  implements  Runnable {          public   void  run() {              while  (!Thread.currentThread().isInterrupted()) {                  Message message = new  Message();                  message.what = Activity01.REFRESH;                  // 发送消息                   Activity01.this .myHandler.sendMessage(message);                  try  {                      Thread.sleep(100 );                  } catch  (InterruptedException e) {                      Thread.currentThread().interrupt();                  }              }          }      }  [java] view plaincopy// 在onCreate()中开启线程      new Thread(new GameThread()).start();、      // 实例化一个handler      Handler myHandler = new Handler() {          // 接收到消息后处理          public void handleMessage(Message msg) {              switch (msg.what) {              case Activity01.REFRESH:                  mGameView.invalidate(); // 刷新界面                  break;              }              super.handleMessage(msg);          }      };      class GameThread implements Runnable {          public void run() {              while (!Thread.currentThread().isInterrupted()) {                  Message message = new Message();                  message.what = Activity01.REFRESH;                  // 发送消息                  Activity01.this.myHandler.sendMessage(message);                  try {                      Thread.sleep(100);                  } catch (InterruptedException e) {                      Thread.currentThread().interrupt();                  }              }          }      }  使用postInvalidate()刷新界面     使用postInvalidate则比较简单,不需要handler,直接在线程中调用postInvalidate即可。 Java代码  class  GameThread  implements  Runnable {          public   void  run() {              while  (!Thread.currentThread().isInterrupted()) {                  try  {                      Thread.sleep(100 );                  } catch  (InterruptedException e) {                      Thread.currentThread().interrupt();                  }                  // 使用postInvalidate可以直接在线程中更新界面                   mGameView.postInvalidate();              }          }      }

3 0
原创粉丝点击