CustomSeekBar.java
5.97 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
package com.wd.common.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatSeekBar;
import com.wd.base.log.Logger;
import com.wd.fastcoding.base.R;
public class CustomSeekBar extends AppCompatSeekBar {
private Paint mTextPaint;
private Paint mLinePaint;
private RectF mRectF;
private Drawable mThumb;
private float mMaxShowValue; //需要显示的最大值
private int mPrecisionMode;//精度模式
private int mViewWidth;
private int mCenterX;
private int mCenterY;
private int mThumbHeight;
private int mThumbWith;
private SongTimeCallBack songTimeCallBack;
/**
* 长滑块标识
*/
boolean longThumbFlag = false;
public CustomSeekBar(Context context) {
this(context, null);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//自定义属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DigitalThumbSeekbar);
mMaxShowValue = typedArray.getFloat(R.styleable.DigitalThumbSeekbar_maxShowValue, getMax());//获取最大显示值
mPrecisionMode = typedArray.getInt(R.styleable.DigitalThumbSeekbar_PrecisionMode, 0);//进度模式
typedArray.recycle();//释放
//设置滑块样式
mThumb = context.getResources().getDrawable(R.mipmap.seekbar_select);
setThumb(mThumb);
setThumbOffset(0);
initPaint();//初始化画笔
}
private void initPaint() {
//文字画笔
mTextPaint = new Paint();
mTextPaint.setARGB(255, 255, 255, 255);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(mThumb.getMinimumHeight() * 5.2f / 9);//文字大小与滑块高度的比 5.5:9
mTextPaint.setTextAlign(Paint.Align.CENTER); // 设置文本对齐方式,居中对齐
//进度画笔
mLinePaint = new Paint();
mLinePaint.setAntiAlias(true);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//父容器传过来的宽度的值
mViewWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
// Log.d("aaa",String.valueOf(mViewWidth));
//根据滑块的尺寸确定大小 布局文件中的android:layout_height="" 任何设置不会改变绘制的大小
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mThumb.getMinimumHeight(), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取滑块坐标
Rect thumbRect = getThumb().getBounds();
mCenterX = thumbRect.centerX();//中心X坐标
mCenterY = thumbRect.centerY(); //中心Y坐标
mThumbHeight = thumbRect.height();//滑块高度
mThumbWith = thumbRect.width();//滑块宽度
//绘制进度条
drawRect(canvas);
//绘制进度文字
drawProgress(canvas);
}
/**
* 绘制进度条
* @param canvas
*/
private void drawRect(Canvas canvas) {
int lineHight = 2;
//绘制左边的进度
mRectF = new RectF();
mRectF.left = 10;
mRectF.right = mCenterX;
mRectF.top = mCenterY - lineHight;
mRectF.bottom = mCenterY + lineHight;
mLinePaint.setColor(getResources().getColor(R.color.res_color_common_C11));
canvas.drawRoundRect(mRectF, lineHight, lineHight, mLinePaint);
//绘制右边剩余的进度
mRectF.left= mCenterX+mThumbWith/2.0f-7;
mRectF.right = mViewWidth-7;
mRectF.top = mCenterY - lineHight;
mRectF.bottom = mCenterY + lineHight;
mLinePaint.setARGB(255,0,0,0);
canvas.drawRoundRect(mRectF, lineHight, lineHight, mLinePaint);
}
/**
* 绘制显示的进度文本
* @param canvas
*/
private void drawProgress(Canvas canvas) {
String progressText = "";//要画的文字
if (songTimeCallBack != null) {
//将要画的时间对外提供
progressText = songTimeCallBack.getDrawText();
}
//测量文字高度
Rect bounds = new Rect();
mTextPaint.getTextBounds(progressText, 0, progressText.length(), bounds);
int mTextHeight = bounds.height();//文字高度
// float mTextWidth = mTextPaint.measureText(progress);
canvas.drawText(progressText, mCenterX, mCenterY + mTextHeight/2, mTextPaint);
}
public interface SongTimeCallBack {
String getSongTime(int progress);
String getDrawText();
}
public void setSongTimeCallBack(SongTimeCallBack songTimeCallBack) {
this.songTimeCallBack = songTimeCallBack;
if(songTimeCallBack != null){
String drawText = songTimeCallBack.getDrawText();
int length = drawText.length();
Logger.t("CustomSeekBar").d("drawTextLength========>"+drawText.length());
if(!longThumbFlag && length > 13){
longThumbFlag = true;
//设置滑块样式(总时长超过1小时,滑块背景使用长的)
mThumb = getResources().getDrawable(R.mipmap.seekbar_select_long);
setThumb(mThumb);
setThumbOffset(0);
initPaint();//初始化画笔
}
}
}
}