Search code examples
androiddrag-and-droptouch-event

Dragging and Dropping the bitmap in specific location in Android, otherwise restoring to initial position


colorballs is a class that has two bitmaps (not Shown Here), and I am struggling to find that if the bitmap is dragged and then dropped within the "Square" (please see On Draw Method below) on the screen. Then bitmap should never be dragged again, if the same bitmap is moved anywhere-else but not within the square, it should restore to original position when i stopped moving the bitmap. Please help with the Code.

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

Solution

  • I figured out what i wanted to achieve, Here is my updates in the above code,

    In the MotionEvent.ACTION_DOWN, I did the following changes

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

    In the MotionEvent.Move, add following lines

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

    in the MotionEvent.ACTION_UP, I added following Code

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