0

colorballsは2つのビットマップ(ここには表示されていません)を持つクラスであり、ビットマップをドラッグしてから画面の「正方形」(下記の「描画方法」を参照)内にドロップすると、見つけるのに苦労しています。次に、同じビットマップをどこかに移動した場合、ビットマップを再度ドラッグすることはできません。それ以外の場合、正方形内ではなく、ビットマップの移動を停止したときに元の位置に復元する必要があります。コードを手伝ってください。

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

1 に答える 1

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.Moveに、次の行を追加します

   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);
于 2013-03-02T00:20:52.923 に答える