Wednesday, June 25, 2014

Detect AnimationDrawable.isRunning() to toggle start/stop animation

This example base on the old example "Start and Stop frame animation with AnimationDrawable". When button clicked, it read AnimationDrawable.isRunning() and call AnimationDrawable.start() or AnimationDrawable.stop() to toggle the animation.


Download the png files and copy /res/anim/anim_android.xml here.

/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:id="@+id/startstopanimation"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start/Stop Animation" />

    <ImageView 
        android:id="@+id/myanimation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@anim/anim_android"
        />

</LinearLayout>

AndroidAnimationActivity.java
package com.exercise.AndroidAnimation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidAnimationActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
        final AnimationDrawable myAnimationDrawable 
         = (AnimationDrawable)myAnimation.getDrawable();
        
        Button startStopAnimation = (Button)findViewById(R.id.startstopanimation);
        
        startStopAnimation.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    
    if(myAnimationDrawable.isRunning()){
     myAnimationDrawable.stop(); 
    }else{
     myAnimationDrawable.start(); 
    }

   }});

    }
}


download filesDownload the files.

Next:
Determine end of animation to start another animation

2 comments:

Carlos Eduardo Vásquez said...

hello,
how can I run a second animation after the first one has finish?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
// Type casting the Image View
view = (ImageView) findViewById(R.id.imagen);
// Setting animation_list.xml as the background of the image view
view.setBackgroundResource(R.drawable.entrada01);
// Type casting the Animation drawable
frameAnimation = (AnimationDrawable) view.getBackground();
}

Andr.oid Eric said...

hello Carlos Eduardo Vásquez,

Please check AnimationDrawable example: determine end of animation to start another animation.