工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/617396/a0e69f29-cfc8-32dc-8da5-7efae91635cc.png[/img]
[/img]
main.xml
/res/drawable-hdpi/move.xml
AnimationDemo5Activity
[img]
[img]http://dl.iteye.com/upload/attachment/617396/a0e69f29-cfc8-32dc-8da5-7efae91635cc.png[/img]
[/img]
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
></Button>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
/>
</LinearLayout>
/res/drawable-hdpi/move.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/p1" android:duration="500"></item>
<item android:drawable="@drawable/p2" android:duration="500"></item>
<item android:drawable="@drawable/p3" android:duration="500"></item>
<item android:drawable="@drawable/p4" android:duration="500"></item>
</animation-list>
AnimationDemo5Activity
package cxt.demo;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class AnimationDemo5Activity extends Activity {
/** Called when the activity is first created. */
Button start = null;
ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.image);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setBackgroundResource(R.drawable.move);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
animationDrawable.start();
}
});
}
}