Android,处理特殊的 TouchEvent 场景
好吧,在业余时间创建游戏时,我在尝试围绕以下场景进行编码时完全陷入困境:
我想捕获点击事件(TouchEvent.ACTION_DOWN
),然后在 500 毫秒内捕获每隔一段时间,我想检查屏幕是否仍然被按下,并无限地重复相同的操作。
所以,我开始:
switch (myTouchHandler.getAction()) {
case (TouchEvent.ACTION_DOWN):
case (TouchEvent.ACTION_MOVE):
if (inputIntervalTooSoon()) return true;
//do the magic i intend to do....
}
....
private boolean inputIntervalTooSoon() {
final long now = System.currentTimeMillis();
if ((now - lastTouchEventTime) < 500) return false;
lastTouchEventTime = now;
return true;
这会产生这样的效果:它将等待 500 毫秒,但它不会立即开始检测我的手指是否仍然向下。如果我稍微动一下手指,它就会再次拾起它,然后回到休眠状态。
如果我不包含 Interval 函数,我的动作就会不断触发。任何关于如何更好地实施这一点的想法将不胜感激。
Ok, so in creating a game in my spare time, I've been completely stuck when trying to code around the following scenario:
I want to capture tap events (TouchEvent.ACTION_DOWN
), and then in 500ms intervals, I want to check to see if the screen is still pressed, and repeat the same action, ad infinitum.
So, I started out with:
switch (myTouchHandler.getAction()) {
case (TouchEvent.ACTION_DOWN):
case (TouchEvent.ACTION_MOVE):
if (inputIntervalTooSoon()) return true;
//do the magic i intend to do....
}
....
private boolean inputIntervalTooSoon() {
final long now = System.currentTimeMillis();
if ((now - lastTouchEventTime) < 500) return false;
lastTouchEventTime = now;
return true;
And this produces the effect that It will wait the 500ms, but it doesn't begin to immediately detect if my finger is still down. It will pick it up again if I move my finger slightly, and then go back to dormant.
If I dont include the Interval function, my action just fires constantly. Any ideas in how to better implement this would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以想象一个在 TouchEvent.ACTION_DOWN 上启动的计时线程,该线程将在休眠 500 毫秒的循环内启动并运行。睡眠后,它将能够执行检查,这将执行您的自定义处理以及检查 TouchEvent.ACTION_UP 是否发生(您必须在 EventHandler 中记录)。
当然,需要一些同步。
这有道理吗?
类似这样的事情:
I could imagine a Timing thread that is started on a TouchEvent.ACTION_DOWN, which would be started and run inside a loop that sleeps for 500ms. After the sleep it would then be able to perform its check, which would do your custom processing as well as checking whether or not TouchEvent.ACTION_UP happened or not (which you would have to record in the EventHandler).
Of course there is a bit of synchronization that would be required.
Does this make sense?
Something along the lines of this:
屏幕将被按下,直到发生带有 ACTION_UP 或 ACTION_CANCEL 操作的事件,所以我会进行相反的检查。
The screen will be pressed until you have an event with either an ACTION_UP or ACTION_CANCEL actions, so I would check inversely.