安卓多点触控 - 第二指操作。移动被忽略

2022-09-02 09:33:59

下面的代码是我一直尝试用于多点触控的代码。手指一设置正确,当我拖动手指时,它会四处移动。当我触摸和释放手指时,手指二出现并消失,但它永远不会移动。任何想法是什么问题?

我已经阅读了开发人员博客,我仍然不明白问题是什么。

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        int pointerId = event.getPointerId(pointerIndex);
        switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            if (pointerId == 0)
            {
                fingerOneDown = 1;
                fingerOneX = event.getX(pointerIndex);
                fingerOneY = event.getY(pointerIndex);
            }
            if (pointerId == 1)
            {
                fingerTwoDown = 1;
                fingerTwoX = event.getX(pointerIndex);
                fingerTwoY = event.getY(pointerIndex);
            }
            break;

        case MotionEvent.ACTION_UP:          
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
            if (pointerId == 0)
            {
                fingerOneDown = 0;
                fingerOneX = event.getX(pointerIndex);
                fingerOneY = event.getY(pointerIndex);
            }
            if (pointerId == 1)
            {
                fingerTwoDown = 0;
                fingerTwoX = event.getX(pointerIndex);
                fingerTwoY = event.getY(pointerIndex);
            }
            break;

        case MotionEvent.ACTION_MOVE:
            if (pointerId == 0)
            {
                fingerOneDown = 1;
                fingerOneX = event.getX(pointerIndex);
                fingerOneY = event.getY(pointerIndex);
            }
            if (pointerId == 1)
            {
                fingerTwoDown = 1;
                fingerTwoX = event.getX(pointerIndex);
                fingerTwoY = event.getY(pointerIndex);
            }

            break;
        }
        return true;
    }

答案 1

别介意这个。我修复了它。操作。由于某种奇怪的原因,MOVE 事件始终以指针 Id 为零。因此,您始终必须重新计算事件中的指针 Id,如下所示:

       case MotionEvent.ACTION_MOVE:

        int pointerCount = event.getPointerCount();
        for(int i = 0; i < pointerCount; ++i)
        {
            pointerIndex = i;
            pointerId = event.getPointerId(pointerIndex);
            Log.d("pointer id - move",Integer.toString(pointerId));
            if(pointerId == 0)
            {
                fingerOneDown = 1;
                fingerOneX = event.getX(pointerIndex);
                fingerOneY = event.getY(pointerIndex);
            }
            if(pointerId == 1)
            {
                fingerTwoDown = 1;
                fingerTwoX = event.getX(pointerIndex);
                fingerTwoY = event.getY(pointerIndex);
            }
        }
        break;

答案 2

推荐