Animator didn’t stop after calling end() method

In the last days I run into a problem: In my layout  I have some Views. To each View I used an Animator object in order to perform infinite animation with them. The animation is pretty simple, and this how I created it:

private static AnimatorSet createLeftTiltAnimator(final View v) {
    AnimatorSet animatorSet = new AnimatorSet();
    final ObjectAnimator scaleX = new ObjectAnimator();
    final ObjectAnimator rotateLeft = new ObjectAnimator();
    scaleX.setPropertyName("scaleX");
    scaleX.setRepeatMode(ValueAnimator.REVERSE);
    scaleX.setRepeatCount(ValueAnimator.INFINITE);
    scaleX.setFloatValues(1f, 0.92f);
    scaleX.setTarget(v);
    rotateLeft.setPropertyName("rotation");
    rotateLeft.setRepeatMode(ValueAnimator.REVERSE);
    rotateLeft.setRepeatCount(ValueAnimator.INFINITE);
    rotateLeft.setFloatValues(0, -7.5f);
    rotateLeft.setTarget(v);
    animatorSet.playTogether(scaleX, rotateLeft);
    animatorSet.setDuration(400);
    return animatorSet;
}

Inside my logic, whenever I needed I just called on that Animator object with:

    animatorSet.start();

Well, it worked well, but the problem was when I wanted to end the animation, after I called:

    animatorSet.end();

Some of the Views just stayed with the animation, and I didn’t realise why it happens.
After some analysing, I figured that on some of the View Animators I called twice:

    animatorSet.start();

In this situation, calling end() just didn’t help. So what the solution to prevent such incidents? Well, actually it is very simple, before calling the start() I just added a check if the Animator not already running:

if (!animatoreSet.isRunning())
    viewHolder.iconAnimatoreSet.start();

Leave a Reply

Your email address will not be published. Required fields are marked *