如何重置AnimationDrawable
我必须为圆形倒计时器设置动画,我是通过使用 AnimationDrawable 对 ImageView 的背景进行动画处理来实现的(每个图像都删除了相应的圆切片)。问题是用户可以暂停这个动画,所以我必须重新加载动画,但随后出现了问题。我尝试将动画设置为空,将 ImageView 的背景设置为空,设置动画的可见性,但实际上没有任何帮助,因为帧数保持不变。我必须找到一种解决方法来删除所有帧并添加新帧或将整个动画重置为默认状态。
步骤 1 - 初始化动画(起始帧索引为 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 - 这是棘手的部分,我不知道该怎么做。我尝试过的事情:
timerAnimation.stop();
timerAnimation.setCallback(null);
timerAnimation.setVisibility(true, false);
timerAnimation = null;
步骤 3 - 调用步骤 2 后,我再次调用步骤 1,但 Sys.out 仍然显示 60 作为当前帧数。 (此处的起始索引设置为点击暂停按钮时的最后一个隐藏帧。)
欢迎任何想法。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一旦调用
stop()
,就会将 (AnimationDrawable)timerAnimation
发送到第一帧:This will send the (AnimationDrawable)
timerAnimation
to the first frame as soon asstop()
has been called:我遇到了同样的问题,停止动画会停止在当前帧上。我希望它像 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:
This first stops it (probably not necessary). Then it kills the background animation. Finally, it recreates the background.
另一种可能的解决方案是使第一帧和最后一帧具有相同的图像,并执行以下操作而不是调用 stop() 方法。
如果有人进一步研究这个问题。还有一个方法叫setVisible(booleanvisible,booleanrestart)。然而,这对我自己不起作用。
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.
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.
您可以尝试
public boolean setVisible(booleanvisible,booleanrestart)
,它会播放一次动画。例如:You can try
public boolean setVisible (boolean visible, boolean restart)
and it will play the animation once. For example:我会
setVisibility(true, true )
:visible
boolean: 如果可见则为 true,否则为 falserestart
boolean:当可见时,true强制动画从第一帧重新开始
I would
setVisibility(true, true)
:visible
boolean: true if visible, false otherwiserestart
boolean:when visible, true to force the animation to restart from the first frame
android.view.animation.Animation.reset() 不起作用?
android.view.animation.Animation.reset() doesnt work?