Search code examples
androidandroid-animationandroid-imageviewobjectanimator

ImageView ObjectAnimator transition + collision detection during transition


I have a "track" that goes for 50 "lenghts" and I have imageView that changes position on click for 5 "lenghts" at a time. That transition I handle with animation like this:

  anima = ObjectAnimator.ofFloat(bar, "translationX", position*pix);
  anima.setDuration(500);
  anima.start();

At the end of the track I have another imageview that I want to detect collision with so I after animation I do this:

anima.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (Rect.intersects(n1, barRect)) {
                   //Stuff after collision
                }


}

My question is, is there a way to detect collision during this animation so I stop it if collision is somewhere between those "5 lenght jumps"


Solution

  • Thanks to @NikolaDespotski I've managed to solve the problem by implementig onUpdateListener for my ObjectAnimator object like this:

     anima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                       //Do collision detection here
                }
    
      });