ProgressTextView.java
3.33 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
package com.sw.laryngoscope.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import com.sw.laryngoscope.R;
public class ProgressTextView extends View {
private static final String TAG = ProgressTextView.class.getSimpleName();
private int mTextColor = Color.WHITE;
private int mHeight;
private int mWidth;
private double mOneProgressWidth;
private int mCurProgress = 0;
private String mProgressText = "";
private int mMaxProgress = 100;
private Paint mPaint;
private float mThumbOffset;
private int mTextSize = 18;
public ProgressTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (null != context && attrs != null) {
//TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressTextView);
//mTextColor = array.getColor(R.styleable.ProgressTextView_ptv_textColor, Color.WHITE);
//mTextSize = array.getDimensionPixelSize(R.styleable.ProgressTextView_ptv_thumWidth, 36);
//float thumWidth = array.getDimension(R.styleable.ProgressTextView_ptv_thumWidth, 20);
float thumWidth = 17;
Log.e(TAG, " thum width " + thumWidth);
mThumbOffset = thumWidth / 2;
}
initObserver();
}
private void initObserver() {
ViewTreeObserver vto = getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mHeight = getMeasuredHeight();
mWidth = getMeasuredWidth();
initPaint();
initData();
return true;
}
});
}
private void initPaint() {
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mPaint.setTextAlign(Paint.Align.CENTER);
//mPaint.setColor(mTextColor);
mPaint.setColor(getResources().getColor(R.color.color_0aede0));
}
private void initData() {
mOneProgressWidth = (double) (mWidth - 2 * mThumbOffset) / (mMaxProgress);
}
@Override
protected void onDraw(Canvas canvas) {
drawText(canvas);
super.onDraw(canvas);
}
//设置字体居中显示
private void drawText(Canvas canvas) {
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;//避免超过左边
}
canvas.translate(mThumbOffset, 0);
canvas.drawText(mProgressText, x, mHeight, mPaint);
}
//设置显示的进度位置和字符串
public void setProgress(int progress, String showText) {
mCurProgress = progress;
mProgressText = showText;
invalidate();
}
}