简体   繁体   English

在surfaceview(android)中执行sleep()onTouchEvent或Control frame rate

[英]execute sleep() onTouchEvent or Control frame rate in surfaceview (android )

Please check this link . 请检查此链接 Following code is from this link. 以下代码来自此链接。 I am not sure why it says we have to use thread sleep to limit number of touch events. 我不确定为什么会说我们必须使用线程睡眠来限制触摸事件的数量。 I have a surfaceview and gamethread which processes events pushed by touch event. 我有一个Surfaceview和gamethread,它处理触摸事件推送的事件。 Is this sleep required or should I handle the sleep in gamethread instead by controlling framerate. 是否需要这种睡眠,还是应该通过控制帧速率来在游戏线程中处理睡眠?

 try {
    Thread.sleep(16);
    } catch (InterruptedException e) {
    }
    return true;
    } 

whole code 整个代码

@Override
public boolean onTouchEvent(MotionEvent event) {
// we only care about down actions in this game.
try {
// history first
int hist = event.getHistorySize();
if (hist > 0) {
// add from oldest to newest
for (int i = 0; i < hist; i++) {
InputObject input = inputObjectPool.take();
input.useEventHistory(event, i);
gameThread.feedInput(input);
}
}
// current last
InputObject input = inputObjectPool.take();
input.useEvent(event);
gameThread.feedInput(input);
} catch (InterruptedException e) {
}
// don't allow more than 60 motion events per second
try {
Thread.sleep(16);
} catch (InterruptedException e) {
}
return true;
}

Your link points to an article that has been posted in 2009, you should really try to find one that is more up to date. 您的链接指向的是2009年发布的一篇文章,您实际上应该尝试查找更新的文章。

The code itself looks wrong, the author explains that his code is designed to avoid blocking the UI thread but then calls Thread.sleep(16) in onTouchEvent() which is called from the UI thread and blocks it. 代码本身看起来是错误的,作者解释说,他的代码旨在避免阻塞UI线程,但随后在onTouchEvent()调用Thread.sleep(16),该函数从UI线程调用并阻塞了该线程。

What about this example? 那这个例子呢? I haven't read it in detail, but the code looks much better : https://www.codeproject.com/Articles/827608/Android-Basic-Game-Loop 我没有详细阅读,但是代码看起来更好: https : //www.codeproject.com/Articles/827608/Android-Basic-Game-Loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM