Author: YugeCse
Date: 2014.06.12
package com.kaiju.kaiju.usercontrols;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class CircleProgressBar extends View {
private Context context;
private Paint paint;
private float radius = 40f;
private float ringRadius = 10f;
private int progress = 0;
private int max = 100;
private int circleColor = Color.GRAY;
private int progressColor = Color.parseColor("#feb239");
public CircleProgressBar(Context context){
super(context);
init();
}
public CircleProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init(){
paint = new Paint();
paint.setAntiAlias(true);
}
//设置默认圆环颜色
public void setCircleColor(int color){
this.circleColor = color;
this.invalidate();
}
public int getCircleColor(){
return this.circleColor;
}
//设置进度条颜色
public void setProgressColor(int color){
this.progressColor = color;
this.invalidate();
}
public int getProgressColor(){
return this.progressColor;
}
//设置圆环半径
public void setRingRadius(float radius){
this.ringRadius = radius;
this.invalidate();
}
public float getRingRadius(){
return this.ringRadius;
}
public void setRadius(float radius){
this.radius = radius;
this.invalidate();
}
public float getRadius(){
return this.radius;
}
//设置最大值
public void setMax(int value){
this.max = value;
}
public int getMax(){
return this.max;
}
//设置当前进度值
public void setProgress(int progress){
this.progress = progress;
this.invalidate();
}
public int getProgress(){
return this.progress;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
//获取控件测量的宽度高度
int width = getMeasuredWidth();
int height = getMeasuredHeight();
super.onDraw(canvas);
//设置画笔参数
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10f);
paint.setColor(circleColor);
//绘制最大圆
canvas.drawArc(new RectF((width-radius*2)/2+2,(height-radius*2)/2,radius*2,radius*2), 270, 360, false, paint);
paint.setColor(progressColor);
//绘制内圆
canvas.drawArc(new RectF((width-radius*2)/2+2,(height-radius*2)/2,radius*2,radius*2), 270, (float)progress/max*360, false, paint);
paint.setColor(Color.BLUE);
String value = (int)(((float)progress/max)*100)+"%";
float widthText = 0f;
float[] widths = new float[value.length()];
for(int i=0;i<widths.length;i++) widthText+=widths[i];
//绘制进度显示的文本
paint.reset();
paint.setAntiAlias(true);
paint.setTextSize(25f);
paint.getTextWidths(value, widths);
FontMetrics fm = paint.getFontMetrics();
paint.setTextAlign(Align.CENTER);
canvas.drawText(value,(width-widthText)/2, (height-(Math.abs(fm.descent)-Math.abs(fm.ascent)))/2, paint);
paint.reset();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int genericValue = +MeasureSpec.makeMeasureSpec((int)(radius*2+ringRadius), MeasureSpec.EXACTLY);
int width = getPaddingLeft()+getPaddingRight()+ genericValue;
int height = getPaddingTop()+getPaddingBottom()+ genericValue;
super.onMeasure(width, height);
super.setMeasuredDimension(width, height);
}
}
掌握这方面的自定义控件,主要的就是掌握Canvas的绘图函数,已经如何通过使用变量来控制绘图。这种进度条的绘制应该也算是最基本的了吧,哈哈。