准备8张图片名字分别为:loading1、loading2、loading3、loading4、
Loading5、loading6、loading7、loading8。
在main.xml中:
<LinearLayout
android:id="@+id/group"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/loading"
android:layout_marginTop="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/start"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="开始动画"/>
</LinearLayout>
在res/anim下新建allloading.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/loading1" android:duration="200" />
<item android:drawable="@drawable/loading2" android:duration="200" />
<item android:drawable="@drawable/loading3" android:duration="200" />
<item android:drawable="@drawable/loading4" android:duration="200" />
<item android:drawable="@drawable/loading5" android:duration="200" />
<item android:drawable="@drawable/loading6" android:duration="200" />
<item android:drawable="@drawable/loading7" android:duration="200" />
<item android:drawable="@drawable/loading8" android:duration="200" />
</animation-list>
在MyAnimationDemo.java中:
package com.li.animation;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
@SuppressLint({ "ParserError", "ParserError" })
public class MyAnimationDemo extends Activity {
private ImageView loading = null;
private Button start = null;
private AnimationDrawable draw = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.loading = (ImageView)super.findViewById(R.id.loading);
this.start = (Button)super.findViewById(R.id.start);
this.start.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MyAnimationDemo.this.loading.setBackgroundResource(R.anim.allloading);
MyAnimationDemo.this.draw = (AnimationDrawable)MyAnimationDemo
.this.loading.getBackground();
MyAnimationDemo.this.draw.setOneShot(false); //一直动
MyAnimationDemo.this.draw.start();
}
}
}