Animationdrawable Example: Stimulate Upward One’s Heed Terminate Of Animation To Showtime Around Other Animation

Posted on

This around other instance of “Detect AnimationDrawable.isRunning() to toggle start/stop animation” to start around other animation when the rootage animation end.


There are no listener method to live on called when animation of AnimationDrawable end. So nosotros bring to decide the duration past times adding all frame duration of the animation, past times calling our getAnimationDuration(AnimationDrawable src), as well as therefore start a delayed runnable() to start around other animation.

Download the PNGs to drawable folder:

Create animations in /res/anim/ folder:

anim_android.xml

<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false" >      <item         android:drawable="@drawable/android_1"         android:duration="100"/>     <item         android:drawable="@drawable/android_2"         android:duration="100"/>     <item         android:drawable="@drawable/android_3"         android:duration="100"/>     <item         android:drawable="@drawable/android_4"         android:duration="100"/>     <item         android:drawable="@drawable/android_5"         android:duration="100"/>     <item         android:drawable="@drawable/android_6"         android:duration="100"/>     <item         android:drawable="@drawable/android_7"         android:duration="100"/>  </animation-list> 

anim_android2.xml

<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false" >      <item         android:drawable="@drawable/android_7"         android:duration="100"/>     <item         android:drawable="@drawable/android_6"         android:duration="100"/>     <item         android:drawable="@drawable/android_5"         android:duration="100"/>     <item         android:drawable="@drawable/android_4"         android:duration="100"/>     <item         android:drawable="@drawable/android_3"         android:duration="100"/>     <item         android:drawable="@drawable/android_2"         android:duration="100"/>     <item         android:drawable="@drawable/android_1"         android:duration="100"/>  </animation-list> 

/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.example.androidanimation.MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/startstopanimation"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="Start/Stop Animation" />      <ImageView         android:id="@+id/myanimation1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@anim/anim_android" />     <ImageView         android:id="@+id/myanimation2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@anim/anim_android2" />  </LinearLayout> 

MainActivity.java

package com.example.androidanimation;  import android.support.v7.app.ActionBarActivity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.ImageView;  populace degree MainActivity extends ActionBarActivity {    AnimationDrawable myAnimationDrawable1, myAnimationDrawable2;  ImageView myAnimation1, myAnimation2;    Handler handlerAnim2;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);    myAnimation1 = (ImageView) findViewById(R.id.myanimation1);   myAnimation2 = (ImageView) findViewById(R.id.myanimation2);      myAnimationDrawable1 = (AnimationDrawable) myAnimation1.getDrawable();   myAnimationDrawable1.stop();   myAnimationDrawable1.setOneShot(true);      myAnimationDrawable2 = (AnimationDrawable) myAnimation2.getDrawable();   myAnimationDrawable2.stop();   myAnimationDrawable2.setOneShot(true);    Button startStopAnimation = (Button) findViewById(R.id.startstopanimation);   startStopAnimation.setOnClickListener(new Button.OnClickListener() {     @Override    populace void onClick(View v) {     if (myAnimationDrawable1.isRunning()) {      myAnimationDrawable1.stop();     } else {      myAnimationDrawable1.run();            handlerAnim2 = novel Handler();      handlerAnim2.postDelayed(       novel Runnable(){         @Override        populace void run() {         myAnimationDrawable1.stop();         myAnimationDrawable2.run();        }},        getAnimationDuration(myAnimationDrawable1));     }    }    });        }    mortal int getAnimationDuration(AnimationDrawable src){   int dur = 0;   for(int i=0; i<src.getNumberOfFrames(); i++){    dur += src.getDuration(i);   }   supply dur;  }  } 

 to start around other animation when the rootage animation halt AnimationDrawable example: decide halt of animation to start around other animationDownload the files.