FPS : frame per second 刷新率 ,每秒绘制的帧数。
如何计算:
在onDraw() 方法中进行逻辑计算
每调用一次onDraw() 方法,则frame 加 1;
因为绘制每帧的时间不同,所以
当时间刚好大于1秒的时候,进行计算。
如绘制了60帧,刚好用了1.2秒
则 fps = 60 / 1.2;
fps = 50 帧/秒;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//耗时的绘制
draw。。。。。。。。
// fps counter: count how many frames we draw and once a second calculate the
// frames per second
++frames;
long nowTime = System.currentTimeMillis();
long deltaTime = nowTime - startTime;
if (deltaTime > 1000) {
float secs = (float) deltaTime / 1000f;
fps = (float) frames / secs;
fpsString = "fps: " + fps;
startTime = nowTime;
frames = 0;
}
canvas.drawText(fpsString, getWidth() - 200, getHeight() - 80, textPaint);
}