2013-03-01 81 views
0

colorballs是一个具有两个位图(未在此处显示)的类,并且我很努力地发现,如果位图被拖动,然后在屏幕上的“Square”(请参阅​​下面的“绘制方法”)。然后,位图不应该再次被拖动,如果相同的位图移动到任何地方 - 其他位置而不是位于正方形内,它应该在我停止移动位图时恢复到原始位置。请帮助守则。在Android中拖放特定位置的位图,否则恢复到初始位置

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


     } 

    // draw the square. 
    Paint rectPaint = new Paint(); 
    // color of the border 
    rectPaint.setColor(Color.BLUE); 
    rectPaint.setStrokeWidth(1);   
    rectPaint.setStyle(Paint.Style.STROKE); 

    int w = this.getWidth(), h = this.getHeight(); 
    int offset = (h - w)/2; 
    int step =w/3; 
    rect= new Rect (0, offset+0*step, step, offset+step*1); 
    canvas.drawRect(rect, rectPaint); 

} 

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

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

    switch (eventaction) { 

    case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball 
     balID = 0; 
     for (ColorBall ball : colorballs) { 
      // check if inside the bounds of the ball (circle) 
      // get the center for the ball 
      int centerX = ball.getX() + 25; 
      int centerY = ball.getY() + 25; 

      // calculate the radius from the touch to the center of the ball 
      double radCircle = Math.sqrt((double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y))); 

      // if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball 
      if (radCircle < 23){ 
       balID = ball.getID(); 
       initPoint.x=X; 
       initPoint.y=Y; 
       ballTouchedFlag =true; 
       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 
     // Determine if the ball is touched. 


     break; 
    } 

    // redraw the canvas 
    invalidate(); 
    return true; 
} 
+1

http://www.vogella.com/articles/AndroidDragAndDrop/article.html – Raghunandan 2013-03-01 19:56:52

回答

0

我想通了,我要实现的目标,这是我在上面的代码更新,

在MotionEvent.ACTION_DOWN,我做了以下变化

// if the click image is not inside the rectangle 

    if (radCircle < 23 && rect.contains(colorballs[balID-1].getX(), colorballs[balID-1].getY() == false){ 
      balID = ball.getID(); 
      initPoint.x=X; 
      initPoint.y=Y; 
      ballTouchedFlag =true; 
      break; 
     } 

在MotionEvent移动,添加以下行

if (balID > 0 && rect.contains(colorballs[balID-1].getX(), colorballs[balID-1].getY() == false) 
    {.....} 

在MotionEvent.ACTION_UP中,我添加了以下代码

 // Now, when you drop the clicked bitmap, if it is not not in the square, store it   // first position 
    if (rect.contains(colorballs[balID-1].getX(), colorballs[balID-1].getY() == false) 
colorballs[balID-1].setX(initPoint.x-25); 
     colorballs[balID-1].setY(initPoint.y-25);