Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How can I stop an infinite animation applied on an ImageView?

I have an ImageView on which I have applied a rotate animation. Since I want the rotation to go on continuously, I gave the repeatCount as infinite in my rotate.xml:

android:repeatCount="infinite" 

In onCreate(), I load the animation and start it.

Animation myAnim    = AnimationUtils.loadAnimation(this, R.anim.rotate); objectImg.startAnimation(myAnim);  

When a button is pressed, the rotation must stop. Hence in my onClick(), I called clearAnimation().

objectImg.startAnimation(myAnim);  

My simple question is whether stopping the animation is the right thing to do. I assume clearAnimation() corresponds to loadAnimation(), but there is no stopAnimation() that corresponds to startAnimation().

like image 431
kiki Avatar asked Oct 12 '10 10:10

kiki


People also ask

How to stop animation in Android java?

clearAnimation() To stop the animation that going on in a loop.

What is Property animation in Android?

A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.

What is object animator in Android?

ObjectAnimator is a Subclass of ValueAnimator, which allows us to set a target object and object property to animate. It is an easy way to change the properties of a view with a specified duration. We can provide the end position and duration of the animation.

What is drawable animation in Android?

Drawable animation lets you load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. The AnimationDrawable class is the basis for Drawable animations.


2 Answers

Use clearAnimation() to stop an animation. There is no loadAnimation() on View.

like image 57
CommonsWare Avatar answered Sep 21 '22 17:09

CommonsWare


You can also call anim.cancel(); but you should also call anim.reset(); immediately after it. Then when you want to start it again, just call startAnimation on the view.

like image 32
John J Smith Avatar answered Sep 18 '22 17:09

John J Smith