invalidate()得在UI线程中被调动,在工作者线程中可以通过Handler来通知UI线程进行界面更新。
// 实例化一个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则比较简单,不需要handler,直接在线程中调用postInvalidate即可。
class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 使用postInvalidate可以直接在线程中更新界面
mGameView.postInvalidate();
}
}
}
postInvalidateOnAnimation会在下一个Frame开始的时候,发起一些invalidate操作,
mInvalidateOnAnimationRunnable 是一个 InvalidateOnAnimationRunnable:
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);
}
}
InvalidateOnAnimationRunnable本质上是一个Runnable ,run()方法中实际执行view.invalidate方法,也就是说现在的问题是run()怎么执行的,什么时候执行的?!
这里将InvalidateOnAnimationRunnable 做为参数传给了mChoreographer对象,也就是这里的逻辑就完成了,剩下的invalidate任务交给了mChoreographer。
其中mPosted为true时表示已经通知了mChoreographer来处理刷新的任务,但任务还没有真正执行,也就在有新的view刷新任务添加时,不再重复通知了。
c.run(frameTimeNanos);这里也就是对应InvalidateOnAnimationRunnable的run()方法。
c是CallbackRecord的实例,CallbackRecord里拥有InvalidateOnAnimationRunnable的实例,即下面代码中的action;
c.run(frameTimeNanos);中有个时间参数,但((Runnable)action).run();是没有用到的,可以不关心。
所以现在找定义执行该函数时间的地方。
doCallbacks被doFrame方法调用,doFrame方法执行是在handleMessage中:
而发送MSG_DO_FRAME消息的代码为:
那么执行时间就由nextFrameTime来决定了。也就是从当前时间开始的10ms内(理论情况)。
这里是没有使用Vsync同步机制的情况,使用Vsync会增加一些另外的操作,但是最终执行逻辑是一样的,详细的分析可参考Android系统Choreographer机制实现过程。
总结一下:
postInvalidate方法是将任务添加到队列中排队后立即执行的,而postInvalidateOnAnimation依赖上一帧动画的的执行时间,因为动画的刷新是存在一个频率的,直到下一帧动画的时间才会真正执行刷新操作。
而postIfNeededLocked()干的事情就是把mInvalidateOnAnimationRunnable作为Choreographer.CALLBACK_ANIMATION(这个类型的task会在mesaure/layout/draw之前被运行)的Task 交给 UI线程的Choreographer.
View的invalidate会进一步触发ViewRootImpl的invalidateChildInParent()->invalidate()<一种情况(dirty == null 表示全部重绘),不过另外一种差不多>:
不过虽然没有发出去,不代表这次invalidate就不会生效,因为前面的invalidate()里已经设置了mDirty了: