How to reset AnimationDrawable

坚强是说给别人听的谎言 提交于 2019-11-28 21:07:27

I had the same problem where stopping the animation would stop on the current frame. I wanted it to behave like iOS, where stopping would go back to the first frame. This solution works for me:

((AnimationDrawable)(someButton.getBackground())).stop();
someButton.setBackgroundDrawable(null);
someButton.setBackgroundResource(R.drawable.animation);

This first stops it (probably not necessary). Then it kills the background animation. Finally, it recreates the background.

This will send the (AnimationDrawable) timerAnimation to the first frame as soon as stop() has been called:

timerAnimation.stop();
timerAnimation.selectDrawable(0);

Another possible solution would be to have the same image for the first frame and last frame and do the following instead of calling the stop() method.

((AnimationDrawable)(someButton.getBackground())).setOneShot(true);

if anyone does look into this any further. There is also a method called setVisible(boolean visible, boolean restart). However, that did not work for myself.

M.L.Lee

You can try public boolean setVisible (boolean visible, boolean restart) and it will play the animation once. For example:

animationDrawable.setVisible(false, true);

android.view.animation.Animation.reset() doesnt work?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!