最近在公司,项目不是很忙了,偶尔看见一个兄台在csdn求助,帮忙要一个自定义的渐变色进度条,我当时看了一下进度条,感觉挺漂亮的,就尝试的去自定义view实现了一个,废话不说,先上图吧!
这个自定义的view,完全脱离了android自带的progressview,并且没使用一张图片,这样就能更好的降低程序代码上的耦合性!
下面我贴出代码 ,大概讲解一下实现思路吧!
package com.spring.progressview;
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.lineargradient;
import android.graphics.matrix;
import android.graphics.paint;
import android.graphics.rectf;
import android.graphics.shader;
import android.util.attributeset;
import android.view.view;
/***
* 自定义进度条
* @author spring sky
* email:vipa1888@163.com
* 创建时间:2014-1-6下午3:28:51
*/
public class springprogressview extends view {
/**分段颜色*/
private static final int[] section_colors = {color.green,color.yellow,color.red};
/**进度条最大值*/
private float maxcount;
/**进度条当前值*/
private float currentcount;
/**画笔*/
private paint mpaint;
private int mwidth,mheight;
public springprogressview(context context, attributeset attrs,
int defstyleattr) {
super(context, attrs, defstyleattr);
initview(context);
}
public springprogressview(context context, attributeset attrs) {
super(context, attrs);
initview(context);
}
public springprogressview(context context) {
super(context);
initview(context);
}
private void initview(context context) {
}
@override
protected void ondraw(canvas canvas) {
super.ondraw(canvas);
mpaint = new paint();
mpaint.setantialias(true);
int round = mheight/2;
system.out.println("max="+maxcount + " current="+currentcount);
mpaint.setcolor(color.rgb(71, 76, 80));
rectf rectbg = new rectf(0, 0, mwidth, mheight);
canvas.drawroundrect(rectbg, round, round, mpaint);
mpaint.setcolor(color.black);
rectf rectblackbg = new rectf(2, 2, mwidth-2, mheight-2);
canvas.drawroundrect(rectblackbg, round, round, mpaint);
float section = currentcount/maxcount;
rectf rectprogressbg = new rectf(3, 3, (mwidth-3)*section, mheight-3);
if(section <= 1.0f/3.0f){
if(section != 0.0f){
mpaint.setcolor(section_colors[0]);
}else{
mpaint.setcolor(color.transparent);
}
}else{
int count = (section <= 1.0f/3.0f*2 ) ? 2 : 3;
int[] colors = new int[count];
system.arraycopy(section_colors, 0, colors, 0, count);
float[] positions = new float[count];
if(count == 2){
positions[0] = 0.0f;
positions[1] = 1.0f-positions[0];
}else{
positions[0] = 0.0f;
positions[1] = (maxcount/3)/currentcount;
positions[2] = 1.0f-positions[0]*2;
}
positions[positions.length-1] = 1.0f;
lineargradient shader = new lineargradient(3, 3, (mwidth-3)*section, mheight-3, colors,null, shader.tilemode.mirror);
mpaint.setshader(shader);
}
canvas.drawroundrect(rectprogressbg, round, round, mpaint);
}
private int diptopx(int dip) {
float scale = getcontext().getresources().getdisplaymetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
/***
* 设置最大的进度值
* @param maxcount
*/
public void setmaxcount(float maxcount) {
this.maxcount = maxcount;
}
/***
* 设置当前的进度值
* @param currentcount
*/
public void setcurrentcount(float currentcount) {
this.currentcount = currentcount > maxcount ? maxcount : currentcount;
invalidate();
}
public float getmaxcount() {
return maxcount;
}
public float getcurrentcount() {
return currentcount;
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
int widthspecmode = measurespec.getmode(widthmeasurespec);
int widthspecsize = measurespec.getsize(widthmeasurespec);
int heightspecmode = measurespec.getmode(heightmeasurespec);
int heightspecsize = measurespec.getsize(heightmeasurespec);
if (widthspecmode == measurespec.exactly || widthspecmode == measurespec.at_most) {
mwidth = widthspecsize;
} else {
mwidth = 0;
}
if (heightspecmode == measurespec.at_most || heightspecmode == measurespec.unspecified) {
mheight = diptopx(15);
} else {
mheight = heightspecsize;
}
setmeasureddimension(mwidth, mheight);
}
}
以上代码就是该控件的全部核心代码了
具体思路:
1.进度条,其实就是一个最大值和最小值的比例值,这个比例就是 当前值/最大值;
2.自定义的圆角问题,只要还是用到了canvar的画板的drawroundrect ;
3.渐变色:lineargradient对象渲染,具体渲染的比例要自己计算,目前我的程序提供3中颜色渲染,具体规则是:
(1)当进度条占最大值的三分之一以下,则提供一种颜色
(2)当最大值超过三分之一话,就区分是否超过三分之二,如果超过则用三种,否则用两种颜色,因为三种颜色各占总进度条的三分之一,这是一个初中数据的问题,自己慢慢画图吧!
4.怎么把进度条放在一个有圆角背景的上面,这个就是绘制两个圆角长方形:第一个作为背景,第二个作为进度条的实体,具体第二个进度的实体占多长,就是当前 currentcount/maxcount*自定义view的长度 ;
其他的,没啥技术难点了,做这种自定义控件,最重要的是,自定要根据人家的效果图,看懂实现思路,具体代码简历在思路上的,否则只会纸上谈兵!如果看不懂,就要多画图,具体的一步步计算,天长地久,也就能“练”出来了!
下面提供一个demo下载地址:springprogressdemo.zip