【问题标题】:Android multi-touch: Not detecting MotionEvent.ACTION_POINTER_DOWNAndroid 多点触控:未检测到 MotionEvent.ACTION_POINTER_DOWN
【发布时间】:2014-03-18 17:43:09
【问题描述】:

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

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

什么对两个玩家有用 -当用户只按下屏幕的任一半时,它调用goUp()

什么对两个玩家不起作用 -当用户按下一半屏幕,再按下另一半,另一半不调用goUp()

另外,有没有办法在模拟器上一次模拟多个触摸,这样我就可以在logcat 中看到logs?

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;
}

【问题讨论】:

    标签: java android multi-touch motionevent ontouch


    【解决方案1】:

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

        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

    【讨论】:

    • 感谢您的回复,但是通过将 else 更改为 if(!touchOnTopOfScreen) 对其进行测试后,这不起作用。它仍然以相同的方式工作。
    • 在 goUp() 中,它只是简单地改变了一个在预定线程中使用的布尔值。
    • touchOnTopOfScreen 返回一个真假,对吗?
    • 是的,touchOnTopOfScreen 返回一个布尔值。
    • 好的,我目前实际上正在尝试寻找解决方案,因为据我所知,我所说的应该已经解决了这个问题。我以前看过这个链接,看看:@ 987654321@
    【解决方案2】:

    在放弃这个几周后,我回到这个并找到了我的解决方案,这实际上是一个非常容易解决的问题。现在一切正常! 看来当你尝试获取事件的x或y值时,你需要使用event.getY(pointerIndex)而不是event.getY() 我所做的唯一更改是:

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

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多