<?xml version="1.0" encoding="utf-8"?>
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<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>、
布局文件中:
<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"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/list"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="31dp"
android:layout_marginTop="18dp"
android:text="停止" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginRight="63dp"
android:layout_toLeftOf="@+id/button1"
android:text="开始" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentLeft="true"
android:text="吟唱" />
</RelativeLayout>
package com.example.domeimage;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.AvoidXfermode;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView animationIV;
private AnimationDrawable animationDrawable;
private Button button,button2,button3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animationIV=(ImageView) findViewById(R.id.image);
button=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button3=(Button) findViewById(R.id.button3);
// animationIV.setImageResource(R.drawable.list);
animationDrawable = (AnimationDrawable) animationIV.getDrawable();
animationDrawable.start();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
animationDrawable.stop();
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
animationDrawable.start();
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
animationIV.setVisibility(View.GONE);
}
});
}
}