Stop AnimatorSet of ObjectAnimators in Android
Asked Answered
P

2

22

Im trying to stop the animation of an ImageView when a button is clicked. The animation I am using is an AnimatorSet consisting of 5 ObjectAnimators... The problem is that I can't figure how to stop and clear this animation from the ImageView when the button is clicked as btn.clearAnimation() obviously doesn't work.

Thank you for your help.

Polysyndeton answered 25/8, 2014 at 19:48 Comment(0)
N
14

You should be able to call animatorSet.cancel() to cancel the animation. Here's an example that cancels the animation 5 seconds after it starts:

package com.example.myapp2;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView) findViewById(R.id.hello_world);

        List<Animator> animations = new ArrayList<Animator>();

        animations.add(ObjectAnimator.ofInt(tv, "left", 100, 1000).setDuration(10000));
        animations.add(ObjectAnimator.ofFloat(tv, "textSize", 10, 50).setDuration(10000));

        final AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animations);
        animatorSet.start();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                animatorSet.cancel();
            }
        }, 5000);
    }
}
Nth answered 25/8, 2014 at 21:29 Comment(0)
J
40

If you had AnimatorSet listeners added, make sure you remove the listeners before cancel.

animatorSet.removeAllListeners();
animatorSet.end();
animatorSet.cancel();
Janae answered 16/1, 2015 at 3:23 Comment(2)
Thats actually a really important fact.Eydie
There is no need for end() as cancel will call it automatically. There would be a difference if you removed the listeners after the cancel as you will receive onAnimationCancel first and afterwards onAnimationEndPregnancy
N
14

You should be able to call animatorSet.cancel() to cancel the animation. Here's an example that cancels the animation 5 seconds after it starts:

package com.example.myapp2;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView) findViewById(R.id.hello_world);

        List<Animator> animations = new ArrayList<Animator>();

        animations.add(ObjectAnimator.ofInt(tv, "left", 100, 1000).setDuration(10000));
        animations.add(ObjectAnimator.ofFloat(tv, "textSize", 10, 50).setDuration(10000));

        final AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animations);
        animatorSet.start();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                animatorSet.cancel();
            }
        }, 5000);
    }
}
Nth answered 25/8, 2014 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.