2011-01-25 3 views
2

quand je lance le code ci-dessous tout fonctionne bien, il est une application simple avec trois balles que vous pouvez vous déplacer ...problème onTouchevent() android

public class dragndrop extends Activity { 
    /** Called when the activity is first created. */ 
     private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls 
     private static final String TAG="MyTAG"; 
     DrawView myView; 
     private int balID = 0; // variable to know what ball is being dragged 
     int X; 
     int Y; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Point point1 = new Point(); 
      point1.x = 50; 
      point1.y = 20; 
      Point point2 = new Point(); 
      point2.x = 100; 
      point2.y = 20; 
      Point point3 = new Point(); 
      point3.x = 150; 
      point3.y = 20; 


      // declare each ball with the ColorBall class 
      colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1); 
      colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2); 
      colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3); 
      myView = new DrawView(this); 
     setContentView(myView); 
    } 


    public class DrawView extends View { 


     public DrawView(Context context) { 
      super(context); 
      setFocusable(true); //necessary for getting the touch events 

      // setting the start point for the balls 


     } 

     // the method that draws the balls 
     @Override protected void onDraw(Canvas canvas) { 

      //draw the balls on the canvas 
      for (ColorBall ball : colorballs) { 
       canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null); 
       } 

     } 



     // events when touching the screen 
     public boolean onTouchEvent(MotionEvent event) { 
      int eventaction = event.getAction(); 

      X = (int)event.getX(); 
      Y = (int)event.getY(); 

      switch (eventaction) { 

      case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball 
       balID = 0; 
       for (ColorBall ball : colorballs) { 
        Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y); 
        Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY()); 

        int x =X; 
        int y =Y; 
        Log.d(TAG,"lalalalalala"+x+" coords: "+y); 



        if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){ 
         Log.d(TAG,"inside ball coords!!!!!!!!!!!!!!!!!!!!!!!!:"+ball.getX()+" coords: "+ball.getY()); 
         balID = ball.getID(); 


         break; 
        } 
        } 

       break; 


      case MotionEvent.ACTION_MOVE: // touch drag with the ball 
       // move the balls the same as the finger 
       if (balID > 0) { 
        colorballs[balID-1].setX(X-25); 
        colorballs[balID-1].setY(Y-25); 
       } 

       break; 

      case MotionEvent.ACTION_UP: 
       // touch drop - just do things here after dropping 

       break; 
      } 
      // redraw the canvas 
      myView.invalidate(); 
      return true; 

     } 


    } 


} 

Mais lorsque je tente de gérer la onTouchevent de la l'activité principale ne fonctionne pas et l'étrange est qu'il ne peut pas lire une variable simple (x, y) !!! Je ne peux pas comprendre pourquoi cela est arrivé, il semble qu'il ne peut les rouge que si c'est dans une vue !!!

public class dragndrop extends Activity { 
    /** Called when the activity is first created. */ 
     private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls 
     private static final String TAG="MyTAG"; 
     DrawView myView; 
     private int balID = 0; // variable to know what ball is being dragged 
     int X; 
     int Y; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Point point1 = new Point(); 
      point1.x = 50; 
      point1.y = 20; 
      Point point2 = new Point(); 
      point2.x = 100; 
      point2.y = 20; 
      Point point3 = new Point(); 
      point3.x = 150; 
      point3.y = 20; 


      // declare each ball with the ColorBall class 
      colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1); 
      colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2); 
      colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3); 
      myView = new DrawView(this); 
     setContentView(myView); 
    } 


// events when touching the screen 
    public boolean onTouchEvent(MotionEvent event) { 
     int eventaction = event.getAction(); 

     X = (int)event.getX(); 
     Y = (int)event.getY(); 

     switch (eventaction) { 

     case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball 
      balID = 0; 
      for (ColorBall ball : colorballs) { 
       Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y); 
       Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY()); 

       int x =X; 
       int y =Y; 
       Log.d(TAG,"lalalalalala"+x+" coords: "+y); 



       if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){ 
      Log.d(TAG,"inside ball coords!!:"+ball.getX()+" coords: "+ball.getY()); 
        balID = ball.getID(); 


        break; 
       } 
       } 

      break; 


     case MotionEvent.ACTION_MOVE: // touch drag with the ball 
      // move the balls the same as the finger 
      if (balID > 0) { 
       colorballs[balID-1].setX(X-25); 
       colorballs[balID-1].setY(Y-25); 
      } 

      break; 

     case MotionEvent.ACTION_UP: 
      // touch drop - just do things here after dropping 

      break; 
     } 
     // redraw the canvas 
     myView.invalidate(); 
     return true; 

    } 




    public class DrawView extends View { 


     public DrawView(Context context) { 
      super(context); 
      setFocusable(true); //necessary for getting the touch events 

      // setting the start point for the balls 


     } 

     // the method that draws the balls 
     @Override protected void onDraw(Canvas canvas) { 
      //canvas.drawColor(0xFFCCCCCC);  //if you want another background color  

      //draw the balls on the canvas 
      for (ColorBall ball : colorballs) { 
       canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null); 
       } 

     } 


    } 


} 

Toute personne qui sait pourquoi? Oui @bigstones l'onTouchevent fonctionne, il capture toutes les actions, le problème est que si j'ai le code ontouchevent dans l'activité les variables X, Y ne semblent pas fonctionner bien qu'elles aient une valeur et je peux imprimer il (ou log) ce que je dis est testé, j'ai essayé et changé toutes les valeurs de l'instruction if() (getX, getY) en entiers et cela n'a pas fonctionné seulement pour X, Y ..... Vérifiez le code à nouveau s'il vous plaît! merci!

+1

Selon les références, 'Activity.onTouchEvent()' est appelé uniquement si aucune des vues ne consomme un événement. Peut-être que laisser votre point de vue orientable le fait "manger" tous les événements? Est-ce que 'Activity.onTouchEvent()' est appelé? (Veuillez ajouter @bigstones dans votre réponse afin que j'obtienne un avis) – bigstones

+0

La prochaine fois formatez votre code correctement et essayez de ne pas poster votre code entier et nous faire passer par là. – Falmarri

Répondre

2

Trouvé!

Je trouve que le problème est qu'il est la disposition qui devrait enregistrerons une sorte de listner donc je l'ai fait:

J'ai ajouté ce code à mes principales CLAS:

private OnClickListener previewListener = new OnClickListener() {  
    @Override 
    public void onClick(View v) { 
     System.err.println("I've been clicked"); 
    } 
}; 

Et la cette ligne à la méthode onCreate:

previewLayout.setOnClickListener(previewListener); 

Et cela a fonctionné!

+0

Je pense que vous pouvez voir un effet secondaire de l'ajout d'un écouteur de clic qui est que cela implique 'setClickable (true)'. Plutôt que d'ajouter un écouteur qui ne fait rien d'utile, appelez plutôt 'setClickable()' à la place. –

0

Semble que vous avez déplacé le onTouch en dehors du DrawView. Puisque vous essayez de gérer les événements tactiles, vous devez configurer un onTouchListener mais pas le onClickListener.

Questions connexes