我做了这个小代码:
public class Game1Activity extends Activity {
/** Called when the activity is first created. */
private final Handler handler = new Handler();
private final Runnable run = new Runnable()
{
public void run(){
update();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
update();
}
public void update(){
try
{
Log.i("update thread", "updated");
}
catch (Exception e)
{
Log.i("update thread", "catched an exception!");
}
finally
{
handler.postDelayed(run, 33);
}
}
}
如您所见,除了它在日志聊天中写入更新外,没有什么特别之处,每 33 毫秒(30 fps)。
这是我的问题,如果你可以从日志聊天中看到:
09-15 09:01:05.955: I/update thread(423): updated
09-15 09:01:06.025: I/update thread(423): updated
09-15 09:01:06.097: I/update thread(423): updated
09-15 09:01:06.166: I/update thread(423): updated
09-15 09:01:06.236: I/update thread(423): updated
09-15 09:01:06.305: I/update thread(423): updated
09-15 09:01:06.374: I/update thread(423): updated
09-15 09:01:06.444: I/update thread(423): updated
09-15 09:01:06.513: I/update thread(423): updated
线程大约每 70 毫秒运行一次,是预期时间的两倍! 为什么会这样?我的线程是最基本和最简单的,我怀疑是否有什么可以延迟它
有没有更好的方法让它完全按预期更新?
最佳答案
处理器需要为 Threading 切换内容。 33 毫秒有点低。
如果您使用 300 毫秒,这是一个可接受的值
handler.postDelayed();
如果您想要 30 fps,请尝试延迟 30 毫秒,而不是 1 毫秒。
关于android - 关于线程和延迟时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12436022/