TextThumbSeekBar1.java
4.09 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
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.widget.SeekBar;
import com.sw.laryngoscope.R;
@SuppressLint("AppCompatCustomView")
public class TextThumbSeekBar1 extends SeekBar {
// 画笔
private Paint mPaint;
// 进度文字位置信息
private Rect mProgressTextRect = new Rect();
// 滑块按钮宽度
private int mThumbWidth = dp2px(20);
private int mSeekBarMin;
public TextThumbSeekBar1(Context context) {
this(context, null);
}
public TextThumbSeekBar1(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.seekBarStyle);
initData();
}
private void initData() {
mPaint = new TextPaint();
mPaint.setAntiAlias(true);
mPaint.setColor(getResources().getColor(R.color.color_0aede0));
mPaint.setTextSize(sp2px(18));
// 如果不设置padding,当滑动到最左边或最右边时,滑块会显示不全
setPadding(mThumbWidth / 2, 0, mThumbWidth / 2, 0);
}
public TextThumbSeekBar1(Context context, AttributeSet attrs, int defStyleAttr){
super(context,attrs,defStyleAttr);
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);
/*String progressText = getProgress() + "%";
mPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);
//The height of the displacement area of our bar (the height - the padding - the thumb size)
int bottomPadding = getPaddingLeft() - getThumbOffset();
int topPadding = getPaddingRight() - getThumbOffset();
int height = getHeight() - bottomPadding - topPadding;
float progressRatio = (float) getProgress() / getMax();
float thumbOffset = getThumb().getIntrinsicWidth() * (.5f - progressRatio);
// In our rotated canvas, positive x is higher, positive Y is more to the left
float thumbX = progressRatio * height + bottomPadding + thumbOffset;
float thumbY = getWidth() / 2f;
//Since we rotate the canvas again when drawing the text, we need to translate the coords for the text
float textX = thumbY - mProgressTextRect.width() / 2f;
float textY = -( thumbX - mProgressTextRect.height()/2f );
canvas.drawText(progressText, textY, textX, 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());
}
}