【问题标题】:Is it possible to limit the multitouch to only two fingers?是否可以将多点触控限制为仅两个手指?
【发布时间】:2021-08-22 13:59:03
【问题描述】:

在我的应用上同时按下三个或更多手指时出现以下错误。

'JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
        at boolean

以下代码控制多点触控:

    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        int pointerID = event.getPointerId(pointerIndex);
        int[] touchX = new int[event.getPointerCount()];

        for(int i = 0 ; i < event.getPointerCount() ; i++){
            touchX[i] = (int) event.getX(i);
        }

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (touchX[0] < screenX / 2) {
                    astronaut.isGoingUp = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                astronaut.isGoingUp = false;
                if (touchX[0] > screenX / 2) {
                    astronaut.toShot++;
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                if(touchX[pointerID] < screenX / 2) {
                    astronaut.isGoingUp = true;
                }
            case MotionEvent.ACTION_POINTER_UP:
                if(touchX[pointerID] > screenX / 2){
                    astronaut.toShot++;
                }
            default:
                break;

        }
        return true;
    }

我猜这与event.getPointerCount() 或for 循环有关。 我想知道当它崩溃超过两个手指时我做错了什么,除了它完全按照预期工作。

谢谢

【问题讨论】:

    标签: java android multi-touch


    【解决方案1】:

    您可以使用MotionEvent.ACTION_POINTER_DOWN 掩码过滤多点触控操作,如果手指数大于2,则返回。

    getPointerCount() 返回一次接触的装配工的数量。

    public boolean onTouchEvent(MotionEvent event) {
    
        // More than one down touches
        if ((action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN) { 
            int count = event.getPointerCount(); // Number of touched 'fingers' 
            if (count > 2) // return if greater than 2 fingers
                return true;
        }
        
        // Your code here is for one or two fingers
        
    }
    
        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2015-10-02
      • 2012-03-18
      • 1970-01-01
      相关资源
      最近更新 更多