-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperCircleView.java
302 lines (270 loc) · 12.2 KB
/
SuperCircleView.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package tp.custom_components.project.widget;
import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.Date;
public class SuperCircleView extends View {
int mInrArcRadio; //内圆弧半径
float mRingWidth; //圆环的宽度
int mSegment; //分成几段
int mSegmentAngle; //每个圆弧之间的间隔
int mMaxCircleColor; //最外面圆的颜色
int mRingNormalColor; //默认圆环的颜色
int mColorSelect; //选中状态的颜色
float mRingSweepAngle; //每一段圆弧划过的角度
int mInitAngle; //起始角度
int subtextSize; //圆弧上的文字大小
int maintextSize; //中心圆上的文字大小
int mMinCircleRadio;//中心圆半径
int mMaxCircleRadio;//外圈圆半径
int mViewCenterX; //view宽的中心点
int mViewCenterY; //view高的中心点
Paint mPaint;
RectF mRectF; //用来绘制圆环的矩形区域
long touchTime;
int mClickState = -10086;
public SuperCircleView(Context context) {
this(context, null);
}
public SuperCircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SuperCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mInrArcRadio = 300; //内圆弧半径
mRingWidth = 160; //圆环的宽度
mSegment = 6; //分成几段
mSegmentAngle = 5; //每个圆弧之间的间隔角度
mMaxCircleColor = Color.parseColor("#e9e9e9"); //最外面圆的颜色
mRingNormalColor = Color.parseColor("#d9d9d9"); //圆环和中心圆的颜色
mColorSelect = Color.parseColor("#bfbfbf"); //选中状态的颜色
mInitAngle = 240; //起始角度 /**设置为240度即可显示竖直*/
maintextSize = 64; //中心圆上的text大小
subtextSize = 48; //圆环上的text大小
mMinCircleRadio = 240;//中心圆半径
mMaxCircleRadio = 520;//外圈圆半径
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
this.setWillNotDraw(false);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mViewCenterX = getMeasuredWidth() / 2;
mViewCenterY = getMeasuredHeight() / 2;
mRectF = new RectF(mViewCenterX - mInrArcRadio - mRingWidth / 2, mViewCenterY - mInrArcRadio - mRingWidth / 2, mViewCenterX + mInrArcRadio + mRingWidth / 2, mViewCenterY + mInrArcRadio + mRingWidth / 2);
mRingSweepAngle = (360 - mSegment * mSegmentAngle) / mSegment;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**draw a big circle*/
mPaint.setColor(mMaxCircleColor);
canvas.drawCircle(mViewCenterX, mViewCenterY, mMaxCircleRadio, mPaint);
/**add a litter circle*/
if (mClickState == -1) {
mPaint.setColor(mColorSelect);
} else {
mPaint.setColor(mRingNormalColor);
}
canvas.drawCircle(mViewCenterX, mViewCenterY, mMinCircleRadio, mPaint);
/**draw text*/
mPaint.setTextSize(maintextSize);
mPaint.setColor(Color.BLACK);
mPaint.setTextAlign(Paint.Align.CENTER);
//计算baseline
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
float baseline = mViewCenterY + distance;
canvas.drawText("OK", mViewCenterX, baseline, mPaint); //绘制中心文字
//画默认圆环
drawNormalRing(canvas);
}
/**
* 画默认圆环
*
* @param canvas
*/
private void drawNormalRing(Canvas canvas) {
Paint ringNormalPaint = new Paint(mPaint);
ringNormalPaint.setStyle(Paint.Style.STROKE);
ringNormalPaint.setStrokeWidth(mRingWidth);
ringNormalPaint.setColor(mRingNormalColor);
for (int i = 0; i < mSegment; i++) {
if (mClickState == i) {
ringNormalPaint.setColor(mColorSelect);
} else {
ringNormalPaint.setColor(mRingNormalColor);
}
canvas.drawArc(mRectF, mInitAngle + (i * mRingSweepAngle + i * mSegmentAngle), mRingSweepAngle, false, ringNormalPaint);
float startAngle = mInitAngle + i * (mRingSweepAngle + mSegmentAngle);
float endAngle = startAngle + mRingSweepAngle;
float middleAndle = (startAngle + endAngle) / 2;
float scion = 0;
float r = mInrArcRadio + (mRingWidth / 2);
double x1 = 0;
double y1 = 0;
float zz = Math.abs(middleAndle % 360); // 求圆弧中心所在象限
if (middleAndle > 360) {
if (zz > 0 && zz <= 90) { // 第一
scion = Math.abs(zz);
x1 = mViewCenterX + r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY + r * Math.sin(scion * Math.PI / 180);
} else if (zz > 90 && zz <= 180) { //第二
scion = Math.abs(180 - zz);
x1 = mViewCenterX - r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY + r * Math.sin(scion * Math.PI / 180);
} else if (zz > 180 && zz <= 270) { //第三
scion = Math.abs(zz - 180);
x1 = mViewCenterX - r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY - r * Math.sin(scion * Math.PI / 180);
} else if (zz > 270 && zz < 360) { //第四
scion = Math.abs(360 - zz);
x1 = mViewCenterX + r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY - r * Math.sin(scion * Math.PI / 180);
}
} else {
if (zz > 0 && zz <= 90) { // 第一
scion = Math.abs(zz);
x1 = mViewCenterX + r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY + r * Math.sin(scion * Math.PI / 180);
} else if (zz > 90 && zz <= 180) { //第二
scion = Math.abs(180 - zz);
x1 = mViewCenterX - r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY + r * Math.sin(scion * Math.PI / 180);
} else if (zz > 180 && zz <= 270) { //第三
scion = Math.abs(zz - 180);
x1 = mViewCenterX - r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY - r * Math.sin(scion * Math.PI / 180);
} else if (zz > 270 && zz < 360) { //第四
scion = Math.abs(360 - zz);
x1 = mViewCenterX + r * Math.cos(scion * Math.PI / 180);
y1 = mViewCenterY - r * Math.sin(scion * Math.PI / 180);
}
}
/***draw text**/
mPaint.setTextSize(subtextSize);
mPaint.setColor(Color.BLACK);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
mPaint.setTextAlign(Paint.Align.CENTER);
//计算baseline
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
float baseline = (float) (y1 + distance);
canvas.drawText("" + i, (float) x1, baseline, mPaint); //绘制中心文字
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchTime = new Date().getTime(); //按下时间,超过300ms计算一次点击
float evX = event.getX();
float evY = event.getY();
int distance = (int) xx(mViewCenterX, mViewCenterY, evX, evY); //计算点击位置距圆心的距离
if (distance <= mInrArcRadio) {
mClickState = -1; //点击坐标距离圆心的距离小于中心圆的半径,点击的是中心圆
} else if (distance <= mInrArcRadio + mRingWidth + 40) { //点击坐标距离圆心的距离小于大圆的半径,点击的就是圆弧
mClickState = getClickState(mViewCenterX, mViewCenterY, evX, evY, mInitAngle, mSegment, mRingSweepAngle);
} else {
// 点击范围不计算在View内
mClickState = -10086;
}
invalidate();
break;
case MotionEvent.ACTION_UP:
if ((new Date().getTime() - touchTime) < 300) {
//点击小于300ms视为一次点击事件
//doOnClickListener(mClickState)
}
mClickState = -10086;
invalidate();
break;
}
return true;
}
static double xx(float x1, float y1, float x2, float y2) {
return Math.sqrt(Math.abs((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
}
/**
* Math.max(min,current) == Math.min(current,max)
*
* @param x0
* @param y0
* @param x1
* @param y1
* @return
*/
static int getClickState(float x0, float y0, float x1, float y1, int mInitAngle, int count, float mRingSweepAngle) {
int state = -10086;
if (x1 > x0 && y1 <= y0) { //数学第一象限
if (mInitAngle > 270) {
state = getSection(mInitAngle + 90 - getAngle(x0, y0, x1, y1), count, mInitAngle, mRingSweepAngle);
} else {
state = getSection(Math.abs(270 - mInitAngle) + mInitAngle + (90 - getAngle(x0, y0, x1, y1)), count, mInitAngle, mRingSweepAngle);
}
} else if (x1 <= x0 && y1 < y0) { //数学第二象限
if (mInitAngle > 270) {
state = getSection(mInitAngle + 270 + getAngle(x0, y0, x1, y1), count, mInitAngle, mRingSweepAngle);
} else {
state = getSection(Math.abs(270 - mInitAngle) + mInitAngle + 270 + getAngle(x0, y0, x1, y1), count, mInitAngle, mRingSweepAngle);
}
} else if (x1 < x0 && y1 >= y0) { //数学第三象限
if (mInitAngle > 270) {
state = getSection(mInitAngle + 270 - getAngle(x0, y0, x1, y1), count, mInitAngle, mRingSweepAngle);
} else {
state = getSection(Math.abs(270 - mInitAngle) + mInitAngle + 270 - getAngle(x0, y0, x1, y1), count, mInitAngle, mRingSweepAngle);
}
} else if (x1 >= x0 && y1 > y0) { //数学第四象限
if (mInitAngle > 270) {
state = getSection(mInitAngle + 90 + getAngle(x0, y0, x1, y1), count, mInitAngle, mRingSweepAngle);
} else {
state = getSection(Math.abs(270 - mInitAngle) + mInitAngle + 90 + getAngle(x0, y0, x1, y1), count, mInitAngle, mRingSweepAngle);
}
}
return state;
}
/**
* 获得圆上一点与圆心夹角
*
* @param x1
* @param y1
* @param x2
* @param y2
* @return
*/
static int getAngle(float x1, float y1, float x2, float y2) {
float x = Math.abs(x1 - x2);
float y = Math.abs(y1 - y2);
// double z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
double tan = y / x;
double radian = Math.atan(tan);
double angle = 180 / (Math.PI / radian);
return (int) angle;
}
/**
* Math.max(min,current) == Math.min(current,max)
* 获取一个数在区间内
*
* @param current
* @param count
* @return
*/
static int getSection(int current, int count, int mInitAngle, float mRingSweepAngle) {
if (current > mInitAngle + 360) {
return 0;
}
for (int i = 0; i < count; i++) {
int min = (int) (mInitAngle + (360 / count) * i);
int max = (int) (min + mRingSweepAngle);
int a = Math.max(min, current);
int b = Math.min(current, max);
if (a == b) {
return i;
}
}
return -10086;
}
}