1.自定义圆形的ProgressBarandroid
效果图:动画
圆形ProgressBar的样式主要有如下几个,咱们这里以progressBarStyleLarge为例进行样式的修改,其余的相似。spa
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"/>
首先看一下style="?android:attr/progressBarStyleLarge"的源码,在\frameworks\base\core\res\res\values\styles.xmlcode
<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>
看到这一行<item name="android:indeterminateDrawable">@android:drawable/progress_large_white</item>有木有,咱们去看一下它的源码,在\frameworks\base\core\res\res\drawable\progress_large_white.xmlxml
<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" 咱们就明白了,原来它在这里放了一张图片,进行旋转。blog
接下来我定义本身的ProgressBarStyle:图片
首先咱们先找一张图片加入咱们的项目中(如一开始的效果图片),而后在drawable下新建progress_large.xml文件ip
<?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" />
在 \value\style.xml中定义myProgressBarStyleLargeutf-8
<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>
最后在ProgressBar中使用咱们本身定义的style,android:indeterminateDuration="700"指定图片旋转的速度,这样咱们就能够根据本身的须要来定义ProgressBar的样式。get
<ProgressBar style="@style/myProgressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDuration="700" />
2.上面是经过一张图片填充android:indeterminateDrawable,咱们也能够定义一个动画或者自定义颜色来实现,跟图片的用法同样:
定义res/anim/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>
在咱们定义的style中引入<item name="android:indeterminateDrawable">@anim/progress_large_loading</item>
定义res/drawable/progress_large_shape.xml以下:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" > <shape android:innerRadiusRatio="3" android:shape="ring" android:thicknessRatio="8" android:useLevel="false" > <gradient android:centerColor="#FFFFFF" android:centerY="0.50" android:endColor="#1E90FF" android:startColor="#000000" android:type="sweep" android:useLevel="false" /> </shape> </rotate>
在咱们定义的style中引入<item name="android:indeterminateDrawable">@drawable/progress_large_shape</item>