2015-01-15 38 views
0

所以我有这样的代码,旋转的ImageView的点周围靠近屏幕的中心如何,直到检测到碰撞旋转图像

RotateAnimation anim = new RotateAnimation(0, 360,0,135); 
anim.setInterpolator(new LinearInterpolator()); 
anim.setRepeatCount(Animation.INFINITE); 
anim.setDuration(2000); 
image.startAnimation(anim); 

我也有这个代码,检查是否存在图像之间的碰撞和另一个

Rect rc_img1 = new Rect(); 
image.getDrawingRect(rc_img1); 

Rect rc_img2 = new Rect(); 
image2.getDrawingRect(rc_img2); 

if (Rect.intersects(rc_img1, rc_img2)) { 

} 

我将如何做某种循环,不断旋转图像,直到检测到碰撞,然后它将停止旋转。我似乎无法弄清楚这一点。谢谢。

回答

0

尝试使用布尔标志来检查图像是否发生了碰撞,如果不是继续旋转。它可能无法正常工作,但我认为值得尝试。

if (Rect.intersects(rc_img1, rc_img2)) { 
    isCollided = true; 
} 
+0

我怎么会让它不断地检查它是否在图像旋转 – Jake

+0

只是尝试了冲突,你可以尝试anim.cancel()或anim.reset()在你相交?我假设你的if块在一个循环中。 http://developer.android.com/reference/android/view/animation/Animation.html – Recursive

+0

正确,但它将如何继续检查动画发生时的相交?多数民众赞成在混淆我。 – Jake

0

将ObjectAnimator与UpdateListener一起使用,并检查视图是否在更新方法中发生冲突。

// Generate RotationAnimator 
final ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotationY", 0.0f, 360f); 
animation.setDuration(5000); 
animation.setRepeatCount(ObjectAnimator.INFINITE); 
animation.setInterpolator(new AccelerateDecelerateInterpolator()); 
// Add Update Listener 
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator animation) { 
       // Check if collision 
       if(isCollided){ 
        animation.cancel(); 
       } 
      } 
     }); 

animation.start(); 
+0

它适合你吗? updatelistener是完美的东西 – Mann