package com.example.animation;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
AnimationDrawable an;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setBackgroundResource(R.anim.my_animation);
an = (AnimationDrawable) iv.getBackground();
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
an.start();
}
});
}
}
<?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/flash201" android:duration = "400"/>
<Item android:drawable="@drawable/flash202" android:duration = "400"/>
<Item android:drawable="@drawable/flash203" android:duration = "400"/>
<Item android:drawable="@drawable/flash204" android:duration = "400"/>
<Item android:drawable="@drawable/flash205" android:duration = "400"/>
</animation-list>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginLeft="43dp"
android:layout_marginTop="50dp"
android:text="Button" />
</RelativeLayout>