How to clear animation in Android?

In my Android app, I am doing a button jitter using this code

public void FlashButton() { final Button button = ((Button)findViewById(R.id.button_message)); final Animation moveright = AnimationUtils.loadAnimation(this, R.anim.moveright); final Animation moveleft = AnimationUtils.loadAnimation(this, R.anim.moveleft); final Animation moveright2 = AnimationUtils.loadAnimation(this, R.anim.moveright2); AnimationListener animation1Listener = new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { button.startAnimation(moveleft); } }; AnimationListener animation2Listener = new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { button.startAnimation(moveright2); } }; AnimationListener animation3Listener = new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { button.startAnimation(moveright); } }; moveright.setAnimationListener(animation1Listener); moveleft.setAnimationListener(animation2Listener); moveright2.setAnimationListener(animation3Listener); button.startAnimation(moveright); } 

This code sets up three animations sequentially in a loop forever.

But how to stop the animation and make the button normal again?

I tried .clearAnimation(); but that didn't work ...

Somebody knows?

Thanks.

+4
source share
2 answers

Try to cancel the animation and see something like

  moveright.cancel(); moveright2.cancel(); moveleft.cancel(); 

Or try reset, and

  moveright.reset(); 

similar to others

+5
source

If you cannot solve the problem for any reason, I found that starting a new animation like reset can help:

 .... <scale android:duration="1" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="1" android:toYScale="1" android:fillAfter="true" 

/ "> ....

0
source

Source: https://habr.com/ru/post/1496940/


All Articles