TextThumbSeekBar.java
4.74 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
package com.sw.laryngoscope.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewTreeObserver;
import android.widget.SeekBar;
import com.sw.laryngoscope.R;
@SuppressLint("AppCompatCustomView")
public class TextThumbSeekBar extends SeekBar {
// 画笔
private Paint mPaint;
// 进度文字位置信息
private Rect mProgressTextRect = new Rect();
// 滑块按钮宽度
private int mThumbWidth = dp2px(20);
private int mSeekBarMin;
private int mHeight;
private int mWidth;
private double mOneProgressWidth;
private float mThumbOffset;
private int mMaxProgress = 100;
private int mCurProgress = 0;
private String mProgressText = "";
public TextThumbSeekBar(Context context) {
this(context, null);
}
public TextThumbSeekBar(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.seekBarStyle);
//initData();
}
private void initObserver() {
ViewTreeObserver vto = getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mHeight = getMeasuredHeight();
mWidth = getMeasuredWidth();
initData();
return true;
}
});
}
private void initData() {
mPaint = new TextPaint();
mPaint.setAntiAlias(true);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setColor(getResources().getColor(R.color.about_red));
mPaint.setTextSize(sp2px(12));
// 如果不设置padding,当滑动到最左边或最右边时,滑块会显示不全
//setPadding(mThumbWidth / 2, 0, mThumbWidth / 2, 0);
mThumbOffset = 20 / 2;
mOneProgressWidth = (double) (mWidth - 2 * mThumbOffset) / (mMaxProgress);
}
public TextThumbSeekBar(Context context,AttributeSet attrs,int defStyleAttr){
super(context,attrs,defStyleAttr);
initObserver();
//initData();
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
/*String txt = "100%";
mPaint.getTextBounds(txt, 0, txt.length(), mProgressTextRect);
canvas.drawText(txt, getWidth() - mProgressTextRect.width(), getHeight() - mProgressTextRect.height() + 10, mPaint);
// 进度文字位置信息
String progressText = getProgress() + "%";
mPaint.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 - dp2px(10);
int thumbPos = getMeasuredWidth() * getProgress()/getMax() - getThumbOffset();
canvas.drawText(progressText, thumbPos, thumbY, mPaint);*/
mCurProgress = getProgress();
mProgressText = getProgress() + "";
float x = (float) (mCurProgress * mOneProgressWidth);
float textWidth = mPaint.measureText(mProgressText);
float textOffset = textWidth / 2;
if (x + textOffset > mWidth - mThumbOffset) {//超过view的右边
float exWidth = x + textOffset - (mWidth - mThumbOffset);
x -= exWidth;//避免超过右边
}
if (x + mThumbOffset < textOffset) {//超过左边
float exWidth = textOffset - (x + mThumbOffset);
x += exWidth;//避免超过左边
}
//mPaint.setStyle(Paint.Style.FILL); //fill the background with blue color
canvas.translate(mThumbOffset, 0);
//canvas.drawRect(x - textWidth, 50, x + textWidth, mHeight, mPaint);
canvas.drawText(mProgressText, x, mHeight - 29, mPaint);
}
public void setMix(int min){
mSeekBarMin=min;
}
/**
* dp转px
*
* @param dp dp值
* @return px值
*/
public int dp2px(float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
/**
* sp转px
*
* @param sp sp值
* @return px值
*/
private int sp2px(float sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
getResources().getDisplayMetrics());
}
}