代码之家  ›  专栏  ›  技术社区  ›  Cody

Android多点触摸:索引>0的指针不会触发事件。Action_Move

  •  3
  • Cody  · 技术社区  · 10 年前

    在我的多点触摸程序中,我遇到了一个运行时错误,其中指针在屏幕上滑动的多点触逻辑(MotionEvent.ACTION_MOVE)仅由初始指针输入。这意味着我的switch语句中的ACTION_MOVE的代码只能通过第一根手指触摸屏幕访问,也就是指针索引=0。以下是我的代码的相关部分:

    public boolean onTouchEvent(MotionEvent event) {
            // get pointer index from the event object
            int pointerIndex = event.getActionIndex();
            // get pointer ID
            int pointerId = event.getPointerId(pointerIndex);
            // get masked (not specific to a pointer) action
            int maskedAction = event.getActionMasked();
    
            switch (maskedAction) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN: {
                // We have a new pointer. Lets add it to the list of pointers
                PointF f = new PointF();
                f.x = event.getX(pointerIndex);
                f.y = event.getY(pointerIndex);
                mActivePointers.put(pointerId, f);
                Log.d("Multitouch", "x coord = " + Float.toString(f.x));
                Log.d("Multitouch", "y coord = " + Float.toString(f.y));
                break;
            }
            case MotionEvent.ACTION_MOVE: { // a pointer was moved
                Log.e("Multitouch", "Pointer"+ Integer.toString(pointerId) +" is moving");
                for (int size = event.getPointerCount(), i = 0; i < size; i++) {
                    PointF f = mActivePointers.get(event.getPointerId(i));
                    if (f != null) {
                        f.x = event.getX(i);
                        f.y = event.getY(i);
                    }
                }
                break;
            }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL: {
                mActivePointers.remove(pointerId);
                break;
            }
            }
            invalidate();
            return true;
        }
    

    不用说,我希望Action_Move中的代码由任何指针触发,而不仅仅是初始指针。是什么防止索引零以外的指针触发移动事件?

    1 回复  |  直到 10 年前
        1
  •  4
  •   ProgrammingMachine5000    10 年前

    如文件中所述, MotionEvent.getActionIndex() 仅返回的索引 ACTION_POINTER_UP ACTION_POINTER_DOWN ( http://developer.android.com/reference/android/view/MotionEvent.html#getActionIndex%28%29 ). 但是,在日志输出中使用由所述函数返回的指针Index生成的指针ID。因此,日志是不正确的,因为函数没有返回您期望的结果。