/**
* This custom, static handler handles the timing pulse that is shared by all active
* ValueAnimators. This approach ensures that the setting of animation values will happen on the
* same thread that animations start on, and that all animations will share the same timesfor
* calculating their values, which makes synchronizing animations possible.
*
* The handler uses the Choreographer by default for doing periodic callbacks. A custom
* AnimationFrameCallbackProvider can be set on the handler to provide timing pulse that
* may be independent of UI frame update. This could be useful in testing.
*
* @hide
*/
public class AnimationHandler{
复制代码
scheduleVsync(): Schedules a single vertical sync pulse. 用白话说就是,下次v-sync的时候通知我(回调onVsync)。
onVsync(): Called when a vertical sync pulse is received.
/**
* Provides a low-level mechanism for an application to receive display events
* such as vertical sync.
* ...
*/
public abstract class DisplayEventReceiver {
public DisplayEventReceiver(Looper looper, int vsyncSource) {
// ...
mReceiverPtr = nativeInit(new WeakReference<DisplayEventReceiver>(this), mMessageQueue,
vsyncSource);
//...
}
public void scheduleVsync() {
// ...
nativeScheduleVsync(mReceiverPtr);
}
// Called from native code.
private void dispatchVsync(long timestampNanos, int builtInDisplayId, int frame) {
onVsync(timestampNanos, builtInDisplayId, frame);
}
/**
* Called when a vertical sync pulse is received.
* The recipient should render a frame and then call {@link #scheduleVsync}
* to schedule the next vertical sync pulse.
public void onVsync(long timestampNanos, int builtInDisplayId, int frame) {
}
复制代码
FrameDisplayEventReceiver的源码
private final class FrameDisplayEventReceiver extends DisplayEventReceiver
implements Runnable {
// ...
@Override
public void onVsync(long timestampNanos, int builtInDisplayId, int frame) {
// ...
Message msg = Message.obtain(mHandler, this);
msg.setAsynchronous(true);
mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS);
}
@Override
public void run() {
mHavePendingVsync = false;
doFrame(mTimestampNanos, mFrame);
}
}
复制代码
void doFrame(long frameTimeNanos, int frame) {
final long startNanos;
synchronized (mLock) {
// ...
if (frameTimeNanos < mLastFrameTimeNanos) {
//...
scheduleVsyncLocked();
return;
}
}
private void scheduleVsyncLocked() {
mDisplayEventReceiver.scheduleVsync();
}
复制代码