2

我正在尝试为一些 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 但不起作用。

4

3 回答 3

2

改成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>
于 2016-01-20T11:29:26.583 回答
1

您上面的代码显示

    android:oneshot="true"

这将使您的动画运行一次且仅一次。

你说你试过 android:oneshot="false"。
这对于多次运行动画列表至关重要。所以放回去。

请记住,正在运行的动画是一个“后台”任务,它将在主/前台任务完成时终止,而不管其自身的设置如何。

如果你想要别的东西,你可能需要采取不同的方法。

于 2015-06-24T20:49:37.940 回答
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>
于 2015-06-24T19:49:34.490 回答