循环动画可绘制物件

3

我想对一些PNG图片进行动画处理,同时需要使动画循环播放。以下是我的代码:

wave.setBackgroundResource(R.drawable.wave_animation);
                frameAnimation = (AnimationDrawable)wave.getBackground();
                frameAnimation.setCallback(wave);
                frameAnimation.setVisible(true, true);
                frameAnimation.start();

这里是带有PNG图像的XML文件

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  android:oneshot="true">
    <item android:drawable="@drawable/wave_01" android:duration="200" />
    <item android:drawable="@drawable/wave_02" android:duration="200" />
    <item android:drawable="@drawable/wave_03" android:duration="200" />
    <item android:drawable="@drawable/wave_04" android:duration="200" />
</animation-list>

我也添加了android:oneshot=false,但是不起作用。

我更新了我的答案,请告诉我这是否有帮助。 - undefined
3个回答

3
只需要将 android:oneshot="false" 改为以下形式即可。
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"  android:oneshot="false">
    <item android:drawable="@drawable/wave_01" android:duration="200" />
    <item android:drawable="@drawable/wave_02" android:duration="200" />
    <item android:drawable="@drawable/wave_03" android:duration="200" />
    <item android:drawable="@drawable/wave_04" android:duration="200" />
</animation-list>

1
你上面的代码显示:
    android:oneshot="true"

这将使您的动画只运行一次。您说您尝试过android:oneshot="false",对于多次运行animation-list是必要的。因此,请将其放回。
请记住,运行动画是一个“后台”任务,无论其自身设置如何,它都会在主/前台任务完成时终止。
如果您想要其他内容,您可能需要采取不同的方法。

0

在图像上运行动画。在开始动画之前,您需要确保它尚未运行。在开始动画之前添加一个检查,如果正在运行,则停止它,然后再开始。

private void startAnimation(){

        imageView.setImageResource(R.drawable.img_animation);

        AnimationDrawable imgAnimation = (AnimationDrawable) imageView.getDrawable();

        if (imgAnimation .isRunning()) {
            imgAnimation .stop();
        }
        imgAnimation .start();

    }

img_animation.xml // 检查下面的注释

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >

    <!-- 24 frame per sec | 1000ms/24 i.e. 41ms per image -->
    <item
        android:drawable="@drawable/ball01" android:duration="41"/>
    <item
        android:drawable="@drawable/ball02" android:duration="41"/>
   ..........so on ..........
    <item
        android:drawable="@drawable/ball24" android:duration="41"/>

     <!-- Reset to first when animation stops-->
    <item
        android:drawable="@drawable/ball01"
        android:duration="10"/>

</animation-list>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接