如何重置AnimationDrawable [英] How to reset AnimationDrawable

查看:1584
本文介绍了如何重置AnimationDrawable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要动画一个圆形的倒计时器,我使用AnimationDrawable一个ImageView的背景(每个图像有圈中删除的按片)动画做。问题是,用户必须暂停这个动画的能力,所以我必须重新加载动画,但随后出现了问题。我已经试过了动画设置为null,设置ImageView的为空的背景下,设定动画的知名度,但几乎没有任何帮助,因为帧数保持不变。我必须找到一个解决方法删除所有框架和增加新的或重新设置整个动画设置为默认状态。

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.

第1步 - 初始化动画(起始帧指数为0)

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);
}

步骤2 - 这里是棘手的部分,我不知道该怎么办。的事情,我已经试过:

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;

第3步 - 我再次拨打第1步,但Sys.out仍显示为60帧的当前号码后调用第2步。 (这里开始的索引设置为暂停时,按钮被窃听的最后一个隐框。)

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.

感谢

推荐答案

我有同样的问题,即停止该动画将停止在当前帧上。我希望它表现得像的iOS,在那里停会回到第一帧。该解决方案对我的作品:

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.

这篇关于如何重置AnimationDrawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆