Android 代码获取帧率FPS

 private long mStartFrameTIme = 0;
    private int mFrameCount = 0;
    private static final long MONITOR_INTERVAL = 160L;
    private static final long MONITOR_INTERVAL_NANOS = MONITOR_INTERVAL * 1000 * 1000;
    private void getFps() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            return;
        }
        Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
            @Override
            public void doFrame(long frameTimeNanos) {
                if (mStartFrameTIme == 0) {
                    mStartFrameTIme = frameTimeNanos;
                }
                float interval = (frameTimeNanos - mStartFrameTIme) / 1000000.0f;
                if (interval > MONITOR_INTERVAL) {
                    double fps = ((double) mFrameCount * 1000L) / interval;
                    Log.e("test", "fps:" + fps);
                    mFrameCount = 0;
                    mStartFrameTIme = 0;
                } else {
                    ++mFrameCount;
                }
                Choreographer.getInstance().postFrameCallback(this);
            }
        });
    }

你可能感兴趣的:(Android 代码获取帧率FPS)