2010-09-22 5 views
2

Tout ce que j'essaie de faire est de dessiner des cercles autour de deux points qui sont touchés, et comme les doigts traînent sur l'écran, ces cercles suivent chaque doigt.Manipulation de Multitouch

Cependant, je reçois un comportement étrange que je ne peux pas comprendre. Donc, quand je place deux doigts sur l'écran, mes cercles ne me posent aucun problème. Quand je traîne avec les deux doigts tout fonctionne comme je le veux. Cependant, si je soulève le premier doigt, tout s'arrête de redessiner, et le second doigt qui est encore sur l'écran cesse d'être suivi. Si je remets le premier doigt, tout se comporte bien. Je suppose que je dois mal gérer les deux doigts.

Quelqu'un peut-il voir ce que je fais mal?

De plus, si j'ai deux doigts sur l'écran et que je soulève et touche le deuxième doigt, tout se comporte bien. C'est seulement dans le cas où j'ai deux doigts et que je soulève le premier je vois un problème.

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

Répondre

3

Vous appelez MotionEvent # getX()/getY()/getSize() avec l'ID de pointeur comme argument au lieu de l'indice.

+0

Donc je devrais utiliser findPointerIndex au lieu de getPointerID? Je ne comprends pas la différence. –

+1

Non, vous avez déjà l'index du pointeur. Vous êtes en boucle avec 'pour (int i = 0; i adamp

+5

Pour clarifier la différence: Index est la façon dont vous adressez les données de pointeur dans un 'MotionEvent'. ID est l'identifiant unique attribué à chaque pointeur. ID est un attribut de chaque pointeur, tout comme la valeur X ou Y. – adamp