自定义的圆形进度条

来源:互联网 发布:http 请求数据方式 编辑:程序博客网 时间:2024/04/28 06:43

最近做一个检测系统,项目中后台上传的东西太多,要把状态展示给用户看,觉得原生的进度条太坑了,于是自己研究写了个,菜鸟~  轻喷  = =。


其实就是一个自定义view  先上个图   截屏太麻烦,就没做动态图了,有兴趣的可以去下了看看  


https://github.com/Piscen/CustomProgresbar




介绍下:正在上传,显示的是中间一个箭头,外面圆环进度在动,到达100%后就成了一个勾,上传失败就感叹号。状态可以动态设置,个人觉得效果挺好

                中间显示也可以是数字,可以看到上传具体进度,这里就不贴了

ondraw代码:

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 画最外层的大圆环
*/
centreX= getWidth()/2; //获取圆心的x坐标
centreY= getHeight()/2;
// int centre = (int) 24.5;
int radius = (int) (centreX - roundWidth/2); //圆环的半�?
paint.setColor(roundColor); //设置圆环的颜�?
paint.setStyle(Paint.Style.STROKE); //设置空心
paint.setStrokeWidth(roundWidth); //设置圆环的宽�?
paint.setAntiAlias(true);  //消除锯齿 
canvas.drawCircle(centreX, centreX, radius, paint); //画出圆环


/*
* 中间白色�?
*/
paint.setColor(Color.WHITE); //设置中间圆的颜色
paint.setStyle(Paint.Style.FILL); //设置实心
paint.setAntiAlias(true);  //消除锯齿 
int radius1 = (int) (radius- roundWidth/2);
canvas.drawCircle(centreX, centreX, radius1, paint); //画出中间白色�?


// Log.e("log", centre + "");
/**
* 画进度百分比
*/
paint.setStrokeWidth(0); 
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体


// int percent = (int)(((float)progress / (float)max) * 100);  //中间的进度百分比,先转换成float在进行除法运算,不然都为0
// float textWidth = paint.measureText(percent + "%");   //测量字体宽度,我们需要根据字体的宽度设置在圆环中�?


if(textIsDisplayable  && style == STROKE){
// canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //画出进度百分�?
drawImage(state ,canvas, centreX - textSize/2, centreX - textSize/2,paint);


}
/**
* 画圆�?,画圆环的进�?
*/


//设置进度是实心还是空�?
paint.setStrokeWidth(roundWidth); //设置圆环的宽�?
paint.setColor(roundProgressColor);  //设置进度的颜�?
RectF oval = new RectF(centreX - radius, centreX - radius, centreX
+ radius, centreX + radius);  //用于定义的圆弧的形状和大小的界限


switch (style) {
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval, -90, 360 * progress / max, false, paint);  //根据进度画圆�?
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
if(progress !=0)
canvas.drawArc(oval, -90, 360 * progress / max, true, paint);  //根据进度画圆�?
break;
}
}
}

设置几种状态,选择画哪个,默认是不画图

/**
* {tags}
* @TODO 画中心图�?
*/
private void drawImage(int state,Canvas canvas,float f,float g,Paint paint){
Bitmap bitmap = null;
switch (state) {
case 01:


bitmap = BitmapFactory.decodeResource(getResources(), images[0]);
setProgress(100);
break;
case 02:
// bitmap = small(BitmapFactory.decodeResource(getResources(), images[1]));
bitmap = BitmapFactory.decodeResource(getResources(), images[1]);

setCricleColor(Color.RED);
setCricleProgressColor(Color.RED);
break;
case 03:
// bitmap = small(BitmapFactory.decodeResource(getResources(), images[2]));
bitmap = BitmapFactory.decodeResource(getResources(), images[2]);

break;
default:
break;
}
int  x = centreX - bitmap.getWidth()/2;
int  y = centreY - bitmap.getHeight()/2;
canvas.drawBitmap(bitmap, x,y, paint);
}


设置一些熟悉和进度,同时刷新UI

public synchronized int getState() {
return state;
}


public synchronized void setState(int state) {
if(max < 0){
throw new IllegalArgumentException("max not less than 0");
}
this.state = state;
postInvalidate();


}


public  void initColors(){
if(progress<100){
paint.setColor(roundProgressColor);
}
else{
paint.setColor(Color.RED);
}
}


public synchronized int getMax() {
return max;
}


/**
* 设置进度的最大�?
* @param max
*/
public synchronized void setMax(int max) {
if(max < 0){
throw new IllegalArgumentException("max not less than 0");
}
this.max = max;
}


/**
* 获取进度.�?��同步
* @return
*/
public synchronized int getProgress() {
return progress;
}


/**
* 设置进度,此为线程安全控件,由于考虑多线的问题,�?��同步
* 刷新界面调用postInvalidate()能在非UI线程刷新
* @param progress
*/
public synchronized void setProgress(int progress) {
if(progress < 0){
throw new IllegalArgumentException("progress not less than 0");
}
if(progress > max){
progress = max;
}
if(progress <= max){
this.progress = progress;


if(progress == 100){
state = SUCCES;
}
if(progress <100&progress>0){
state = CONTINUE;
}
if(progress == 0){
state = FAILD;
}
postInvalidate();
}
}




public int getCricleColor() {
return roundColor;
}


public void setCricleColor(int cricleColor) {
this.roundColor = cricleColor;
postInvalidate();
}


public int getCricleProgressColor() {
return roundProgressColor;
}


public void setCricleProgressColor(int cricleProgressColor) {
this.roundProgressColor = cricleProgressColor;
postInvalidate();
}


public int getTextColor() {
return textColor;
}


public void setTextColor(int textColor) {
this.textColor = textColor;
}


public float getTextSize() {
return textSize;
}


public void setTextSize(float textSize) {
this.textSize = textSize;
}


public float getRoundWidth() {
return roundWidth;
}


public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}


其实也是参考了大牛的Demo,然后自己改了改,demo地址 https://github.com/Piscen/CustomProgresbar


0 0
原创粉丝点击