-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleProgressView.java
154 lines (109 loc) · 4.06 KB
/
CircleProgressView.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.suphu.demo.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.suphu.demo.R;
/**
* Created by huchao on 2017/8/8.
* 简单的圆形进度条 文字显示进度
*/
public class CircleProgressView extends View{
private int textColor;
private int textSize;
private int progress;//当前的进度
private int progressColor;
private int progressBgColor;
private Paint mPaint;
private Rect mTextRect;
private RectF mRectF;
private int progressWidth;//进度宽度
private int maxProgress;
public CircleProgressView(Context context) {
this(context,null);
}
public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
textSize= (int) ta.getDimension(R.styleable.CircleProgressView_progress_text_size,13);
textColor=ta.getColor(R.styleable.CircleProgressView_progress_text_color,Color.BLACK);
progressBgColor=ta.getColor(R.styleable.CircleProgressView_progress_bg_colcor, Color.parseColor("#66CCCCCC"));
progressColor=ta.getColor(R.styleable.CircleProgressView_progress_color,Color.RED);
progress=ta.getInt(R.styleable.CircleProgressView_progress,0);
progressWidth= (int) ta.getDimension(R.styleable.CircleProgressView_progress_width,5);
maxProgress=ta.getInt(R.styleable.CircleProgressView_max_progress,100);
ta.recycle();
mPaint=new Paint();
mPaint.setTextSize(textSize);
mTextRect=new Rect();
mRectF=new RectF();
setProgress();
}
@Override
protected void onDraw(Canvas canvas) {
//绘制背景圆弧
mPaint.setColor(progressBgColor);
mPaint.setStrokeWidth(progressWidth);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);//设置空心
//圆心坐标
int centre=getWidth()/2;
//半径
int radius=centre-progressWidth/2;
mRectF.left=centre-radius;
mRectF.top=centre-radius;
mRectF.bottom=centre+radius;
mRectF.right=centre+radius;
canvas.drawArc(mRectF,-90,360,false,mPaint);
//绘制进度圆
mPaint.setColor(progressColor);
canvas.drawArc(mRectF,-90,progress,false,mPaint);
//绘制进度文字
mPaint.setColor(textColor);
mPaint.setStyle(Paint.Style.FILL);
//按照100%的比例来显示
String text=progress*maxProgress/360+"%";
mPaint.getTextBounds(text,0,text.length(),mTextRect);
canvas.drawText(text,0,text.length(),centre-mTextRect.width()/2,centre+mTextRect.height()/2,mPaint);
Log.d("ondraw","draw");
}
private boolean isLoop;
//测试函数
public void setProgress(){
isLoop=true;
new Thread(new Runnable() {
@Override
public void run() {
while (isLoop){
if(progress<360){
progress++;
Log.d("setProgress","setProgress2:"+progress);
postInvalidate();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
isLoop=false;
}
}
}
}).start();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
isLoop=false;
}
}