Android 加载动画效果
使用animation-list 实现
动画文件 res/anim/loading.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/loading_1" android:duration="200" />
<item android:drawable="@drawable/loading_2" android:duration="200" />
<item android:drawable="@drawable/loading_3" android:duration="200" />
</animation-list>
使用布局
<ImageView
<span style="white-space:pre"> </span>android:id="@+id/img_loading"
<span style="white-space:pre"> </span>android:layout_width="fill_parent"
<span style="white-space:pre"> </span>android:layout_height="wrap_content"
<span style="white-space:pre"> </span>android:layout_gravity="center"
<span style="white-space:pre"> </span>android:src="@drawable/loading"
<span style="white-space:pre"> </span>android:adjustViewBounds="true"/>
启动动画,这里不能写在activity 的onCreate()方法中, 需要写在onClick监听或者内部方法中。
ImageView rocketImage = (ImageView) findViewById(R.id.img_loading);
AnimationDrawable rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();
亦可写成自定义的类
public class Loader extends ImageView {
public Loader(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public Loader(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Loader(Context context) {
super(context);
init();
}
private void init() {
setBackgroundResource(R.drawable.loading);
final AnimationDrawable frameAnimation = (AnimationDrawable) getBackground();
post(new Runnable(){
public void run(){
frameAnimation.start();
}
});
}
}
使用时
<com.yourpackage.Loader
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
参看:
http://stackoverflow.com/questions/8721458/using-android-animation-list
http://developer.android.com/guide/topics/resources/animation-resource.html