android ProgressBar

概述

progressBar,进度条,可以设置自定义的样式,其派生出很多控件,如SeekBar,RatingBar,还有ProgressDialog。

1. 基本用法

最常用的用法,就是直接在XML布局文件中声明,制定style就可以了。其style有很多种,有横向长方形的,也有圆圈的。

<ProgressBar
        android:max="100"
        android:progress="20"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

其中,我们比较关注的几个属性是:

android:max制定进度条的最大值

android:progress进度条的初始值,默认为0

style="@android:style/Widget.ProgressBar.Horizontal"指定了其样式

以上的效果图如下:

《android ProgressBar》 2016-04-19_151929.png

当然,android中自带的样式有很多:(这里的是API 19)

style="@android:style/Widget.ProgressBar"灰色圆形

style="@android:style/Widget.ProgressBar.Inverse"灰色圆形,旋转方向根上面的相反

style="@android:style/Widget.ProgressBar.Large"大号的灰色圆形

style="@android:style/Widget.ProgressBar.Large.Inverse"大号灰色圆形,带有Inverse,所以旋转方向相反

style="@android:style/Widget.ProgressBar.Small"小号灰色圆形

style="@android:style/Widget.ProgressBar.Small.Inverse"小号灰色圆形,带有Inverse,所以旋转方向相反

style="@android:style/Widget.ProgressBar.Horizontal"黄色进度,横向进度条

其实,还有很多不同系列的。。。hole系列的 等等,我们只需要知道几个关键的地方即可:Large,small,Inverse,Horizontal。

《android ProgressBar》 2016-04-19_152658.png

Holo系列:

《android ProgressBar》 2016-04-19_152901.png

Material系列:(需要API 21)

《android ProgressBar》 2016-04-19_153126.png

DeviceDefault系列:
跟当前的API等级是一样的样式。

2. 自定义风格的进度条

首先,在src/main/res/values/styles.xml中自定义自己的样式:

<style name="mprogressbar">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:progressDrawable">@drawable/progressbar_diy</item>
    <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
    <item name="android:minHeight">20dip</item>
    <item name="android:maxHeight">20dip</item>
</style>

这里,我定义的是mprogressbar。属性说明:从上到下:

  • indeterminateOnly 表示进度条的进度值是否确定。
  • progressDrawable 我们自定义的文件
  • indeterminateDrawable 进度条的进度值的风格,定义一写时间之类 的。
  • minHeight 最小高
  • maxHeight 最大高

我们主要是重写progressDrawable 指定的文件,这里的如下:

<?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="1dip" />
            <solid android:color="@android:color/darker_gray"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="1dip" />
                <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="1dip" />
               <solid android:color="@android:color/holo_orange_dark"/>
            </shape>
        </clip>
    </item>
</layer-list>

通过一个 layer-list 根标签套住,三个item分别是:

  • background:背景, 主要是 shape 形状的设置
  • secondaryProgress:第二进度条样式
  • progress:主进度条样式

3. 竖立的进度条

设计思路,在Draw的时候把其旋转即可。。。

public class VerticalProgressBar extends ProgressBar {

    public VerticalProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalProgressBar(Context context) {
        super(context);
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }
    
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.rotate(-90);
        canvas.translate(-getHeight(), 0);
        super.onDraw(canvas);
    }
}
    原文作者:栗子酱油饼
    原文地址: https://www.jianshu.com/p/63af8ea97aae
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞