1.先看postInvalidate方法执行过程
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);
}
}
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的执行代码:
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_INVALIDATE:
((View) msg.obj).invalidate();
break;
...
代码中处理消息时view.invalidate方法是立即执行的(排除其它原因:排队、阻塞、CPU抢占等)。
也就是说postInvalidate()实际将invalidate任务加入队列中,理想民的 不考虑排队之类的情况,可以看成是立即执行的。
2.再来看postInvalidateOnAnimation的执行过程
public void postInvalidateOnAnimation() {
// We try only with the AttachInfo because there's no point in invalidating
// if we are not attache