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"指定了其样式

以上的效果图如下:

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。

2016-04-19_152658.png

Holo系列:


2016-04-19_152901.png

Material系列:(需要API 21)


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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,467评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,568评论 2 45
  • 日出日落,形色匆匆,每个人忙碌着,生存者,挣扎着。有的人表面一套,背后一套,嘴上一套,实际又一套,这就是我们所谓的...
    独孤意阅读 250评论 0 0
  • 比起令人伤感的夕阳更想看让人充满希望的朝阳 所以请再给我一点追逐梦想的信心为了迎接明天 若能对我微笑若能对我微笑 ...
    陈煜伟阅读 658评论 0 1
  • 深呼吸,我在纠结中,却不知道自己想要什么样的答案,是自己想离开还是被离开。不懂得改变让我处于现在的境地还是不努力导...
    洛茴阅读 270评论 2 2