帧速率:我指的是显示变化的速率。即调用 Ondraw() 并重新绘制画布。
所有 Android 设备都有默认费率吗?由于该速率取决于设备的处理能力,因此在开始为该移动设备编程之前如何找出该设备的帧速率?
这可能是后续这个问题 https://stackoverflow.com/questions/5650810/canvas-and-surfaceview-example-crash-freeze-memory-leak/5659778#5659778,我建议使用一个不断重复绘制的重绘循环可能有点过度。可能有一个 api 可以找出设备显示的功能,但如果有的话我不知道。当您编写自己的事件循环/线程函数时,您可以通过调用“draw”方法的频率来控制帧速率。通常情况下,我认为对于大多数用途来说,刷新率在 30 左右就可以了。如果您正在编写快速动作游戏,需要快速动画,那么您may想要跑得尽可能快,fps越高,就越流畅。
典型的事件循环(线程运行函数)可能如下所示:
// define the target fps
private static final int UPDATE_RATE = 30; // Frames per second (fps)
public void run() {
while(running) { // volatile flag, set somewhere else to shutdown
long beginTimeMillis, timeTakenMillis, timeLeftMillis;
// get the time before updates/draw
beginTimeMillis = System.currentTimeMillis();
// do the thread processing / draw
performUpdates(); // move things if required
draw(); // draw them on the screen
// get the time after processing and calculate the difference
timeTakenMillis = System.currentTimeMillis() - beginTimeMillis;
// check how long there is until we reach the desired refresh rate
timeLeftMillis = (1000L / UPDATE_RATE) - timeTakenMillis;
// set some kind of minimum to prevent spinning
if (timeLeftMillis < 5) {
timeLeftMillis = 5; // Set a minimum
}
// sleep until the end of the current frame
try {
TimeUnit.MILLISECONDS.sleep(timeLeftMillis);
} catch (InterruptedException ie) {
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)