自定义ProgressBar的样式
1、默认的ProgressBar的样式\sdk\platforms\android-16\data\res\values\styles.xml <style name="Widget.ProgressBar">
<item name="android:indeterminateOnly">true</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_medium_white</item>
<item name="android:indeterminateBehavior">repeat</item>
<item name="android:indeterminateDuration">3500</item>
<item name="android:minWidth">48dip</item>
<item name="android:maxWidth">48dip</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style> progress_medium_white.xml <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/spinner_white_48"
android:pivotX="50%"
android:pivotY="50%"
android:framesCount="12"
android:frameDuration="100" />
spinner_white_48.png |
2、水平的Horizontal的ProgressBar <style name="Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
</style> progress_horizontal.xml <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
//1、ProgressBar背景
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
//2、ProgressBar二级背景(类似于网络播放缓冲好时的背景)
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
//3、ProgressBar过程中的背景
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
|
3、自定义ProgressBar样式 a)/Mobilesafe v8.0/res/values/styles.xml增加下面的节点 <!-- 自定义的ProgressBar样式 -->
<style name="myHorizontal.ProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/progress_horizontal</item>
</style> b)/Mobilesafe v8.0/res/drawable/progress_horizontal.xml <?xml version="1.0" encoding="utf-8"?>
<!-- 11、自定义的ProgressBar样式 -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@android:id/background"
android:drawable="@drawable/security_progress_bg">
</item>
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/security_progress">
</item>
<item //一定要写在item的属性位置,否则显示不对
android:id="@android:id/progress"
android:drawable="@drawable/security_progress">
</item>
</layer-list> c)背景图片 |
测试代码: new Thread() {
public void run() {
pb_scan_progress.setMax(100);
for (int i = 1; i <= 100; i++) {
pb_scan_progress.setProgress(i);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();结果: |