同时在屏幕的两个部分上处理触摸事件 [英] Handle Touch Event on both segments of the screen simultaneously

查看:86
本文介绍了同时在屏幕的两个部分上处理触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个触摸事件,它一个接一个地处理 ACTION_DOWN ACTION_UP .例如,如果我单击屏幕的左半部分,则 ACTION_DOWN 起作用,但 ACTION_UP 却不起作用,并且在屏幕的右侧也是如此.我想同时处理两个.如果我同时单击左侧和右侧,则两个事件都应执行.谁能帮我这个忙.我的代码是

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() < 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 ++;} 我希望ot这样的代码可以同时处理这两个代码.

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() > screenX / 2f) { flight.toShoot++; } I want ot do such a code that both of them are handled at the same time.

推荐答案

这是处理多点触摸手势的文档.

here is the docs to handle multi touch gestures.

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

这是处理基于Google文档的多点触控的非常简单的示例.

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("The finger # " + index + " is " + getAction(action));
    return true;
}

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

这篇关于同时在屏幕的两个部分上处理触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆