2011-01-25 46 views
2

當我運行下面的一切工作正常的代碼,它與三個球一個簡單的應用程序,你可以走動...的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; 

     } 


    } 


} 

但是當我嘗試從處理的onTouchEvent主要活動不起作用,奇怪的是它不能讀取一個簡單的變量(x,y)! 我不明白爲什麼會發生這種情況,它似乎只有在視圖中才能使它們變紅!

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

     } 


    } 


} 

任何知道爲什麼的人?

是@bigstones onTouchevent正在工作,它捕獲所有的動作,問題是如果我在活動內部有ontouchevent代碼,變量X,Y似乎不工作儘管他們有一個值,我可以打印它(或日誌)我說的是測試,我已經嘗試並將if()語句(getX,getY)中的所有值更改爲整數,並且它不適用於X,Y .....請再次檢查代碼! 謝謝!

+1

根據引用,僅當沒有任何視圖使用事件時才調用Activity.onTouchEvent()。也許這讓你的觀點變得焦點,使得它「吃」所有的事件? Activity.onTouchEvent()被調用嗎? (請在回答中添加@bigstones,以便我收到通知) – bigstones 2011-01-25 21:24:53

+0

下一次正確格式化您的代碼,並儘量不要發佈您的整個代碼,並讓我們通過它。 – Falmarri 2011-01-25 21:42:54

回答

2

找到了!

我發現,問題是,它是一個應該被註冊某種聽者,所以我這樣做了佈局:

我將此代碼添加到我的主要CLAS:

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

而這行到onCreate方法:

previewLayout.setOnClickListener(previewListener); 

它的工作!

0

看起來你搬了DrawView之外的onTouch。由於您嘗試處理觸摸事件,因此您需要設置onTouchListener而不是onClickListener