/
BaseCurveProgressView.kt
259 lines (214 loc) · 9.17 KB
/
BaseCurveProgressView.kt
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright 2016 Vladimir Jovanovic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vlad1m1r.lemniscate.base
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.os.Parcel
import android.os.Parcelable
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.vlad1m1r.lemniscate.base.models.DrawState
import com.vlad1m1r.lemniscate.base.models.Points
import com.vlad1m1r.lemniscate.base.models.ViewSize
import com.vlad1m1r.lemniscate.base.settings.AnimationSettings
import com.vlad1m1r.lemniscate.base.settings.CurveSettings
import com.vlad1m1r.lemniscate.sample.lemniscate.R
import kotlin.math.min
import kotlin.math.round
abstract class BaseCurveProgressView : View, IBaseCurveView {
protected var presenter: IBaseCurvePresenter = BaseCurvePresenter(
this,
CurveSettings(),
ViewSize(), AnimationSettings(),
DrawState(Path()),
Points())
private var valueAnimator: ValueAnimator? = null
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
val curveAttributes = context.obtainStyledAttributes(
attrs,
R.styleable.BaseCurveProgressView,
0, 0)
val colorAccentAttributes = context.obtainStyledAttributes(attrs, intArrayOf(R.attr.colorAccent))
try {
val colorAccent = colorAccentAttributes.getColor(0, 0)
presenter.curveSettings.lineLength.lineMaxLength = curveAttributes.getFloat(R.styleable.BaseCurveProgressView_maxLineLength, 0.8f)
presenter.curveSettings.lineLength.lineMinLength = curveAttributes.getFloat(R.styleable.BaseCurveProgressView_minLineLength, 0.4f)
presenter.curveSettings.color = curveAttributes.getColor(R.styleable.BaseCurveProgressView_lineColor, colorAccent)
presenter.curveSettings.hasHole = curveAttributes.getBoolean(R.styleable.BaseCurveProgressView_hasHole, false)
presenter.curveSettings.strokeWidth = curveAttributes.getDimension(R.styleable.BaseCurveProgressView_strokeWidth, resources.getDimension(R.dimen.lemniscate_stroke_width))
presenter.curveSettings.precision = curveAttributes.getInteger(R.styleable.BaseCurveProgressView_precision, 200)
presenter.animationSettings.duration = curveAttributes.getInteger(R.styleable.BaseCurveProgressView_duration, 1000)
} finally {
curveAttributes.recycle()
colorAccentAttributes.recycle()
}
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
presenter.recreatePoints()
canvas.drawPath(presenter.drawState.path, presenter.curveSettings.paint)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val defaultSize = resources.getDimension(R.dimen.lemniscate_preferred_height) * presenter.viewSize.sizeMultiplier
val xPadding = paddingLeft + paddingRight
val yPadding = paddingTop + paddingBottom
val viewSize = getMaxViewSquareSize(
measuredHeight,
measuredWidth,
xPadding,
yPadding
)
presenter.viewSize.size = getViewDimension(
View.MeasureSpec.getMode(widthMeasureSpec),
viewSize.toFloat(),
defaultSize
)
setMeasuredDimension(round(presenter.viewSize.size + xPadding).toInt(), round(presenter.viewSize.size + yPadding).toInt())
}
internal fun getMaxViewSquareSize(height: Int, width: Int, xPadding: Int, yPadding: Int): Int {
return min(height - yPadding, width - xPadding)
}
internal fun getViewDimension(mode: Int, viewSize: Float, defaultSize: Float): Float {
return when {
viewSize == 0.0f -> defaultSize
mode == View.MeasureSpec.EXACTLY -> viewSize
mode == View.MeasureSpec.AT_MOST -> Math.min(defaultSize, viewSize)
else -> defaultSize
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
animateLemniscate()
}
private fun animateLemniscate() {
valueAnimator?.end()
valueAnimator = ValueAnimator.ofInt(presenter.curveSettings.precision - 1, 0).apply {
duration = presenter.animationSettings.duration.toLong()
repeatCount = -1
repeatMode = ValueAnimator.RESTART
interpolator = LinearInterpolator()
addUpdateListener { animation ->
presenter.updateStartingPointOnCurve(animation.animatedValue as Int)
}
start()
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
valueAnimator?.end()
}
var strokeWidth
get() = presenter.curveSettings.strokeWidth
set(strokeWidth) {
presenter.curveSettings.strokeWidth = strokeWidth
}
var lineMaxLength
get() = presenter.curveSettings.lineLength.lineMaxLength
set(lineMaxLength) {
presenter.curveSettings.lineLength.lineMaxLength = lineMaxLength
}
var lineMinLength
get() = presenter.curveSettings.lineLength.lineMinLength
set(lineMinLength) {
presenter.curveSettings.lineLength.lineMaxLength = lineMinLength
}
var color
get() = presenter.curveSettings.color
set(color) {
presenter.curveSettings.color = color
}
var duration
get() = presenter.animationSettings.duration
set(duration) {
presenter.animationSettings.duration = duration
if (valueAnimator != null) valueAnimator!!.duration = duration.toLong()
}
var precision
get() = presenter.curveSettings.precision
set(precision) {
presenter.curveSettings.precision = precision
animateLemniscate()
invalidate()
}
var sizeMultiplier
get() = presenter.viewSize.sizeMultiplier
set(sizeMultiplier) {
presenter.viewSize.sizeMultiplier = sizeMultiplier
requestLayout()
invalidate()
}
var size = presenter.viewSize.size
get() = presenter.viewSize.size
private set
open var hasHole
get() = presenter.curveSettings.hasHole
set(hasHole) {
presenter.curveSettings.hasHole = hasHole
}
public override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
val ss = BaseCurveSavedState(superState)
ss.curveSettings = this.presenter.curveSettings
ss.animationSettings = this.presenter.animationSettings
return ss
}
public override fun onRestoreInstanceState(state: Parcelable) {
if (state is BaseCurveSavedState) {
super.onRestoreInstanceState(state.superState)
this.presenter.curveSettings = state.curveSettings
this.presenter.animationSettings = state.animationSettings
} else {
super.onRestoreInstanceState(state)
}
}
protected open class BaseCurveSavedState : View.BaseSavedState {
internal lateinit var curveSettings: CurveSettings
internal lateinit var animationSettings: AnimationSettings
constructor(superState: Parcelable) : super(superState)
constructor(state: Parcel) : super(state) {
this.curveSettings = state.readParcelable(CurveSettings::class.java.classLoader)
this.animationSettings = state.readParcelable(AnimationSettings::class.java.classLoader)
}
override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeParcelable(this.curveSettings, flags)
out.writeParcelable(this.animationSettings, flags)
}
companion object {
@JvmField
val CREATOR = object : Parcelable.Creator<BaseCurveSavedState> {
override fun createFromParcel(source: Parcel): BaseCurveSavedState {
return BaseCurveSavedState(source)
}
override fun newArray(size: Int): Array<BaseCurveSavedState?> {
return arrayOfNulls(size)
}
}
}
}
override fun invalidateProgressView() {
invalidate()
}
override fun requestProgressViewLayout() {
requestLayout()
}
}