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;
}
}
您好我使用上面的代码显示位图。我也移动该位图。现在我的问题是我如何将位图与另一个位图进行比较。 请给我一些建议。谢谢你提前
答案 0 :(得分: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的半径,这将比检查图像重叠更快。
答案 1 :(得分:1)
“碰撞检测”是你应该照顾的。那里有无限的算法......一个在SO上:Collision Detection between two images in Java