riku
2025-10-30 e9aa93f381afcf9f9cf0c39f2b9e32375ed49528
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cn.flightfeather.thirdappmodule.view;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
 
/**
 * Created by 王xin on 2017/4/5.
 */
 
public class CircleProgressView extends View {
    private static final String TAG = "CircleProgressView";
    private int mMaxProgress = 100;
    private final int mCircleLineStrokeWidth = 5;
    private final int mTxtStrokeWidth = 2;
    private int mCurrentPercent = 0;
    private float mCurrentAngle = 0;
    // 画圆所在的距形区域
    private final RectF mRectF;
    private final Paint mPaint;
    private final Paint mPaint2;
    private int stepCount = 0;
    private int stepGoal = 0;
    private int mProgress = 90;
    private int r, g, b;
    private int totalTask;
    private int finishTask;
 
    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRectF = new RectF();
        mPaint = new Paint();
        mPaint2 = new Paint();
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();
        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }
        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        //79,202,245
        mPaint.setColor(Color.rgb(239, 239, 239));
        canvas.drawColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(mCircleLineStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        // 位置
        mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
        mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
        mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
        mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y
        // 绘制圆圈,进度条背景
        canvas.drawArc(mRectF, -90, 360, false, mPaint);
        mPaint.setColor(Color.rgb(r, g, b));
        canvas.drawArc(mRectF, -90, mCurrentAngle, false, mPaint);
        if (mCurrentPercent < mProgress) {
            //当前百分比+1
            mCurrentPercent += 1;
            //当前角度+3.6
            mCurrentAngle += 3.6;
            //每10ms重画一次
            postInvalidateDelayed(5);
        } else if (mCurrentPercent == mProgress) {
 
        }
        // 绘制进度文案显示
        mPaint.setStrokeWidth(mTxtStrokeWidth);
        int stepNumberHeight = height / 4;
        mPaint.setTextSize(stepNumberHeight);
        mPaint.setStyle(Paint.Style.FILL);
        //字符位置
        mPaint2.setStrokeWidth(mTxtStrokeWidth);
        int stepTextHeight = height / 12;
        mPaint2.setTextSize(stepTextHeight);
        int stepTextWidth = (int) mPaint2.measureText("000000000000000", 0, "000000000000000".length());
        int stepNumberY = height / 2 + stepNumberHeight / 3;
        int stepNumberX = (int) (width / 2 - (2 + stepTextWidth) / 2.4);
        mPaint.setColor(Color.GRAY);
        canvas.drawText(finishTask + "/" + totalTask, stepNumberX, stepNumberY, mPaint);
    }
 
    public int getProgress() {
        return mProgress;
    }
 
 
    public void setProgress(int progress, int totalTask) {
        this.finishTask = progress;
        if (totalTask ==0){
            this.mProgress =0;
        }else {
            int newProgress = (int) (((double) progress / (double) totalTask) * 100);
            this.mProgress = newProgress;
        }
        this.totalTask = totalTask;
        this.invalidate();
    }
 
    public void setProgressNotInUiThread(int progress) {
        this.mProgress = progress;
        this.postInvalidate();
    }
 
    public int getStepCount() {
        return stepCount;
    }
 
    public void setStepCount(int stepCount) {
        this.stepCount = stepCount;
        this.postInvalidate();
    }
 
    public int getStepGoal() {
        return stepGoal;
    }
 
    public void setRGB(int r, int g, int b) {
        this.r = r;
        this.g = g;
        this.b = b;
    }
 
    public void setStepGoal(int stepGoal) {
        this.stepGoal = stepGoal;
        this.postInvalidate();
    }
}