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;

    }
}


嗨,我正在使用上面的代码显示位图。我也移动了该位图。现在我的问题是我如何将位图与另一个位图进行比较。
请给我一些建议。谢谢

最佳答案

您应该注意“冲突检测”。那里有无限的算法。.关于SO:Collision Detection between two images in Java

关于android - 比较 Canvas 中的位图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3511211/

10-11 22:28
查看更多