gpt4 book ai didi

Android:如何找到设备的帧率?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:54 25 4
gpt4 key购买 nike

帧速率:我指的是显示变化的速率。即调用 Ondraw() 并重新绘制 Canvas 。

是否有适用于所有 Android 设备的默认费率?由于此速率取决于设备的处理能力,如何在开始为该移动设备编程之前找出该设备的帧速率?

最佳答案

这可能是 this question 的后续行动,在那里我建议有一个只是一遍又一遍地绘制的重绘循环可能有点过分。可能有一个 api 可以找出设备显示的功能,但如果有的话我不知道。当您编写自己的事件循环/线程函数时,您可以通过调用“绘制”方法的频率来控制帧率。通常,我认为对于大多数用途而言,您可以使用 30 左右的刷新率。如果您正在编写一款需要快速动画的快速 Action 游戏,那么您可能希望尽可能快地运行,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) {
}
}
}

关于Android:如何找到设备的帧率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5672503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com