Loop animation drawable

我的未来我决定 提交于 2019-12-23 01:12:07

问题


I'm trying to animate some png and i would loop the animation. This is my code:

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

and here the xml with the png

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

I added also the android:oneshot=false but doesn't work.


回答1:


Your code above shows

    android:oneshot="true"

Which will make your animation run Once and only Once.

You say you tried android:oneshot="false".
That will be essential for running through the animation-list more than once. So put it back.

Keep in mind that running animation is a 'background' task which will terminate when the primary/foreground task completes regardless of its own settings.

If you want something else you might need to take a different approach.




回答2:


just change android:oneshot="false" to be like this

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



回答3:


This is running an animation on image. Before starting the animation you need to make sure its not already running. Add a check before starting animation, if its running then stop it and then start it.

private void startAnimation(){

        imageView.setImageResource(R.drawable.img_animation);

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

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

    }

img_animation.xml // Check for comments in below

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


来源:https://stackoverflow.com/questions/31035565/loop-animation-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!