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);
}
本文详细介绍了如何在Android应用中计算每秒绘制的帧数(FPS),通过在`onDraw()`方法中进行逻辑计算,记录绘制次数,并在合适的时间间隔内计算出FPS值。具体实现过程包括在每次绘制后增加帧计数,在达到一秒时间间隔时计算FPS并更新UI显示。
543

被折叠的 条评论
为什么被折叠?



