Android Multi-touch无法正常工作

时间:2012-05-13 17:11:57

标签: java android surfaceview multi-touch

我正在设计基于android中的surfaceview的游戏虚拟按钮。我已经重写了onTouch(MotionEvent事件)方法来执行多点触控功能,但它的行为方式并不像我想要的那样。当触摸一个手指时,第二个手指不会被识别,因此我无法按要求执行这些功能。这是我覆盖的方法的代码。

@Override
public boolean onTouchEvent(MotionEvent event){

    touchX = (int)event.getX();
    touchY = (int)event.getY();

    int action = event.getActionMasked();//event.getAction() & MotionEvent.ACTION_MASK;

    if(touchX >= 0 && touchX <= leftbutton.getWidth() && touchY >= this.getHeight() - 100 && touchY <= this.getHeight()){
        if (action == MotionEvent.ACTION_DOWN){
            starship.setLeftRight(1, 0, true);
            leftButton.update(touchedleftbutton);
        }
        else if (action == MotionEvent.ACTION_UP){
            starship.setLeftRight(0, 0, false);
            leftButton.update(leftbutton);
        }
        else if (action == MotionEvent.ACTION_POINTER_DOWN){
            starship.setLeftRight(1, 0, true);
            leftButton.update(touchedleftbutton);
        }
        else if (action == MotionEvent.ACTION_POINTER_UP){

            starship.setLeftRight(0, 0, false);
            leftButton.update(leftbutton);
        }
    }
    else if (touchX >= 20 + leftbutton.getWidth() && touchX <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY >= this.getHeight() - 100 && touchY <= this.getHeight()){
        if (action == MotionEvent.ACTION_DOWN){
            starship.setLeftRight(0, 1, true);
            rightButton.update(touchedrightbutton);
        }
        else if (action == MotionEvent.ACTION_UP){
            starship.setLeftRight(0, 0, false);
            rightButton.update(rightbutton);
        }
        else if (action == MotionEvent.ACTION_POINTER_DOWN){
            starship.setLeftRight(0, 1, true);
            rightButton.update(touchedrightbutton);
        }
        else if (action == MotionEvent.ACTION_POINTER_UP){
            starship.setLeftRight(0, 0, false);
            rightButton.update(rightbutton);
        }
    }

    else if(touchX >= this.getWidth() - shootbutton.getWidth() && touchY >+ this.getHeight() - 100 && touchY < this.getHeight()){
        if (action == MotionEvent.ACTION_DOWN){

            shootButton.update(shoottouched);
        }
        else if (action == MotionEvent.ACTION_UP){

            shootButton.update(shootbutton);
        }
        else if (action == MotionEvent.ACTION_POINTER_DOWN){

            shootButton.update(shoottouched);
        }
        else if (action == MotionEvent.ACTION_POINTER_UP){

            shootButton.update(shootbutton);
        }
    }

    return true;        
}

----------------------------------------------编辑-----------------------------------------

我能够解决我的问题,现在一切正常。这是我更新的代码。希望它可以帮助任何患有同样问题的人。

@Override
public boolean onTouchEvent(MotionEvent event){

    int action = event.getActionMasked();//event.getAction() & MotionEvent.ACTION_MASK;
    int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
    int pointerID = event.getPointerId(pointerIndex);
    int pointerCount = event.getPointerCount();

    touchX = new int[pointerCount];
    touchY = new int[pointerCount];

    for(int i = 0; i<pointerCount; i++){

        touchX[i] = (int) event.getX(i);
        touchY[i] = (int) event.getY(i);

    }
    switch(action){

    case MotionEvent.ACTION_DOWN:
        if(touchX[0] >= 0 && touchX[0] <= leftbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){
            starship.setLeftRight(1, 0, true);
            leftButton.update(touchedleftbutton);

        }
        else if (touchX[0] >= 20 + leftbutton.getWidth() && touchX[0] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){

            starship.setLeftRight(0, 1, true);
            rightButton.update(touchedrightbutton);
        }
        else if(touchX[0] >= this.getWidth() - shootbutton.getWidth() && touchY[0] >+ this.getHeight() - 100 && touchY[0] < this.getHeight()){
            shootButton.update(shoottouched);
        }

        break;

    case MotionEvent.ACTION_UP:
        if(touchX[0] >= 0 && touchX[0] <= leftbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){
            starship.setLeftRight(0, 0, false);
            leftButton.update(leftbutton);

        }
        else if (touchX[0] >= 20 + leftbutton.getWidth() && touchX[0] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){

            starship.setLeftRight(0, 0, false);
            rightButton.update(rightbutton);
        }
        else if(touchX[0] >= this.getWidth() - shootbutton.getWidth() && touchY[0] >+ this.getHeight() - 100 && touchY[0] < this.getHeight()){
            shootButton.update(shootbutton);
        }

        break;

    case MotionEvent.ACTION_POINTER_DOWN:
        if(touchX[pointerID] >= 0 && touchX[pointerID] <= leftbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){
            starship.setLeftRight(1, 0, true);
            leftButton.update(touchedleftbutton);

        }
        else if (touchX[pointerID] >= 20 + leftbutton.getWidth() && touchX[pointerID] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){

            starship.setLeftRight(0, 1, true);
            rightButton.update(touchedrightbutton);
        }
        else if(touchX[pointerID] >= this.getWidth() - shootbutton.getWidth() && touchY[pointerID] >+ this.getHeight() - 100 && touchY[pointerID] < this.getHeight()){
            shootButton.update(shoottouched);
        }

        break;

    case MotionEvent.ACTION_POINTER_UP:
        if(touchX[pointerID] >= 0 && touchX[pointerID] <= leftbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){
            starship.setLeftRight(0, 0, false);
            leftButton.update(leftbutton);

        }
        else if (touchX[pointerID] >= 20 + leftbutton.getWidth() && touchX[pointerID] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){

            starship.setLeftRight(0, 0, false);
            rightButton.update(rightbutton);
        }
        else if(touchX[pointerID] >= this.getWidth() - shootbutton.getWidth() && touchY[pointerID] >+ this.getHeight() - 100 && touchY[pointerID] < this.getHeight()){
            shootButton.update(shootbutton);
        }

        break;
    }
    return true;        
}

2 个答案:

答案 0 :(得分:2)

我编写Android多点触控编码和测试连接的经验令人沮丧。

我最终使用android-multitouch-controller。该库将让您更好地了解如何处理所有警告。

答案 1 :(得分:2)

请参阅此link,它显示了如何以适当的方式使用多点触控