2010-08-18 68 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的半徑,這會更快然後檢查圖像進行重疊。