Android多点触控:未检测到MotionEvent.ACTION_POINTER_DOWN

时间:2014-03-18 17:43:10

标签: java android multi-touch motionevent ontouch

这是我的代码的一部分。 我不明白为什么没有检测到MotionEvent.ACTION_POINTER_DOWN(触摸屏幕的第二根手指)。 有人可以解释为什么这不起作用吗?

单人游戏正常。 两个玩家(多点触控)不是。

什么适合两名球员 - 当用户只按下屏幕的一半时,它会调用goUp()

什么对两名球员不起作用 - 当用户按下屏幕的一半时,按下另一半,另一半不调用goUp()

此外,有没有办法在模拟器上一次模拟多个触摸,这样我才能看到log中的logcat

private int pointerId = -1, pointerId2 = -1;

@Override
public boolean onTouch(View view, MotionEvent event) {

    // Depending on the touch, depends on the direction of the ricer (up or down)
    if(!TWO_PLAYER_MODE){
        switch (event.getActionMasked()) {

        // When finger touches the screen, ricer will accelerate up
        case MotionEvent.ACTION_DOWN:
            ricerView.goUp();
            break;

            // When finger is lifted from the screen, ricer will accelerate down
        case MotionEvent.ACTION_UP:
            ricerView.goDown();
        }
    }
    else{

        float touchY = event.getY();
        int pointerIndex = event.getActionIndex();
        int pointerID = event.getPointerId(pointerIndex);

        switch (event.getActionMasked()) {
        // When finger touches the screen, ricer will accelerate up
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            if( touchOnTopOfScreen(touchY) ){
                pointerId = pointerID;
                ricerView.goUp();
                log("RV1 down Id=" + pointerID + " Y-pos=" + touchY);
            }
            else{
                pointerId2 = pointerID;
                ricerView2.goUp();
                log("RV2 down Id=" + pointerID + " Y-pos=" + touchY);
            }
            break;

            // When finger is lifted from the screen, ricer will accelerate down
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            if( pointerId == pointerID ){
                ricerView.goDown();
                pointerId = -1;
                log("RV1 up Id=" + pointerID + " Y-pos=" + touchY);
            }
            if( pointerId2 == pointerID ){
                ricerView2.goDown();
                pointerId2 = -1;
                log("RV2 up Id=" + pointerID + " Y-pos=" + touchY);
            }
        }
    }

    // Delegate the touch to the gestureDetector 
    mGestureDetector.onTouchEvent(event);

    return true;

}   // End of onTouchEvent

private boolean touchOnTopOfScreen(float y){
    if(mDisplayHeight/2 > y)
        return true;
    else
        return false;
}

2 个答案:

答案 0 :(得分:2)

问题在于您处理案件的方式。

    case MotionEvent.ACTION_DOWN: 
    case MotionEvent.ACTION_POINTER_DOWN:
        if( touchOnTopOfScreen(touchY) ){
            pointerId = pointerID;
            ricerView.goUp();
            log("RV1 down Id=" + pointerID + " Y-pos=" + touchY);
        }
        else{//<---should be a separate if-statement
            pointerId2 = pointerID;
            ricerView2.goUp();
            log("RV2 down Id=" + pointerID + " Y-pos=" + touchY);
        }
        break;

在上面,有一个额外指针进入屏幕和第一个指针的动作,但它们在一个if-statement中。这意味着如果一个条件发生,另一个不能。你应该分开这两个球员。移动到单独的if-statements

答案 1 :(得分:1)

放弃了几个星期后,我回到这里,发现我的解决方案实际上是一个非常简单的解决方案。现在一切正常! 看来,当您尝试获取事件的x或y值时,您需要使用event.getY(pointerIndex)而不是event.getY() 我做的唯一改变是:

int pointerIndex = event.getActionIndex();
int pointerID = event.getPointerId(pointerIndex);
float touchY = event.getY(pointerIndex);