您的位置:首页 > 其它

postInvalidate postInvalidateOnAnimation 区别

2016-04-20 13:09 225 查看
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 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之后做了什么:

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逻辑很多,我找到了最终的执行代码:

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;

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中:

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消息的代码为:

//上一帧的时间+delay(sFrameDelay=10)与当前时间取最大值,单位为ms
final 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 依赖上一帧动画的的执行时间,因为动画的刷新是存在一个频率的,直到下一帧动画的时间才会真正执行刷新操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: