如何旋转图像直到检测到碰撞

de90aj5v  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(340)

所以我有一个代码,它围绕屏幕中心附近的一个点旋转一个图像视图

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)) {

}

我如何做某种循环,不断旋转图像,直到检测到碰撞,然后停止旋转。我好像想不通。谢谢。

brqmpdu1

brqmpdu11#

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

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

iih3973s2#

将objectanimator与updatelistener一起使用,并在update方法中检查视图是否冲突。

// 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();

相关问题