处理多点触控

时间:2010-09-22 19:04:15

标签: android multi-touch

所以我要做的就是在被触摸的两个点周围画圆圈,当手指在屏幕上拖动时,让每个手指跟随这些圆圈。

然而,我得到了一些我无法弄清楚的奇怪行为。因此,当我将两根手指放在屏幕上时,我的圈子没问题。当我用两根手指拖动时,一切都像我想要的那样。但是,如果我抬起第一根手指,一切都会停止重绘,并且仍然在屏幕上的第二根手指停止被跟踪。如果我重新放置第一根手指,一切都会再次表现良好。我想我必须把两根手指处理不好。

谁能看到我做错了什么?

另外,如果屏幕上有两根手指,我抬起并触摸第二根手指,一切都表现良好。只有在我有两个手指的情况下,我才能解除第一个手指,我看到了问题。

@Override
    public boolean onTouchEvent(MotionEvent event) {    

       /*
       * Log this touch event 
       */
      String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
                  "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
       StringBuilder sb = new StringBuilder();
       int action = event.getAction();
       int actionCode = action & MotionEvent.ACTION_MASK;

       sb.append("OnTouchEvent ACTION_" ).append(names[actionCode]);

       if (actionCode == MotionEvent.ACTION_POINTER_DOWN
             || actionCode == MotionEvent.ACTION_POINTER_UP) {
          sb.append("(pid " ).append(
          action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
          sb.append(")" );
       }
       sb.append("[" );

       for (int i = 0; i < event.getPointerCount(); i++) {
          sb.append("#" ).append(i);
          sb.append("(pid " ).append(event.getPointerId(i));
          sb.append(")=" ).append((int) event.getX(i));
          sb.append("," ).append((int) event.getY(i));
          if (i + 1 < event.getPointerCount())
          sb.append(";" );
       }
       sb.append("]" );

       Log.d(TAG, sb.toString());


        /*
        * Track the touches that are on the screen
        * Grab X,Y, and Size data
        */
        int action = event.getAction();
        int actionCode = action & MotionEvent.ACTION_MASK;

        if (actionCode == MotionEvent.ACTION_POINTER_DOWN) {

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = true;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = true;
            }
        }
        else if (actionCode == MotionEvent.ACTION_POINTER_UP){

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = false;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = false;
            }
        }

        if(actionCode == MotionEvent.ACTION_DOWN){

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = true;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = true;
            }
        }


        else if (actionCode == MotionEvent.ACTION_UP){

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = false;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = false;
            }
        }


        int pointCt = event.getPointerCount();
        for(int i = 0; i < pointCt; i++){

            int pid = event.getPointerId(i);
            if((pid == 0) && p0Down){
                curX1 = event.getX(pid);
                curY1 = event.getY(pid);
                size1 = event.getSize(pid);
            }
            if((pid == 0) && (p0Down == false)){
                curX1 = -1;
                curY1 = -1;
                size1 = -1;
            }
            if((pid == 1) && p1Down){
                curX2 = event.getX(pid);
                curY2 = event.getY(pid);
                size2 = event.getSize(pid);
            }
            if((pid == 1) && (p1Down == false)){
                curX2 = -1;
                curY2 = -1;
                size2 = -1;
            }

        }
        return true;
    }

1 个答案:

答案 0 :(得分:3)

您使用指针ID作为参数而不是索引来调用MotionEvent#getX()/ getY()/ getSize()。