处理屏幕两个部分的触摸事件同时发生

huangapple go评论83阅读模式
英文:

Handle Touch Event on both segments of the screen simultaneously

问题

我有一个触摸事件,它逐个处理ACTION_DOWNACTION_UP。例如,如果我点击屏幕的左半边,ACTION_DOWN可以工作,但ACTION_UP不起作用,右边的情况也是如此。我希望能同时处理两者。如果我同时点击左边和右边,两个事件都应该执行。有人可以帮助我吗?

我的代码是:

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //如果用户按下屏幕
            if (event.getX() < screenX / 2f) {
                flight.isGoingUp = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            flight.isGoingUp = false;
            if (event.getX() > screenX / 2f) {
                flight.toShoot++;
            }
            break;
    }
    return true;
}

编辑1
屏幕分为两半。屏幕的左侧用于flight.isGoingUp = true;,当我们释放左侧或点击屏幕的右半边时,flight.isGoingUp = false;if (event.getX() > screenX / 2f) { flight.toShoot++; }。我想编写代码,同时处理这两个事件。

英文:

So I have a touch event and it handles ACTION_DOWN and ACTION_UP one by one. For example if I click on the left half of the screen ACTION_DOWN works but not ACTION_UP and same for the right side of the screen. i want to handle both of them simultaneously. If I click on left side and right side at the same time both the event should perform. Can anyone please help me on this.
My code is

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //if user press the screen
            if (event.getX() &lt; screenX / 2f) {
                flight.isGoingUp = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            flight.isGoingUp = false;
            if (event.getX() &gt; screenX / 2f) {
                flight.toShoot++;
            }
            break;
    }
    return true;
}

Edit 1
The screen is split into two half. The left side of the screen is used for flight.isGoingUp = true; and when we release the left side or tap the right helg of the screen flight.isGoingUp = false; and if (event.getX() &gt; screenX / 2f) {
flight.toShoot++;
}
I want ot do such a code that both of them are handled at the same time.

答案1

得分: 1

以下是处理多点触控手势的文档。

https://developer.android.com/training/gestures/multi#java

以下是一个基于Google文档的处理多点触控的非常简单的示例。

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getActionMasked();
    int index = event.getActionIndex();
    System.out.println("手指 # " + index + " " + getAction(action));
    return true;
}

public static String getAction(int action) {
    switch (action) {
        case MotionEvent.ACTION_DOWN: return "按下";
        case MotionEvent.ACTION_MOVE: return "移动";
        case MotionEvent.ACTION_POINTER_DOWN: return "指针按下";
        case MotionEvent.ACTION_UP: return "释放";
        case MotionEvent.ACTION_POINTER_UP: return "指针释放";
        case MotionEvent.ACTION_OUTSIDE: return "外部";
        case MotionEvent.ACTION_CANCEL: return "取消";
        default: return "未知";
    }
}

希望这对你有帮助。

英文:

here is the docs to handle multi touch gestures.

https://developer.android.com/training/gestures/multi#java

Here is a very simple example to handle multitouch based on google docs.

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getActionMasked();
    int index = event.getActionIndex();
    System.out.println(&quot;The finger # &quot; + index + &quot; is &quot; + getAction(action));
    return true;
}

public static String getAction(int action) {
    switch (action) {
        case MotionEvent.ACTION_DOWN: return &quot;Down&quot;;
        case MotionEvent.ACTION_MOVE: return &quot;Move&quot;;
        case MotionEvent.ACTION_POINTER_DOWN: return &quot;Pointer Down&quot;;
        case MotionEvent.ACTION_UP: return &quot;Up&quot;;
        case MotionEvent.ACTION_POINTER_UP: return &quot;Pointer Up&quot;;
        case MotionEvent.ACTION_OUTSIDE: return &quot;Outside&quot;;
        case MotionEvent.ACTION_CANCEL: return &quot;Cancel&quot;;
        default: return &quot;Unknown&quot;;
    }
}

huangapple
  • 本文由 发表于 2020年7月22日 06:06:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63023789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定