Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop AnimatorSet of ObjectAnimators in Android

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.

like image 673
MajorDanger Avatar asked Aug 25 '14 19:08

MajorDanger


People also ask

How do I turn off animations on Android?

How to enable Remove Animations. To access the Accessibility features on your Android device open the Settings app . In the Settings app, select Accessibility from the list. Now scroll down to the Display section and select Remove Animations to set the toggle switch to On.

How do I stop Valueanimator?

Use cancel() or stop() , along with a boolean field somewhere.

What is AnimatorSet Android?

android.animation.AnimatorSet. This class plays a set of Animator objects in the specified order. Animations can be set up to play together, in sequence, or after a specified delay.

How do I set up animator on Android?

Navigate to the app > res > Right-Click on res >> New >> Directory >> Name your directory as “anim”. Inside this directory, we will create our animations. For creating a new anim right click on the anim directory >> Animation Resource file and give the name to your file.


2 Answers

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

animatorSet.removeAllListeners();
animatorSet.end();
animatorSet.cancel();
like image 168
Libin Avatar answered Sep 17 '22 23:09

Libin


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);
    }
}
like image 36
Jessie A. Morris Avatar answered Sep 17 '22 23:09

Jessie A. Morris