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

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

问题描述

我必须为圆形倒数计时器设置动画,我通过使用 AnimationDrawable 为 ImageView 的背景设置动画来实现(每个图像都删除了相应的圆圈切片).问题是用户有能力暂停这个动画,所以我必须重新加载动画,但随后出现了问题.我尝试将动画设置为空,将 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.

第一步——初始化动画(起始帧索引为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 步 - 调用第 2 步后,我再次调用第 1 步,但 Sys.out 仍显示 60 作为当前帧数.(此处的起始索引设置为点击暂停按钮时最后隐藏的帧.)

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.)

欢迎提出任何想法.

谢谢

推荐答案

我遇到了同样的问题,即停止动画会在当前帧上停止.我希望它表现得像 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天全站免登陆