Pinki
Pinki

Reputation: 21929

Compare the bitmaps in canvas

public class DrawView extends View
{
   private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
   private int balID = 0; // variable to know what ball is being dragged

      /* protected Bitmap getImage(int id) {
        return BitmapFactory.decodeResource(mContex.getResources(), id);
    }*/
   private Paint mBitmapPaint = new Paint();
  public DrawView(Context context) {
        super(context);
        setFocusable(false); //necessary for getting the touch events

        // setting the start point for the balls
        Point point1 = new Point();
        point1.x = 50;
        point1.y = 400;
        Point point2 = new Point();
        point2.x = 100;
        point2.y = 400;
        Point point3 = new Point();
        point3.x = 150;
        point3.y = 400;


        // declare each ball with the ColorBall class
        colorballs[0] = new ColorBall(context,R.drawable.b, point1);
       colorballs[2] = new ColorBall(context,R.drawable.t, point3);


    }

    // the method that draws the balls
    @Override protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFCCCCCC);
        setFocusable(false);
        Log.v("Images","3333");
        //if you want another background color       
        canvas.drawBitmap((BitmapFactory.decodeResource(getResources(),R.drawable.caralpha)), 10, -50, mBitmapPaint);
        //draw the balls on the canvas
        for (ColorBall ball : colorballs) {
            canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
          }
        //canvas.drawRect(10, 50, 10 + 2, 10 + 2,mBitmapPaint);
        canvas.drawText("A", 10,350, mBitmapPaint);


        Vector correctname=correct("B");

        String name="b";



        for(int i=0,xCo=20;i<correctname.size();i++)
        {
          try {
            int image=selectImage(name.charAt(i));
            canvas.drawBitmap((BitmapFactory.decodeResource(getResources(),image)), 10+xCo,350, mBitmapPaint);
            xCo=xCo+100;
          }
          catch(Exception e)
          {

          }
        }
    }

    private int selectImage(char charAt) {
        switch(charAt)
        {
        case 'a':
            return R.drawable.a;
        case 'b':
            return R.drawable.b;
        case 't':
            return R.drawable.t;

        }
        return 0;

    }

    private Vector correct(String word) {
         Vector al = new Vector();
            for (int i = 0; i < word.length(); i++) 
            {
            al.add(word.charAt(i));
            }
            al.toString();
            return al;
    }

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

        int X = (int)event.getX(); 
        int 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) {
                // 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();
                    break;
                }

                // check all the bounds of the ball (square)
                //if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
                //  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) {
                Log.v("Images","3333  Moving");
                colorballs[balID-1].setX(X-25);
                colorballs[balID-1].setY(Y-25);
            }

            break; 

        case MotionEvent.ACTION_UP: 
            /*for (ColorBall ball : colorballs) {
                Log.v("y value","YYYYYYYYYYY  "+ball.getY()+"XXXXXXXXXXXX "+ball.getID());
            }*/


            // touch drop - just do things here after dropping
//setFocusable(false);
             break; 
        } 
        // redraw the canvas
        invalidate(); 
        return true; 

    }
}

Hi I am using the above code for displaying bitmap.and i also move that bitmap.Now my qusetion is how i am compare bitmap with another bitmap. Please give me some suggestions.Thanks in advance

Upvotes: 1

Views: 1431

Answers (2)

Ultimate Gobblement
Ultimate Gobblement

Reputation: 1879

bitmaps are equal when we move one bimap near to anther bitmap and if both are same the bitmap moving become false

Assuming you want to do Collision detection between two circles, this can be performed by getting the distance between the 2 centres. EG:

double getDistance(Point p1, Point p2){
    double dx = p1.x - p2.x;
    double dy = p1.y - p2.y;
    return Math.sqrt((dx*dx)+(dy*dy));
}

Then checking if the value is less than the radius of circle1 + the radius of circle2, which would be faster then checking the images for overlap.

Upvotes: 1

WarrenFaith
WarrenFaith

Reputation: 57672

"collision detection" is what you should look after. There are infinite algorithms for that out there.. one on SO: Collision Detection between two images in Java

Upvotes: 1

Related Questions