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


colorballs class has 2 bitmaps (not shown here), , struggling find if bitmap dragged , dropped within "square" (please see on draw method below) on screen. bitmap should never dragged again, if same bitmap moved anywhere-else not within square, should restore original position when stopped moving bitmap. please code.

     // method draws balls        @override protected void ondraw(canvas canvas) {         //canvas.drawcolor(0xffcccccc);     //if want background color             //draw balls on canvas     (colorball ball : colorballs) {         canvas.drawbitmap(ball.getbitmap(), ball.getx(), ball.gety(), null);         }      // draw square.     paint rectpaint = new paint();     // color of 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 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 check if finger on ball         balid = 0;         (colorball ball : colorballs) {             // check if inside bounds of ball (circle)             // center ball             int centerx = ball.getx() + 25;             int centery = ball.gety() + 25;              // calculate radius touch center of ball             double radcircle  = math.sqrt( (double) (((centerx-x)*(centerx-x)) + (centery-y)*(centery-y)));              // if radius smaller 23 (radius of ball 22), must on ball             if (radcircle < 23){                 balid = ball.getid();                 initpoint.x=x;                 initpoint.y=y;                 balltouchedflag =true;                 break;             }             }           break;        case motionevent.action_move:   // touch drag ball         // move balls same finger         if (balid > 0 ) {             colorballs[balid-1].setx(x-25);             colorballs[balid-1].sety(y-25);         }          break;       case motionevent.action_up:          // touch drop - things here after dropping         // determine if ball touched.            break;      }        // redraw canvas     invalidate();      return true; } 

i figured out wanted achieve, here updates in above code,

in motionevent.action_down, did following changes

  // if click image not inside 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 motionevent.move, add following lines

   if ( balid > 0 && rect.contains(colorballs[balid-1].getx(), colorballs[balid-1].gety() == false )    {.....} 

in motionevent.action_up, added following code

     // now, when drop clicked bitmap, if not not in square, store            //  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); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -