美文网首页
android ProgressBar 详解 以及 自定义案例

android ProgressBar 详解 以及 自定义案例

作者: ZSGZ_AD | 来源:发表于2019-09-27 14:05 被阅读0次

基础介绍:

进度条也是UI界面中一种非常实用的组件,通常用于向用户显示某个耗时操作完成的百分比。
进度条可以动态地显示进度,因此避免长时间地执行某个耗时操作时,让用户感觉程序失去了响应,从而更好地提高用户界面的友好性。


1568799805638.gif

系统提供的标准的ProgressBar有三种:

精确模式


image.png

模糊模式(圆形)


image.png
模糊模式(横向)
image.png

Android支持多种风格的进度条,通过style属性可以为ProgressBar指定风格。该属性可支持如下几个属性值:

@android:style/Widget.ProgressBar.Horizontal:水平进度条。

@android:style/Widget.ProgressBar.Inverse:普通大小的环形进度条。

@android:style/Widget.ProgressBar.Large:大环形进度条。

@android:style/Widget.ProgressBar.Large.Inverse:大环形进度条。

@android:style/Widget.ProgressBar.Small:小环形进度条。

@android:style/Widget.ProgressBar.Small.Inverse:小环形进度条。

其实在Android开发中,ProgressBar的样式设定有两种方式,除了上面这种,还有一种可以通过如下方式使用:

?android:attr/progressBarStyle

?android:attr/progressBarStyleHorizontal

?android:attr/progressBarStyleInverse

?android:attr/progressBarStyleLarge

?android:attr/progressBarStyleLargeInverse

?android:attr/progressBarStyleSmall

?android:attr/progressBarStyleSmallInverse

?android:attr/progressBarStyleSmallTitle

除此之外,ProgressBar还支持如下常用XML属性:

android:max:进度条的最大值。

android:progress:进度条已完成进度值。

android:progressDrawable:设置轨道对应的Drawable对象。

android:indeterminate:该属性一般在进度条为横向情况(Widget.ProgressBar.Horizontal)下才设置,因为圆形进度条本身就是模糊模式。true - 启用模糊模式
       false - 禁用模糊模式
android:indeterminateDrawable:设置模糊状态下ProgressBar的Drawable资源,该Drawable资源是一个带动画的资源文件,例如旋转动画<rotate>。在ProgressBar的自定义章节中,我们会对该属性进行更详细的说明。

android:indeterminateDuration:设置模糊状态下ProgressBar一个周期动画的持续时间,该属性值必须是整型。在【ProgressBar的自定义】中,我们会对该属性进行更详细的说明。

android:secondaryProgress:二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度。

其中android:progressDrawable用于指定进度条的轨道的绘制形式,该属性可指 定为一个LayerDrawable对象的引用。

ProgressBar提供了如下方法来操作进度:

getMax():返回这个进度条的范围的上限。

getProgress():返回进度。

getSecondaryProgress():返回次要进度。

incrementProgressBy(int diff):指定增加的进度。为正数时进度增加;为负数时进度减少。

isIndeterminate():指示进度条是否在不确定模式下。

setIndeterminate(boolean indeterminate):设置是否为不确定模式。

自定义ProgressBar

1568799805638.gif

上代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:indeterminate="true"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:indeterminateDrawable="@drawable/custom_progressbar"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <ProgressBar
        android:id="@+id/pb_download_status"
        style="@android:style/Widget.ProgressBar.Large"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_margin="16dp" />

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="16dp"
        android:indeterminateDrawable="@drawable/custom_progressbar"
      />
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="16dp"
        android:indeterminateDrawable="@drawable/loading_progressbar"
        />
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="16dp"
        android:indeterminate="true"
        android:indeterminateDrawable="@drawable/run_loading" />
</LinearLayout>

custom_progressbar

<?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="4"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false" >

        <gradient
            android:centerY="0.50"
            android:startColor="#193DACB7"
            android:centerColor="#FF77C5CC"
            android:endColor="#FF3DACB7"
            android:type="sweep"
            android:useLevel="false" />
    </shape>

</rotate>

loading_progressbar

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/loading"
    android:fromDegrees="0.0"
    android:pivotX="50.0%"
    android:pivotY="50.0%"
    android:toDegrees="360.0" />

run_loading

<?xml version="1.0" encoding="UTF-8"?>
<animation-list android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:duration="160" android:drawable="@mipmap/icon_run1" />
    <item android:duration="160" android:drawable="@mipmap/icon_run2" />
    <item android:duration="160" android:drawable="@mipmap/icon_run3" />
    <item android:duration="160" android:drawable="@mipmap/icon_run4" />
    <item android:duration="160" android:drawable="@mipmap/icon_run5" />
    <item android:duration="160" android:drawable="@mipmap/icon_run6" />
</animation-list>

loading.png图片


image.png

icon_run.png图片


icon_run1.png icon_run2.png icon_run3.png icon_run4.png icon_run5.png icon_run6.png

源码地址:https://github.com/sgl890226/objectanimator

自定义圆形的progressbar有很多,也可以参考:[https://www.jianshu.com/p/47b1b4733e86]

相关文章

网友评论

      本文标题:android ProgressBar 详解 以及 自定义案例

      本文链接:https://www.haomeiwen.com/subject/voahuctx.html