获取帧率
参考:android.view.ViewRootImpl#draw
里的 trackFPS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class FpsCounter {
private static final long MONITOR_INTERVAL = 160L; private static final long MONITOR_INTERVAL_NANOS = MONITOR_INTERVAL * 1000L * 1000L; private static final long MAX_INTERVAL = 1000L;
private long mStartFrameTime = 0; private int mFrameCount = 0;
private void getFPS() { Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() { @Override public void doFrame(long frameTimeNanos) { if (mStartFrameTime == 0) { mStartFrameTime = frameTimeNanos; } long interval = frameTimeNanos - mStartFrameTime; if (interval > MONITOR_INTERVAL_NANOS) { double fps = (((double) (mFrameCount * 1000L * 1000L)) / interval) * MAX_INTERVAL; mFrameCount = 0; mStartFrameTime = 0; } else { ++mFrameCount; }
Choreographer.getInstance().postFrameCallback(this); } }); } }
|