如何重置AnimationDrawable

发布于 2024-12-08 21:42:41 字数 1194 浏览 1 评论 0原文

我必须为圆形倒计时器设置动画,我是通过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情绪少女 2024-12-15 21:42:41

一旦调用 stop(),就会将 (AnimationDrawable) timerAnimation 发送到第一帧:

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

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

timerAnimation.stop();
timerAnimation.selectDrawable(0);
并安 2024-12-15 21:42:41

我遇到了同样的问题,停止动画会停止在当前帧上。我希望它像 iOS 一样,停止时会返回到第一帧。这个解决方案对我有用:

((AnimationDrawable)(someButton.getBackground())).stop();
someButton.setBackgroundDrawable(null);
someButton.setBackgroundResource(R.drawable.animation);

这首先会阻止它(可能没有必要)。然后它会杀死背景动画。最后,它重新创建背景。

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.

失去的东西太少 2024-12-15 21:42:41

另一种可能的解决方案是使第一帧和最后一帧具有相同的图像,并执行以下操作而不是调用 stop() 方法。

((AnimationDrawable)(someButton.getBackground())).setOneShot(true);

如果有人进一步研究这个问题。还有一个方法叫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.

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

夜还是长夜 2024-12-15 21:42:41

您可以尝试public boolean setVisible(booleanvisible,booleanrestart),它会播放一次动画。例如:

animationDrawable.setVisible(false, true);

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

animationDrawable.setVisible(false, true);
压抑⊿情绪 2024-12-15 21:42:41

我会 setVisibility(true, true ):

timerAnimation.stop();
timerAnimation.setVisibility(true, true);
  • visible boolean: 如果可见则为 true,否则为 false
  • restart boolean:
    当可见时,true强制动画从第一帧重新开始

I would setVisibility(true, true):

timerAnimation.stop();
timerAnimation.setVisibility(true, true);
  • visible boolean: true if visible, false otherwise
  • restart boolean:
    when visible, true to force the animation to restart from the first frame
無處可尋 2024-12-15 21:42:41

android.view.animation.Animation.reset() 不起作用?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文