开发过程中需要自定义开发一个加载进度条显示,这里记录下来方便以后查找。
首先:定义一个anim文件夹下的animation.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/progress_1" android:duration="200"/>
<item android:drawable="@drawable/progress_2" android:duration="200"/>
<item android:drawable="@drawable/progress_3" android:duration="200"/>
<item android:drawable="@drawable/progress_4" android:duration="200"/>
<item android:drawable="@drawable/progress_5" android:duration="200"/>
<item android:drawable="@drawable/progress_6" android:duration="200"/>
<item android:drawable="@drawable/progress_7" android:duration="200"/>
<item android:drawable="@drawable/progress_8" android:duration="200"/>
</animation-list>
其中,animation-list是加载过程中的动画列表。item标签是其中的图片资源文件,其中android:duration="200"表示动画持续的时间,时间以毫秒为单位。
定义好之后,便可以在Activity中进行调用。
ImageView imageView = (ImageView) findViewById(R.id.loadingImageView);
imageView.setBackgroundResource(R.drawable.progress_circular);
AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}