This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnimatedValue<T>( | |
initial: T, | |
private val spec: AnimationSpec, | |
private val updateCallback: (value: T) -> Unit, | |
private val evaluation: (fraction: Float, startValue: T, endValue: T) -> T | |
) { | |
private var animator = ValueAnimator.ofFloat(0f, 1f).apply { | |
duration = spec.duration | |
interpolator = spec.interpolator | |
addUpdateListener { currentValue = evaluation(it.animatedFraction, startValue, value) } | |
} | |
init { | |
updateCallback(initial) | |
} | |
var value: T = initial | |
set(value) { | |
if (field != value) { | |
field = value | |
animate() | |
} | |
} | |
private var startValue: T = initial | |
private var currentValue: T = initial | |
set(value) { | |
field = value | |
updateCallback(value) | |
} | |
private fun animate() { | |
startValue = currentValue | |
animator.cancel() | |
animator.start() | |
} | |
fun dispose() { | |
animator.cancel() | |
animator.removeAllListeners() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment