How to reset AnimationDrawable

2019-01-22 18:12发布

I have to animate a circular count down timer and I'm doing it by animating the background of an ImageView using AnimationDrawable (each image has the according slice of the circle removed). The problem is that the user has the ability to pause this animation, so I have to reload the animation but then a problem appears. I've tried setting the the animation to null, setting the background of the ImageView to null, setting the visibility of the animation, but practically nothing helped, because number of frames remains the same. I have to find a workaround for deleting all frames and adding new ones or to reset the whole animation to the default state.

Step 1 - initializing the animation (starting frame index is 0)

timerView.setBackgroundResource(R.drawable.timer_switch);
timerAnimation = (AnimationDrawable)timerView.getBackground();

System.out.println("Number of frames: " + timerAnimation.getNumberOfFrames());

for (int frameIndex = startingFrameIndex; frameIndex < 60; frameIndex++) {
    if (frameIndex < 10) {
        uri = "drawable/timer_0000" + frameIndex;
    } else {
        uri = "drawable/timer_000" + frameIndex;
    }

    imageResourceId = getResources().getIdentifier(uri, null, getPackageName());
    timerAnimation.addFrame(getResources().getDrawable(imageResourceId), timerImageFrameDuration);
}

Step 2 - here's the tricky part where I don't know what to do. Things that I've tried:

timerAnimation.stop();
timerAnimation.setCallback(null);
timerAnimation.setVisibility(true, false);
timerAnimation = null;

Step 3 - after calling step 2 I call step 1 again, but the Sys.out still displays 60 as the current number of frames. (starting index here is set to the last hidden frame when pause button was tapped.)

Any idea is welcomed.

Thanks

5条回答
forever°为你锁心
2楼-- · 2019-01-22 18:23

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

timerAnimation.stop();
timerAnimation.selectDrawable(0);
查看更多
来,给爷笑一个
3楼-- · 2019-01-22 18:29

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.

查看更多
成全新的幸福
4楼-- · 2019-01-22 18:29

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

animationDrawable.setVisible(false, true);
查看更多
等我变得足够好
5楼-- · 2019-01-22 18:37

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

查看更多
冷血范
6楼-- · 2019-01-22 18:47

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.

查看更多
登录 后发表回答