原文:http://www.cnblogs.com/xingfuzzhd/archive/2013/12/28/3495941.html
一、了解系统的ProgressBar怎么实现
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:attr/progressBarStyleLarge"/>
系统为我们提供了几种ProgressBar的样式,其中一个样式的源码:
<style name="Widget.ProgressBar.Large">
<item name="android:indeterminateDrawable">@android:drawable/progress_large_white</item>
<item name="android:minWidth">76dip</item>
<item name="android:maxWidth">76dip</item>
<item name="android:minHeight">76dip</item>
<item name="android:maxHeight">76dip</item>
</style>
接着,打开progress_large_white.xml
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/spinner_white_76"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360" />
看到这行android:drawable="@drawable/spinner_white_76"我们就明白了,原来他在这里放了张图片,进行旋转。
即:定义一个动画,然后在样式中引用,最后在ProgressBar中使用该样式。
二、定义自己的ProgressBar
1、定义动画:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/progress_large"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
2、样式中引用
<style name="myProgressBarStyleLarge" >
<item name="android:indeterminateDrawable">@drawable/progress_large</item>
<item name="android:minWidth">76dip</item>
<item name="android:maxWidth">76dip</item>
<item name="android:minHeight">76dip</item>
<item name="android:maxHeight">76dip</item>
</style>
3、ProgressBar中使用该样式
<ProgressBar
style="@style/myProgressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDuration="700" />
indeterminateDuration指定图片旋转的速度。当然也可不指定。
4、通过帧动画来实现
上面是通过一张图片填充android:indeterminateDrawable,我们也可定义一个动画或自定义颜色来实现:
先定义动画:progress_large_loading.xml
<?xml version="1.0" encoding="UTF-8"?>
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="100" android:drawable="@drawable/loading_1" />
<item android:duration="100" android:drawable="@drawable/loading_2" />
<item android:duration="100" android:drawable="@drawable/loading_3" />
<item android:duration="100" android:drawable="@drawable/loading_4" />
<item android:duration="100" android:drawable="@drawable/loading_5" />
<item android:duration="100" android:drawable="@drawable/loading_6" />
</animation-list>
android:oneshot帧动画的自动执行,如果此值为true,表示执行动画只播放一次最后停在最后一帧上;为false表示动画循环播放。
然后在style中引入,最后使用这个style即可。
效果图: