CircleSeekBar.java
8.31 KB
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
303
304
package com.sw.laryngoscope.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import androidx.appcompat.widget.AppCompatSeekBar;
import android.util.AttributeSet;
import com.sw.laryngoscope.utils.Logger;
/**
* @author:
* @time 2022/12/06
* <p>
* 类描述:自定义SeekBar
*/
public class CircleSeekBar extends AppCompatSeekBar {
/**
* 绘制的类型 0为圆点,1为刻度
*/
private int drawStyle = 1;
/**
* 是否绘制文字
*/
private boolean drawText = false;
/**
* 刻度线画笔
*/
private Paint mRulerPaint;
/**
* 圆点画笔
*/
private Paint mCirclePaint;
/**
* 字体画笔
*/
private Paint mTestPaint;
/**
* 绘制的个数,等分数等于刻度线的个数加1
*/
private int mCount = 10;
/**
* 每条刻度线的宽度
*/
private int mRulerWidth = 2;
/**
* 刻度线的颜色
*/
private int mRulerColor = Color.WHITE;
/**
* 圆点的颜色
*/
private int mCircleColor = Color.WHITE;
/**
* 字体的颜色
*/
private int mTextColor = Color.RED;
/**
* 滑块上面是否要显示
*/
private boolean isShowTopOfThumb = false;
public CircleSeekBar(Context context) {
super(context);
init();
}
public CircleSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 初始化
*/
private void init() {
//创建绘制刻度线的画笔
mRulerPaint = new Paint();
mRulerPaint.setColor(mRulerColor);
mRulerPaint.setAntiAlias(true);
//创建绘制圆点的画笔
mCirclePaint = new Paint();
mCirclePaint.setColor(mCircleColor);
mCirclePaint.setAntiAlias(true);
//创建绘制文字的画笔
mTestPaint = new Paint();
mTestPaint.setTextSize(12F);
mTestPaint.setColor(mTextColor);
mTestPaint.setAntiAlias(true);
//Api21及以上调用,去掉滑块后面的背景
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSplitTrack(false);
}
}
/**
* 重写onDraw方法绘制刻度线
*
* @param canvas
*/
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
//极限条件校验
if (getWidth() <= 0 || mCount <= 0) {
return;
}
//获取每一份的长度
int length = 0;
if (drawStyle==0){
length = (getWidth()) / (mCount + 1);
}else if (drawStyle==1){
length = (getWidth() - getPaddingLeft() - getPaddingRight() - mCount * mRulerWidth) / (mCount + 1);
}
//计算刻度线的顶部坐标和底部坐标 注意绘制刻度的时候要设置minHeight
int rulerTop = getHeight() / 2 - getMinimumHeight() / 2;
int rulerBottom = rulerTop + getMinimumHeight();
float drawY = (float) ((rulerTop+rulerBottom) / 2);
float drawTestY = (float) ((rulerTop+rulerBottom) / 2) -50;
//获取滑块的位置信息
Rect thumbRect = null;
if (getThumb() != null) {
thumbRect = getThumb().getBounds();
}
int rulerLeft = 0 , rulerRight = 0;
float drawX = 0 ;
//绘制刻度线
for (int i = 1; i <= mCount; i++) {
//计算刻度线的左边坐标和右边坐标
if (drawStyle==0){
rulerLeft = i * length;
rulerRight = rulerLeft ;
drawX = (float) rulerLeft;
}else if (drawStyle==1){
rulerLeft = i * length + getPaddingLeft();
rulerRight = rulerLeft + mRulerWidth;
drawX = (float) ((rulerLeft+rulerRight)/2);
}
float drawTestX = (float) ((rulerLeft)-10);
//判断是否需要绘制刻度线
if (!isShowTopOfThumb && thumbRect != null && rulerLeft - getPaddingLeft() > thumbRect.left && rulerRight - getPaddingLeft() < thumbRect.right) {
continue;
}
//进行绘制
if (drawStyle==0){
canvas.drawCircle(drawX, drawY, 8.0F, mCirclePaint);
}else if (drawStyle==1){
canvas.drawRect(rulerLeft, rulerTop, rulerRight, rulerBottom, mRulerPaint);
}
//是否绘制文字
if (drawText){
canvas.drawText(String.valueOf(i-1), drawTestX, drawTestY, mTestPaint);
}
}
// 进度文字位置信息
String progressText = "8";
mTestPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);
// 进度百分比
float progressRatio = (float) getProgress() / getMax();
// thumb偏移量
float thumbOffset = (mThumbWidth - mProgressTextRect.width()) / 2 - mThumbWidth * progressRatio;
float thumbX = getWidth() * progressRatio + thumbOffset;
//这里修改文字的范围,上或下
float thumbY = getHeight() / 2f + mProgressTextRect.height() / 2f;
canvas.drawText(progressText, thumbX, thumbY, mTestPaint);
Logger.d("=====seek_bar======progressRatio " +
getWidth() * progressRatio + " getThumbOffset " + getThumbOffset());
}
// 进度文字位置信息
private Rect mProgressTextRect = new Rect();
private int mThumbWidth = 22;
/**
* 设置绘制的个数
*
* @param mCount
*/
public void setCount(int mCount) {
this.mCount = mCount;
requestLayout();
}
/**
* 设置绘制类型
*
* @param mStyle
*/
public void setDrawStyle (int mStyle) {
this.drawStyle = mStyle;
requestLayout();
}
/**
* 设置是否绘制文字
*
* @param drawText
*/
public void setDrawText (boolean drawText) {
this.drawText = drawText;
requestLayout();
}
/**
* 设置刻度线的宽度,单位(px)
*
* @param mRulerWidth
*/
public void setRulerWidth(int mRulerWidth) {
this.mRulerWidth = mRulerWidth;
requestLayout();
}
/**
* 设置刻度线的颜色
*
* @param mRulerColor
*/
public void setRulerColor(int mRulerColor) {
this.mRulerColor = mRulerColor;
if (mRulerPaint != null) {
mRulerPaint.setColor(mRulerColor);
requestLayout();
}
}
/**
* 设置圆点的颜色
*
* @param mCircleColor
*/
public void setCircleColor(int mCircleColor) {
this.mCircleColor = mCircleColor;
if (mCirclePaint != null) {
mCirclePaint.setColor(mCircleColor);
requestLayout();
}
}
/**
* 设置字体的颜色
*
* @param TextColor
*/
public void setTextColor(int TextColor) {
this.mTextColor = TextColor;
if (mTestPaint != null) {
mTestPaint.setColor(mTextColor);
requestLayout();
}
}
/**
* 设置整体颜色
*
* @param mCircleColor,mTextColor
*/
public void setAllColor(int mCircleColor,int mTextColor) {
//圆的颜色
this.mCircleColor = mCircleColor;
//字体的颜色
this.mTextColor = mTextColor;
if (mCirclePaint != null && mTestPaint != null) {
mCirclePaint.setColor(mCircleColor);
mTestPaint.setColor(mTextColor);
requestLayout();
}
}
/**
* 滑块上面是否需要显示刻度线
*
* @param isShowTopOfThumb
*/
public void setShowTopOfThumb(boolean isShowTopOfThumb) {
this.isShowTopOfThumb = isShowTopOfThumb;
requestLayout();
}
}