Android,处理特殊的 TouchEvent 场景

发布于 2024-11-06 05:53:15 字数 738 浏览 2 评论 0原文

好吧,在业余时间创建游戏时,我在尝试围绕以下场景进行编码时完全陷入困境:

我想捕获点击事件(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-11-13 05:53:15

我可以想象一个在 TouchEvent.ACTION_DOWN 上启动的计时线程,该线程将在休眠 500 毫秒的循环内启动并运行。睡眠后,它将能够执行检查,这将执行您的自定义处理以及检查 TouchEvent.ACTION_UP 是否发生(您必须在 EventHandler 中记录)。

当然,需要一些同步。

这有道理吗?

类似这样的事情:

boolean upHappened;

switch (myTouchHandler.getAction()) {
  case (TouchEvent.ACTION_DOWN):
  upHappened = false;
  new Thread(new TimerRunnable()).start();
  case (TouchEvent.ACTION_UP):
  case (TouchEvent.ACTION_CANCEL):
  upHappened = true;
}

class TimerRunnable implements Runnable {
  public void run() {
    while(true) {
      Thread.sleep(500);
      if(upHappened) break;
      //custom processing
    }
  }
}

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:

boolean upHappened;

switch (myTouchHandler.getAction()) {
  case (TouchEvent.ACTION_DOWN):
  upHappened = false;
  new Thread(new TimerRunnable()).start();
  case (TouchEvent.ACTION_UP):
  case (TouchEvent.ACTION_CANCEL):
  upHappened = true;
}

class TimerRunnable implements Runnable {
  public void run() {
    while(true) {
      Thread.sleep(500);
      if(upHappened) break;
      //custom processing
    }
  }
}
梦巷 2024-11-13 05:53:15

屏幕将被按下,直到发生带有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文