user1761680
user1761680

Reputation:

How to stop an animation (from ObjectAnimator)

Below is the code belonging to my activity where I have a button (someButton) which, when clicked, starts an animation on another view, more specifically, a progressbar view, on that same activity:

// [...]
someButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        ObjectAnimator animation = ObjectAnimator.ofInt(progressView, "progress", 0, 2000);
        animation.setDuration(2000);
        animation.setInterpolator(new DecelerateInterpolator());
        animation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // some code to execute when the animation ends
            }
        });
        animation.start();
    }
});
// [...]

Now, at some point I may need to stop that progress bar view's animation (when the user taps a stop button, for example). I called progressView.clearAnimation() to stop the animation but got no success. I have also noticed that progressView.getAnimation() returns null... And when I make ObjectAnimator animation variable final and move it outside the someButton.setOnClickListener, so that I can access it later to stop the animation, I get the following exception when I tap on someButton (just like what happens here):

java.lang.NullPointerException
   at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:505)
   at android.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:487)
   at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:517)
   at android.animation.ValueAnimator.start(ValueAnimator.java:936)
   at android.animation.ValueAnimator.start(ValueAnimator.java:946)
   at android.animation.ObjectAnimator.start(ObjectAnimator.java:465)
   at android.animation.AnimatorSet$1.onAnimationEnd(AnimatorSet.java:579)
   at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1056)
   at android.animation.ValueAnimator.access$400(ValueAnimator.java:50)
[...]

And the code that throws that exception is:

// [...]

final ObjectAnimator animation = ObjectAnimator.ofInt(progressView, "progress", 0, 2000);
animation.setDuration(2000);
animation.setInterpolator(new DecelerateInterpolator());
animation.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        // some code to execute when the animation ends
    }
});

someButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        animation.start();// NPE occurs here!
    }
});
// [...]

What am doing wrong here?? How can one stop that animation???

Upvotes: 3

Views: 6515

Answers (4)

Rasoul Miri
Rasoul Miri

Reputation: 12222

you can use this way:

valueAnimator?.removeAllUpdateListeners()
valueAnimator?.removeAllListeners()
valueAnimator?.end()
valueAnimator?.cancel()
valueAnimator = null

Upvotes: 2

Waqas Khan
Waqas Khan

Reputation: 1

try this

anim.cancel();
anim.end();
anim.removeAllListeners();

Upvotes: 0

Meet Vora
Meet Vora

Reputation: 2818

Try: animation.setCurrentPlayTime(0);

Upvotes: 0

Udi Oshi
Udi Oshi

Reputation: 6867

First you can Check this link for better examples.

I would also suggest you to not stop animation (Unless you're implementing it right with Extended view, handlers with postDelay and thread sleeps. If some logic is done just remove the view/open another screen , specially if it progressView, it doesn't need to be shown but not spinning, just remove it's visibility.

Good luck

Upvotes: 0

Related Questions