blob: 6f8bbc1062b92fb2c2ee02fb078451b8c83ec7da (
plain)
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
|
/*
* Copyright 2018 Allan Wang
*
* 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 ca.allanwang.kau.ui
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.view.animation.Interpolator
/**
* Created by Allan Wang on 2017-11-10.
*
* Wrapper for value animator specifically dealing with progress values
* This is typically a float range of 0 to 1, but can be customized
* This differs in that everything can be done with simple listeners, which will be bundled
* and added to the backing [ValueAnimator]
*/
class ProgressAnimator private constructor(private vararg val values: Float) {
companion object {
inline fun ofFloat(crossinline builder: ProgressAnimator.() -> Unit) = ofFloat(0f, 1f) { builder() }
fun ofFloat(vararg values: Float, builder: ProgressAnimator.() -> Unit) = ProgressAnimator(*values).apply {
builder()
build()
}
}
private val animators: MutableList<(Float) -> Unit> = mutableListOf()
private val startActions: MutableList<() -> Unit> = mutableListOf()
private val endActions: MutableList<() -> Unit> = mutableListOf()
var duration: Long = -1L
var interpolator: Interpolator? = null
/**
* Add more changes to the [ValueAnimator] before running
*/
var extraConfigs: ValueAnimator.() -> Unit = {}
/**
* Range animator. Multiples the range by the current float progress before emission
*/
fun withAnimator(from: Float, to: Float, animator: (Float) -> Unit) = animators.add {
val range = to - from
animator(range * it + from)
}
/**
* Standard animator. Emits progress value as is
*/
fun withAnimator(animator: (Float) -> Unit) = animators.add(animator)
/**
* Start action to be called once when the animator first begins
*/
fun withStartAction(action: () -> Unit) = startActions.add(action)
/**
* End action to be called once when the animator ends
*/
fun withEndAction(action: () -> Unit) = endActions.add(action)
fun build() {
ValueAnimator.ofFloat(*values).apply {
if (this@ProgressAnimator.duration > 0L)
duration = this@ProgressAnimator.duration
if (this@ProgressAnimator.interpolator != null)
interpolator = this@ProgressAnimator.interpolator
addUpdateListener {
val progress = it.animatedValue as Float
animators.forEach { it(progress) }
}
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator?) {
startActions.forEach { it() }
}
override fun onAnimationEnd(animation: Animator?) {
endActions.forEach { it() }
}
override fun onAnimationCancel(animation: Animator?) {
endActions.forEach { it() }
}
})
extraConfigs()
start()
}
}
}
|