嗨,我正在尝试使用 Rect.intersects 在两个矩形图像之间进行碰撞检测,但我总是得到错误的结果。如果可能的话,请尽快帮助我。我的代码如下。
img1 = (ImageView) findViewById(R.id.imgLeft);
img2 = (ImageView) findViewById(R.id.imgRight);
bmp1 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
bmp2 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Drawable d1 = new BitmapDrawable(getResources(), bmp1);
img1.setBackgroundDrawable(d1);
Drawable d2 = new BitmapDrawable(getResources(), bmp2);
img2.setBackgroundDrawable(d2);
rec1 = new Rect(0, 0, img1.getWidth(), img1.getHeight());
tw = ObjectAnimator.ofFloat(img2, "translationX", 20, -550f);
tw.setDuration(6000);
tw.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Rect recTest1 = new Rect((int)img1.getX(), (int)img1.getY(),rec1.width(),
rec2.height());
Rect recTest2 = new Rect((int)img2.getX(), (int)img2.getY(),rec2.width(),
rec2.height());
System.out.println("Test : "+Rect.intersects(recTest1, recTest2));
}
});
tw.start();
tw_One = ObjectAnimator.ofFloat(img1, "translationX", 0, 550f);
tw_One.setDuration(6000);
tw_One.start();
tw_One.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
img_one_CurrentX = img1.getX();
}
});
提前致谢