为什么触摸事件会破坏我的 Android 帧速率?
我正在开发一款 Android 游戏。 虽然发生了很多事情,但运行相当顺利。 当然,直到用户触摸屏幕。
当他们触摸它时,会调用 onTouchEvent
(使用 action = ACTION_MOVE
、x = 0
和 y = 0
>)大约每十毫秒一次,这似乎是相当高的优先级,因为它绝对会消除帧速率。 一旦触摸结束,帧速率就会恢复到良好的状态。
我尝试
- 让
onTouchEvent
像往常一样处理游戏的输入, - 让
onTouchEvent
立即返回true
而 - 没有
onTouchEvent
完全实施
该问题在所有三种情况下都存在。
有人遇到过这种情况吗? 有没有办法降低 ACTION_MOVE 事件的生成速率,或者确保它们仅在实际移动时生成,或者使用仅获取当前位置的轮询方法触碰? 或者只是一种完全禁用它的方法?
I'm developing a game for Android. It's got a lot going on but is running reasonably smoothly. That is, of course, until the user touches the screen.
While they're touching it, onTouchEvent
is called (with action = ACTION_MOVE
, x = 0
and y = 0
) roughly once every ten milliseconds at what appears to be a fairly high priority, as it absolutely obliterates the framerate. As soon as the touch ends the framerate returns to its nice state.
I've tried
- having
onTouchEvent
handle input for the game as usual - having
onTouchEvent
returntrue
straight away - not having
onTouchEvent
implemented at all
The problem persists in all three situations.
Has anyone encountered this? Is there a way to reduce the rate at which ACTION_MOVE
events are generated, or to ensure that they're only generated when there is actual movement, or use a polling method that just gets the current location of the touch? Or even just a way to disable it entirely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
阅读此帖子。 基本上,您希望使事件线程休眠,否则系统将泵送大量您需要处理的事件(在 x、y 和压力之间总是发生一些运动)。
Read this thread. Basically you want to sleep the event thread since otherwise the system will pump a lot of events (between x,y and pressure there is always some movement going on) that you need to handle.
这是我的代码的样子。 在 UI 线程上:
和在 Game 线程上:
Here's what my code looks like. On the UI thread:
and on the Game thread:
也许这是一个比较明显的解决方案,但是...您是否尝试过只处理其中的大约 1/10? 如果您正在执行 UI 线程上每 10 毫秒触发一次的处理,那么这可能会降低帧速率,是的。 那么,如果您只是累积一个计数器,并且仅在超过某个最小阈值后才进行任何处理,该怎么办?
Perhaps a somewhat obvious solution, but... have you tried only handling about 1/10 of them? If you're doing processing that's triggered every 10ms on the UI thread, that's likely going to slow down the framerate, yes. So what if you just accumulate a counter somewhat instead and only do any processing once that's gotten past some minimal threshold?
如果您想要一个无需处理同步线程的解决方案,我建议您使用 Handler 接口,使用 Message/Bundle 将事件和相关数据发送到游戏渲染线程。 这里暗示的睡眠解决方法仍然适用。
If you want a solution without having to deal with synchronizing threads I can recommend using the Handler interface to send the event and relevant data using a Message/Bundle to the game rendering thread. The sleep workaround hinted here still applies.
通常,事件带有时间戳属性,因此很容易计算和限制事件速率。
Generally events comes with a timestamp property so it's easy to compute and throttle event rate.