先贴上布局文件
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/slider_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/animal1"
android:contentDescription="@string/app_name"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"
android:onClick="showAnimation"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2"
android:onClick="showAnimation"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3"
android:onClick="showAnimation"/>
</LinearLayout>
在drawable文件夹下放置所需的图片
在drawable文件夹下建立animal1.xml animal2.xml
animal1.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/icon1" android:duration="150"></item>
<item android:drawable="@drawable/icon2" android:duration="150"></item>
<item android:drawable="@drawable/icon3" android:duration="150"></item>
<item android:drawable="@drawable/icon4" android:duration="150"></item>
<item android:drawable="@drawable/icon5" android:duration="150"></item>
<item android:drawable="@drawable/icon6" android:duration="150"></item>
</animation-list>
android:oneshot 表示是否重复播放动画
animal2.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/icon6" android:duration="150"></item>
<item android:drawable="@drawable/icon5" android:duration="150"></item>
<item android:drawable="@drawable/icon4" android:duration="150"></item>
<item android:drawable="@drawable/icon3" android:duration="150"></item>
<item android:drawable="@drawable/icon2" android:duration="150"></item>
<item android:drawable="@drawable/icon1" android:duration="150"></item>
</animation-list>
main.java内容如下
package com.example.animation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView sliderImg;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sliderImg = (ImageView)this.findViewById(R.id.slider_image);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void showAnimation(View v) {
int btnId = v.getId();
switch (btnId) {
case R.id.btn1:
sliderImg.setImageResource(R.drawable.animal1);
animationDrawable = (AnimationDrawable)sliderImg.getDrawable();
animationDrawable.start();
break;
case R.id.btn2:
animationDrawable = (AnimationDrawable)sliderImg.getDrawable();
animationDrawable.stop();
break;
case R.id.btn3:
sliderImg.setImageResource(R.drawable.animal2);
animationDrawable = (AnimationDrawable)sliderImg.getDrawable();
animationDrawable.start();
break;
default:
break;
}
}
}