2010-08-18 26 views
1
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; 

    } 
} 

嗨我正在使用上面的代码来显示位图。我也移动该位图。现在我的问题是我如何与另一个位图比较位图。 请给我一些suggestions.Thanks提前比较画布中的位图

+0

指定'比较'请...你想知道,如果位图是否相等或如果他们在同一个地方? – WarrenFaith 2010-08-18 10:47:25

+0

当我们将一个bimap移动到另一个位图附近时,如果两个位图相同,则位图移动会变为假 – Pinki 2010-08-18 10:53:25

回答

1

“碰撞检测”是你应该照顾的。有那里有无限的算法..一个SO:Collision Detection between two images in Java

+0

实际上,我希望我的应用程序的输出看起来像(请参阅此网站)http://www.youtube.com/watch? v = 46E85EXHvW0。在那个大脑拼图。首先我显示的图像拼写不可聚焦,另一行我显示图像拼写错误。当我clcik特定图像时,图像移动到上面一行的第一位,如果它正确的地方。给出一些代码建议 – Pinki 2010-08-18 11:23:52

+0

信念我不明白碰撞检测。请给我一些excursmle应用whatevr你不用这个概念。提前感谢 – Pinki 2010-08-18 11:46:00

1

位图是平等的,当我们移动一个bimap的附近花药位图,如果两个相同的位图动成为假

假设你想要在两个圆之间进行碰撞检测,这可以通过获得两个中心之间的距离来执行。 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)); 
} 

然后检查是否该值小于CIRCLE1的半径+ CIRCLE2的半径,这会更快然后检查图像进行重叠。