Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

android - How to reset AnimationDrawable

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...