views:

76

answers:

1

Hello,

So all I'm trying to do is draw circles around two points that are being touched, and as the fingers drag along on the screen, have these circles follow each finger.

However, I'm getting some weird behavior that I can't figure out. So when I place two fingers on the screen I get my circles no problem. When I drag around with the two fingers everything works like I want it. However, if I lift the first finger, everything stops redrawing, and the second finger that is still on the screen stops being tracked. If I place the first finger back on, everything behaves good again. I figure I must be handling the two fingers poorly.

Can anyone see what I'm doing wrong?

Also, if I have two fingers on the screen, and I lift and touch the second finger, everything behaves well. It's only in the case where I have two fingers and I lift the first one do I see a problem.

@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;
    }
A: 

You're calling MotionEvent#getX()/getY()/getSize() with the pointer ID as the argument instead of the index.

adamp
So I should be using findPointerIndex instead of getPointerID? I don't understand the difference.
CrazyJay
No, you have the pointer index already. You're looping over it with `for(int i = 0; i < pointCt; i++){` Right below that you call `event.getPointerId(i)` - `i` is your index. Simply use `i` as the parameter to your calls to getX()/getY()/getSize() below that as well.
adamp
To clarify the difference: Index is how you address pointer data within a `MotionEvent`. ID is the unique identifier assigned to each pointer. ID is an attribute of each pointer just like the X or Y value is.
adamp