开发者

How to replay a one shot animation

开发者 https://www.devze.com 2023-04-01 15:44 出处:网络
I want to make a one shot animation but be able to play it as many times as I want. Right now it plays only the first time.

I want to make a one shot animation but be able to play it as many times as I want. Right now it plays only the first time.

.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/p1_ch1" android:oneshot="true">
    <item 
      android:drawable="@drawable/p1_ch1_5" 
      android:duration="500"/>
    <item 
      android:drawable="@drawable/p1_ch1_4" 
      android:duration="1000"/>
    <item 
      android:drawable="@drawable/p1_ch1_5" 
      android:duration="500"/>
</animation-list>

.java:

public void 开发者_高级运维handler_p1_ch1_5 (View target){
      ImageView iv = (ImageView)findViewById(R.id.p1_ch1_5);
      AnimationDrawable aw = (AnimationDrawable)iv.getBackground();
      aw.start();
}


Just call aw.stop() before aw.start()


Use this:

frameAnimation.setOneShot(true);


Change

android:oneShot="true" to
android:oneShot="false"


If you want to replay a one shot animation after previous animation finish you can do like

final AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable();
if (!animationDrawable.isRunning()) {

    int totalFrameDuration = 0;
    for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
        totalFrameDuration += animationDrawable.getDuration(i);
    }

    animationDrawable.start();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            animationDrawable.stop();
        }
    }, duration);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消