progressBar,进度条,能够设置自定义的样式,其派生出不少控件,如SeekBar,RatingBar,还有ProgressDialog。php
最经常使用的用法,就是直接在XML布局文件中声明,制定style就能够了。其style有不少种,有横向长方形的,也有圆圈的。java
<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
android:max
制定进度条的最大值canvas
android:progress
进度条的初始值,默认为0ide
style="@android:style/Widget.ProgressBar.Horizontal"
指定了其样式布局
以上的效果图以下:spa
固然,android中自带的样式有不少:(这里的是API 19)设计
style="@android:style/Widget.ProgressBar"
灰色圆形3d
style="@android:style/Widget.ProgressBar.Inverse"
灰色圆形,旋转方向根上面的相反code
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。
Holo系列:
Material系列:(须要API 21)
DeviceDefault系列: 跟当前的API等级是同样的样式。
首先,在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。属性说明:从上到下:
咱们主要是重写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分别是:
设计思路,在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);
}
}
复制代码