循环动画drawable

时间:2015-06-24 19:41:05

标签: java android xml android-drawable animationdrawable

我试图动画一些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但不起作用。

3 个答案:

答案 0 :(得分:1)

上面的代码显示

    android:oneshot="true"

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

你说你试过android:oneshot =“false”。
这对于不止一次运行动画列表至关重要。所以把它放回去。

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

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

答案 1 :(得分:1)

只需将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>

答案 2 :(得分: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>