![Start and Stop frame animation with AnimationDrawable Start and Stop frame animation with AnimationDrawable](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhGLDKfJxxYNNZseN6iL6slWvND0Z3MX_Q7uzbXZBL2-7ExpeQTXgxB48xshnvYacxKlzMpw_KsqO7TwgJyX5gcUV9QhzGqgXgqslB2lsaLWnOde5TuMVsjG70Mriww32uDVrUV4Wz5PZl/s400/AndroidAnimation_02.png)
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 startAnimation = (Button)findViewById(R.id.startanimation);
Button stopAnimation = (Button)findViewById(R.id.stopanimation);
startAnimation.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!myAnimationDrawable.isRunning()){
myAnimationDrawable.start();
}
}});
stopAnimation.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(myAnimationDrawable.isRunning()){
myAnimationDrawable.stop();
}
}});
}
}
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/startanimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Animation" />
<Button
android:id="@+id/stopanimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Animation" />
<ImageView
android:id="@+id/myanimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@anim/anim_android"
/>
</LinearLayout>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZsNHnMVLKGkriPAh_38qMgbwcNAHGWktiikeRKL9yZjIf0pfWXOSpgWaXXyGyLjKyt6HhIyGocvFqqk3__GgEpRWA_ply9Yrs5ISGt0_ZUThmVJ4oZHqQYntmdqanQJq-1aRb5do83BKf/s200/icons_download.gif)
next:
- Set alpha of AnimationDrawable