To implement Drawable Animation in Android follow the steps :-
1) Prepare a series of images with the individual frames of your animation. Add the images to your projects drawable folder.
2) Declare a XML file that defines the animation sequence that holds the list of drawables. Create a new file named pro_frame.xml and paste it to your project resource res/anim or res/drawable folder.
pro_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/run1" android:duration="80" />
<item android:drawable="@drawable/run2" android:duration="80" />
<item android:drawable="@drawable/run3" android:duration="80" />
<item android:drawable="@drawable/run4" android:duration="80" />
<item android:drawable="@drawable/run5" android:duration="80" />
<item android:drawable="@drawable/run6" android:duration="80" />
<item android:drawable="@drawable/run7" android:duration="80" />
<item android:drawable="@drawable/run8" android:duration="80" />
<item android:drawable="@drawable/run1" android:duration="210" />
<item android:drawable="@drawable/run2" android:duration="210" />
<item android:drawable="@drawable/run3" android:duration="210" />
<item android:drawable="@drawable/run4" android:duration="210" />
<item android:drawable="@drawable/run5" android:duration="210" />
<item android:drawable="@drawable/run6" android:duration="210" />
<item android:drawable="@drawable/run7" android:duration="210" />
<item android:drawable="@drawable/run8" android:duration="210" />
</animation-list>
3) Now declare an image view to your activity layout where the animation will be shown. In this example, I have declared an image view and two buttons Start and Stop to control the animation.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.androinahi ..................d.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Start" />
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Stop" />
</LinearLayout>
</RelativeLayout>
4) Now that we are ready with the animation sequence, we need to add this Drawable as a background resource for our ImageView in our MainActivity.
MainActivity.java
public class MainActivity extends Activity {
private ImageView mImageView;
private Button mStartButton,mStopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView=(ImageView)findViewById(R.id.imageView); // Type casting the Image View
mStartButton=(Button)findViewById(R.id.start);
mStopButton=(Button)findViewById(R.id.stop);
mImageView.setBackgroundResource(R.drawable.pro_frame);// Setting animation_list.xml as the background of the image view
final AnimationDrawable frameAnimation=(AnimationDrawable) mImageView.getBackground();// Type casting the Animation drawable
frameAnimation.setOneShot(true);//set true if you want to animate only once
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
frameAnimation.start();
}
});
mStopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
frameAnimation.stop();
}
});
}
0 Comment(s)