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
|
class SimpleProgressBar : View {
constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
constructor(context: Context, attributeSet: AttributeSet, defaultAttr: Int) : super(context, attributeSet, defaultAttr)
private var mAnimator: ValueAnimator? = null
private var currentPresent = 0F
private lateinit var mRectF: RectF
private val backPaint = Paint()
private val fillPaint = Paint()
private var backColor = Color.GRAY
set(value) {
field = value
invalidate()
}
private var fillColor = Color.RED
set(value) {
field = value
invalidate()
}
init {
backPaint.color = backColor
backPaint.isAntiAlias = true
backPaint.style = Paint.Style.FILL
fillPaint.color = fillColor
fillPaint.isAntiAlias = true
fillPaint.style = Paint.Style.FILL
setPercent(0.6F)
}
fun setPercent(percent: Float, duration: Long = 2000L) {
mAnimator?.cancel()
mAnimator = ValueAnimator.ofFloat(currentPresent, percent)
mAnimator?.duration = duration
mAnimator?.addUpdateListener({
currentPresent = it.animatedValue as Float
invalidate()
})
mAnimator?.start()
}
private var mWidth: Int = 0
private var mHeight: Int = 0
private var radius: Float = 0F
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
mRectF = RectF(
paddingLeft.toFloat(),
paddingTop.toFloat(),
(w - paddingRight).toFloat(),
(h - paddingBottom).toFloat())
mWidth = w - paddingLeft - paddingRight
mHeight = h - paddingTop - paddingBottom
radius = mHeight / 2F
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
//draw background rect
canvas.save()
mRectF.right = mWidth.toFloat()
canvas.drawRoundRect(mRectF, radius, radius, backPaint)
//draw fill progress
val fillWidth = mWidth * currentPresent
when {
fillWidth > 0F && fillWidth < radius * 2 -> {
mRectF.right = fillWidth
canvas.clipRect(mRectF)
mRectF.right = radius * 2
canvas.drawRoundRect(mRectF, radius, radius, fillPaint)
}
fillWidth > radius -> {
mRectF.right = fillWidth
canvas.drawRoundRect(mRectF, radius, radius, fillPaint)
}
}
canvas.restore()
}
}
|