First look
Below are two animated XML files
Animation1.xml
<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration represents the length of time used to display each image in the animation through the item tag --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "true"> <item Android: drawable = "@ drawable/icon1" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon2" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon3" Android: duration = "250"> </item> <item Android: drawable = "@ drawable/icon4" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon5" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon6" Android: duration = "250"> </item> </animation-List>
Animation2.xml
<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration represents the length of time used to display each image in the animation through the item tag --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "true"> <item Android: drawable = "@ drawable/icon6" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon5" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon4" Android: duration = "250"> </item> <item Android: drawable = "@ drawable/icon3" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon2" Android: Duration = "250"> </item> <item Android: drawable = "@ drawable/icon1" Android: duration = "250"> </item> </animation-List>
XML layout file:
<? 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"> <imageview Android: id = "@ + ID/animationiv" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: padding = "5px" Android: src = "@ anim/animation1" Android: scaletype = "center"/> <button Android: Id = "@ + ID/buttona" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: padding = "5px" Android: onclick = "onclick" Android: text = "sequence Display"/> <button Android: id = "@ + ID/buttonb" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: padding = "5px" Android: onclick = "onclick" Android: TEXT = "stop"/> <button Android: Id = "@ + ID/buttonc" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: padding = "5px" Android: onclick = "onclick" Android: text = "reverse display"/> </linearlayout>
Java code:
package com.example.animationdemo;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.Window;import android.widget.ImageView;public class MainActivity extends Activity { private ImageView animationIV; private AnimationDrawable animationDrawable; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); animationIV = (ImageView) findViewById(R.id.animationIV); } public void onClick(View v) { switch (v.getId()) { case R.id.buttonA: start_animation(R.anim.animation1); break; case R.id.buttonB: stop_animation(); break; case R.id.buttonC: start_animation(R.anim.animation2); break; default: break; } } private void start_animation(int id){ animationIV.setImageResource(id); animationDrawable = (AnimationDrawable) animationIV .getDrawable(); animationDrawable.start(); } private void stop_animation(){ animationDrawable.stop(); }}
Http://download.csdn.net/detail/wenwei19861106/4856995 (csdn)