@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画背景圆环
int center = getWidth() / 2;
float radius = center - roundWidth / 2;
paint.setColor(roundColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(roundWidth); // 圆环的宽度
paint.setAntiAlias(true);
canvas.drawCircle(center,center,radius,paint);
// 画进度百分比
paint.setColor(textColor);
paint.setStrokeWidth(0);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
int percent = (int)(progress / (float)max * 100);
String strPercent = percent + "%";
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
if(percent != 0){
canvas.drawText(strPercent, getWidth() / 2 - paint.measureText(strPercent) / 2 ,
getWidth() / 2 +(fm.bottom - fm.top)/2 - fm.bottom, paint);
}
// 画圆弧
RectF oval = new RectF(center - radius, center - radius,
center + radius, center + radius);
paint.setColor(roundProgressColor);
paint.setStrokeWidth(roundWidth);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawArc(oval, 0 , 360 * progress / max, false, paint);
}
public void setProgress(int progress){
if(progress < 0 ){
throw new IllegalArgumentException("进度Progress不能小于0");
}
if(progress > max){
progress = max;
}
if(progress <= max){
this.progress = progress;
postInvalidate();
}
}
圆形进度条实现方案
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1、重写UITextField子类的drawRect方法 - (void)drawRect:(CGRect)rec...