環形進度條CircleProgress
阿新 • • 發佈:2019-01-07
自定義環形進度條,兩種顏色,背景顏色和進度顏色,記錄進度值
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
* @author Joker_Ya
*/
public class MyCircleProgress extends View {
private int progress;
// 設定最大值
private int max;
// 圓環寬度
private int circleWidth;
// 控制元件的寬度
private int width;
// 控制元件的高度
private int height;
// 預設圓的半徑
private int radius;
// 繪製軌跡的畫筆
private Paint paint;
// 繪製填充的畫筆
private Paint fillpaint;
private RectF oval;
// View重繪標誌
private boolean reset = false;
// 設定監聽
private OnProgressChangeListener listener = null;
public MyCircleProgress(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
// 獲得自定義屬性
TypedArray mArray = context.obtainStyledAttributes(attrs,
R.styleable.myattrs);
// 獲得自定義屬性的初始進度屬性,否則返回0
progress = mArray.getInteger(R.styleable.myattrs_progress, 0);
// 獲得自定義屬性的最大進度值屬性,否則返回100
max = mArray.getInteger(R.styleable.myattrs_max, 100);
// 獲得自定義屬性的圓環寬度屬性,否則返回20
circleWidth = mArray.getInteger(R.styleable.myattrs_circle_width, 20);
// 獲得自定義屬性的半徑屬性,否則返回120
radius = mArray.getInteger(R.styleable.myattrs_radius, 120);
paint = new Paint();
// 抗鋸齒
paint.setAntiAlias(true);
// 幫助抗鋸齒
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
// 設定樣式為空心
paint.setStyle(Paint.Style.STROKE);
paint.setDither(true);
// paint.setStrokeJoin(Paint.Join.ROUND);
fillpaint = new Paint();
// 抗鋸齒
fillpaint.setAntiAlias(true);
// 幫助抗鋸齒
fillpaint.setFlags(Paint.ANTI_ALIAS_FLAG);
// 設定樣式為空心
fillpaint.setStyle(Paint.Style.STROKE);
fillpaint.setDither(true);
// fillpaint.setStrokeJoin(Paint.Join.ROUND);
oval = new RectF();
// 回收mArray
mArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (reset) {
canvas.drawColor(Color.TRANSPARENT);
reset = false;
}
// 獲得元件的寬高
width = getMeasuredWidth();
height = getMeasuredHeight();
// 獲得半徑
radius = width / 2 - circleWidth;
// 設定畫筆顏色64646d 背景顏色
paint.setColor(Color.argb(0xff, 0x64, 0x64, 0x6d));
// 設定畫筆寬度
paint.setStrokeWidth(circleWidth);
// 中心畫圓
canvas.drawCircle(width / 2, height / 2, radius, paint);
// /* 繪製邊線 */
// // 畫筆寬度
// paint.setStrokeWidth(1f);
// // 邊線顏色
// paint.setColor(Color.RED);
// canvas.drawCircle(width / 2, height / 2, radius + circleWidth / 2
// + 0.5f, paint);
// canvas.drawCircle(width / 2, height / 2, radius - circleWidth / 2
// - 0.5f, paint);
//畫筆填充顏色
fillpaint.setColor(Color.argb(0xff, 0xee, 0x6b, 0x9e));
// 設定填充畫筆的型別,邊角是圓角的
fillpaint.setStrokeCap(Paint.Cap.ROUND);
// 畫筆寬度
fillpaint.setStrokeWidth(circleWidth);
// 設定左上角和右下角座標
oval.set(width / 2 - radius, height / 2 - radius, width / 2 + radius,
height / 2 + radius);
// 繪製圓弧。第二個引數為起始角度,第三個為跨越的角度,第三個為實心,true為實心,false為空心
canvas.drawArc(oval, -90, ((float) progress / max) * 360, false,
fillpaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int width = View.MeasureSpec.getSize(widthMeasureSpec);
int height = View.MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
// 定義介面ProgressChangeListener
public interface OnProgressChangeListener {
public void ProgressChange(int progress1);
public void onComplete(int progress2);
}
public void setOnProgressChangeListener(OnProgressChangeListener listener) {
// TODO Auto-generated method stub
this.listener = listener;
}
// 設定進度
public void setProgress(int progress) {
this.progress = progress;
this.invalidate();
if (listener != null) {
if (this.max <= this.progress) {
listener.onComplete(progress);
} else {
listener.ProgressChange(progress);
}
}
}
// 重置進度
public void reset() {
reset = true;
progress = 0;
// 重繪
invalidate();
}
}