下面列出了android.view.Choreographer#FrameCallback ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* GLコンテキスト上で実行されるChoreographer.FrameCallbackをpostする
* @param callback
* @param delayMs
* @throws IllegalStateException
*/
public synchronized void postFrameCallbackDelayed(
@NonNull final Choreographer.FrameCallback callback,
final long delayMs) throws IllegalStateException {
if (DEBUG) Log.v(TAG, "postFrameCallbackDelayed:");
checkValid();
if (isGLThread()) {
// すでにGLスレッド上であれば直接実行
Choreographer.getInstance().postFrameCallbackDelayed(callback, delayMs);
} else {
// 別スレッド上にいるならGLスレッド上へ投げる
mGLHandler.post(new Runnable() {
@Override
public void run() {
Choreographer.getInstance().postFrameCallbackDelayed(callback, delayMs);
}
});
}
}
/**
* 未実行のChoreographer.FrameCallbackがあれば取り除く
* @param callback
* @throws IllegalStateException
*/
public synchronized void removeFrameCallback(
@NonNull final Choreographer.FrameCallback callback)
throws IllegalStateException {
if (DEBUG) Log.v(TAG, "removeFrameCallback:");
checkValid();
if (isGLThread()) {
// すでにGLスレッド上であれば直接実行
Choreographer.getInstance().removeFrameCallback(callback);
} else {
// 別スレッド上にいるならGLスレッド上へ投げる
mGLHandler.post(new Runnable() {
@Override
public void run() {
Choreographer.getInstance().removeFrameCallback(callback);
}
});
}
}
/**
* Constructs a VSyncMonitor
* @param context The application context.
* @param listener The listener receiving VSync notifications.
*/
public VSyncMonitor(Context context, VSyncMonitor.Listener listener) {
mListener = listener;
float refreshRate = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getRefreshRate();
final boolean useEstimatedRefreshPeriod = refreshRate < 30;
if (refreshRate <= 0) refreshRate = 60;
mRefreshPeriodNano = (long) (NANOSECONDS_PER_SECOND / refreshRate);
mChoreographer = Choreographer.getInstance();
mVSyncFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
TraceEvent.begin("VSync");
if (useEstimatedRefreshPeriod && mConsecutiveVSync) {
// Display.getRefreshRate() is unreliable on some platforms.
// Adjust refresh period- initial value is based on Display.getRefreshRate()
// after that it asymptotically approaches the real value.
long lastRefreshDurationNano = frameTimeNanos - mGoodStartingPointNano;
float lastRefreshDurationWeight = 0.1f;
mRefreshPeriodNano += (long) (lastRefreshDurationWeight
* (lastRefreshDurationNano - mRefreshPeriodNano));
}
mGoodStartingPointNano = frameTimeNanos;
onVSyncCallback(frameTimeNanos, getCurrentNanoTime());
TraceEvent.end("VSync");
}
};
mGoodStartingPointNano = getCurrentNanoTime();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Choreographer.FrameCallback getFrameCallback() {
if (mFrameCallback == null) {
mFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
FrameCallback.this.doFrame(frameTimeNanos);
}
};
}
return mFrameCallback;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Choreographer.FrameCallback getFrameCallback() {
if (mFrameCallback == null) {
mFrameCallback =
new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
ChoreographerCompat.FrameCallback.this.doFrameInternal(frameTimeNanos);
}
};
}
return mFrameCallback;
}
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
mChoreographer = choreographer;
mFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
if (!mStarted || mSpringSystem == null) {
return;
}
long currentTime = SystemClock.uptimeMillis();
mSpringSystem.loop(currentTime - mLastTime);
mLastTime = currentTime;
mChoreographer.postFrameCallback(mFrameCallback);
}
};
}
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
mChoreographer = choreographer;
mFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
if (!mStarted || mSpringSystem == null) {
return;
}
long currentTime = SystemClock.uptimeMillis();
mSpringSystem.loop(currentTime - mLastTime);
mLastTime = currentTime;
mChoreographer.postFrameCallback(mFrameCallback);
}
};
}
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
mChoreographer = choreographer;
mFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
if (!mStarted || mSpringSystem == null) {
return;
}
mSpringSystem.loop();
mChoreographer.postFrameCallback(mFrameCallback);
}
};
}
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
mChoreographer = choreographer;
mFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
if (!mStarted || mSpringSystem == null) {
return;
}
mSpringSystem.loop();
mChoreographer.postFrameCallback(mFrameCallback);
}
};
}
public ChoreographerAndroidSpringLooper(Choreographer choreographer) {
mChoreographer = choreographer;
mFrameCallback = new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
if (!mStarted || mSpringSystem == null) {
return;
}
long currentTime = SystemClock.uptimeMillis();
mSpringSystem.loop(currentTime - mLastTime);
mLastTime = currentTime;
mChoreographer.postFrameCallback(mFrameCallback);
}
};
}
@Override
public void postFrameCallback(Choreographer.FrameCallback callback) {
mChoreographer.postFrameCallback(callback);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void choreographerPostFrameCallback(Choreographer.FrameCallback frameCallback) {
mChoreographer.postFrameCallback(frameCallback);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void choreographerPostFrameCallbackDelayed(
Choreographer.FrameCallback frameCallback,
long delayMillis) {
mChoreographer.postFrameCallbackDelayed(frameCallback, delayMillis);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void choreographerPostFrameCallback(Choreographer.FrameCallback frameCallback) {
mChoreographer.postFrameCallback(frameCallback);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void choreographerPostFrameCallbackDelayed(
Choreographer.FrameCallback frameCallback, long delayMillis) {
mChoreographer.postFrameCallbackDelayed(frameCallback, delayMillis);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void choreographerRemoveFrameCallback(Choreographer.FrameCallback frameCallback) {
mChoreographer.removeFrameCallback(frameCallback);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void choreographerPostFrameCallback(Choreographer.FrameCallback frameCallback) {
mChoreographer.postFrameCallback(frameCallback);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void choreographerRemoveFrameCallback(Choreographer.FrameCallback frameCallback) {
mChoreographer.removeFrameCallback(frameCallback);
}
@Override
public void postFrameCallback(Choreographer.FrameCallback callback) {
mChoreographer.postFrameCallback(callback);
}
void postFrameCallback(Choreographer.FrameCallback callback);