文章目录
- 实时FPS
实时FPS
初始化
choreographer = Choreographer.getInstance();lastFrameTimeNanos = System.nanoTime();choreographer.postFrameCallback(frameCallback);
监听并显示
Choreographer.FrameCallback frameCallback = new Choreographer.FrameCallback() {@Overridepublic void doFrame(long frameTimeNanos) {choreographer.postFrameCallback(this);long now = System.currentTimeMillis();long diff = frameTimeNanos - lastFrameTimeNanos;if (diff > 0) {fps = 1e9 / diff;lastFrameTimeNanos = frameTimeNanos;// 每秒刷新一次if (now - longFpsTime > 1000){// 更新UIgetActivity().runOnUiThread(new Runnable() {@Overridepublic void run() {tvFps.setText(String.format("FPS: %.2f", fps));}});longFpsTime = now;}}}};