效果图:
(1)布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="click"
android:text="开始" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="click"
android:text="停止" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/frame" />
</LinearLayout>
</LinearLayout>
(2)图片资源
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/f1"
android:duration="50"/>
<item
android:drawable="@drawable/f2"
android:duration="50"/>
<item
android:drawable="@drawable/f3"
android:duration="50"/>
<item
android:drawable="@drawable/f4"
android:duration="50"/>
<item
android:drawable="@drawable/f5"
android:duration="50"/>
</animation-list>
(3)MianActivity
public class MainActivity extends Activity {
private ImageView imageView;
private AnimationDrawable aDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
aDrawable= (AnimationDrawable) imageView.getDrawable();
}
public void click(View v){
switch (v.getId()){
case R.id.button1:
aDrawable.start();
break;
case R.id.button2:
aDrawable.stop();
break;
}
}
}