Android碰撞检测?

时间:2014-07-24 16:00:04

标签: java android imageview collision

以下是我用于移动ImageView的代码:

int x=brickimg.getRight()-brickimg.getLeft();
   int y=brickimg.getBottom()-brickimg.getTop();
   final TranslateAnimation translate = new TranslateAnimation(
           Animation.ABSOLUTE,x1, Animation.ABSOLUTE,
           -2000, Animation.ABSOLUTE,0,
           Animation.ABSOLUTE,y);
   translate.setDuration(1200);//speed of the animation
   translate.setFillEnabled(true);
   translate.setFillAfter(true); 
  //brickimg.startAnimation(translate);

//2nd brick animation

  int x1=brickimg2.getRight()-brickimg2.getLeft();
  int y1=brickimg2.getBottom()-brickimg2.getTop();
  final TranslateAnimation secondone = new TranslateAnimation(
          Animation.ABSOLUTE,x1, Animation.ABSOLUTE,
          -2000, Animation.ABSOLUTE,0,
          Animation.ABSOLUTE,y1);
  secondone.setDuration(1200);//speed of the animation
  secondone.setFillEnabled(true);
  secondone.setFillAfter(true); 

这是两张图片。以下是我尝试用于碰撞检测的代码:

Rect rc1 = new Rect();
brickimg.getDrawingRect(rc1);
Rect rc2 = new Rect();
playerimage.getDrawingRect(rc2);
if (rc1.intersect( rc2)) { 
  playerimage.setVisibility(View.GONE);
}

然而,唯一发生的事情是当应用程序启动时,playerimage变为不可见。它不会再次变得不可见。其中一个图像不移动,另一个图像在屏幕上移动到不移动的图像(使用我上面的代码)。不移动的图像立即变为不可见,而不是第二个图像接触它时。我试图做到这一点,当它接触到运动图像时它会做一些事情。除此之外,我还尝试过.getHitRect,我也试过.intersects而不是.intersect,但没有一个有效。运动图像是ImageView,而不是精灵。还有其他选择吗?

0 个答案:

没有答案