控件 -- ProgressBar

一、概念

ProgressBar是Android系统提供的进度条控件。

二、使用

1.进度

ProgressBar有两个进度:一个是Android:progress,另一个是android:secondaryProgress。后者主要是为缓存场景使用,比如在看网络视频时候都会有一个缓存的进度条以及一个播放的进度,在这里缓存的进度就可以是android:secondaryProgress,而播放进度就是android:progress。

2.样式

ProgressBar有如下样式:
Widget.ProgressBar.Horizontal
Widget.ProgressBar.Small
Widget.ProgressBar.Large
Widget.ProgressBar.Inverse
Widget.ProgressBar.Small.Inverse
Widget.ProgressBar.Large.Inverse
使用方法:style="@android:style/Widget.ProgressBar.Small"

另外还有一种方式就是使用系统的attr,下面的方式是系统的style:
style="?android:attr/progressBarStyle"
style="?android:attr/progressBarStyleHorizontal"
style="?android:attr/progressBarStyleInverse"
style="?android:attr/progressBarStyleLarge"
style="?android:attr/progressBarStyleLargeInverse"
style="?android:attr/progressBarStyleSmall"
style="?android:attr/progressBarStyleSmallInverse"
style="?android:attr/progressBarStyleSmallTitle"

使用例子:

<ProgressBar  
     android:id="@+id/progressBar1"  
     style="?android:attr/progressBarStyleHorizontal"  
     style="@android:style/Widget.ProgressBar.Horizontal"(等同于@android:attr)  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content" />

3.自定义样式

Widget.ProgressBar.Horizontal源码如下:

<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>  
    <item name="android:mirrorForRtl">true</item>  
</style>

//progress_horizontal
<?xml version="1.0" encoding="utf-8"?>  
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
     
    <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>  
     
    <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>  
     
    <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>

通过一个layer-list定义了一个LayerDrawable来处理进度的绘制,其中绘制包括3部分:background、secondProgress、progress,知道了绘制原理,那我们就可以很轻松的进行定义自己的进度条样式了,如下:

//progress_bar_style.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/red" />
            </shape>
        </clip>
    </item>
</layer-list>

//layout xml
<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="8dp"
    android:progressDrawable="@drawable/progress_bar_style" />

以上自定义的是有进度的进度条样式,至于无进度的进度条样式可以通过android:indeterminateDrawable属性设置,方法同android:progressDrawable。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容