示例
具体代码
girl「drawable部分」
girl_1~4就是对应的图片,这里作者就不专门提供了
<?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/girl_1" android:duration="100"></item>
<item android:drawable="@drawable/girl_2" android:duration="100"></item>
<item android:drawable="@drawable/girl_3" android:duration="100"></item>
<item android:drawable="@drawable/girl_4" android:duration="100"></item>
</animation-list>
girldonghua「layout部分」
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/girl"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="25dp">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:layout_marginLeft="50dp"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:layout_marginLeft="45dp"/>
</LinearLayout>
</LinearLayout>
Main
这里只截取了主要部分
super.onCreate(savedInstanceState);
setContentView(R.layout.girldonghua);
iv=(ImageView) findViewById(R.id.iv);
start =(Button) findViewById(R.id.start);
stop =(Button) findViewById(R.id.stop);
//得到AnimationDrawable对象
ad =(AnimationDrawable) iv.getBackground();
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == start) {
ad.start();
}else {
ad.stop();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View t) {
if (t == stop){
ad.stop();
}else {
ad.start();
}
}
});
}