Use nested functions.
This commit is contained in:
parent
81c4b822e0
commit
630558ed4f
1 changed files with 14 additions and 35 deletions
|
@ -90,64 +90,43 @@ fun View.animate(
|
||||||
*/
|
*/
|
||||||
fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @ColorInt colorEnd: Int) {
|
fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @ColorInt colorEnd: Int) {
|
||||||
if (MainActivity.DEBUG) {
|
if (MainActivity.DEBUG) {
|
||||||
Log.d(
|
Log.d(TAG, "animateBackgroundColor() called with: view = [$this], duration = [$duration], " +
|
||||||
TAG,
|
"colorStart = [$colorStart], colorEnd = [$colorEnd]"
|
||||||
"animateBackgroundColor() called with: " +
|
|
||||||
"view = [" + this + "], duration = [" + duration + "], " +
|
|
||||||
"colorStart = [" + colorStart + "], colorEnd = [" + colorEnd + "]"
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
val empty = arrayOf(IntArray(0))
|
|
||||||
val viewPropertyAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorStart, colorEnd)
|
val viewPropertyAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorStart, colorEnd)
|
||||||
viewPropertyAnimator.interpolator = FastOutSlowInInterpolator()
|
viewPropertyAnimator.interpolator = FastOutSlowInInterpolator()
|
||||||
viewPropertyAnimator.duration = duration
|
viewPropertyAnimator.duration = duration
|
||||||
viewPropertyAnimator.addUpdateListener { animation: ValueAnimator ->
|
|
||||||
ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(animation.animatedValue as Int)))
|
fun listenerAction(color: Int) {
|
||||||
|
ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(color))
|
||||||
}
|
}
|
||||||
viewPropertyAnimator.addListener(
|
viewPropertyAnimator.addUpdateListener { listenerAction(it.animatedValue as Int) }
|
||||||
onCancel = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) },
|
viewPropertyAnimator.addListener(onCancel = { listenerAction(colorEnd) }, onEnd = { listenerAction(colorEnd) })
|
||||||
onEnd = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) }
|
|
||||||
)
|
|
||||||
viewPropertyAnimator.start()
|
viewPropertyAnimator.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun View.animateHeight(duration: Long, targetHeight: Int): ValueAnimator {
|
fun View.animateHeight(duration: Long, targetHeight: Int): ValueAnimator {
|
||||||
if (MainActivity.DEBUG) {
|
if (MainActivity.DEBUG) {
|
||||||
Log.d(
|
Log.d(TAG, "animateHeight: duration = [$duration], from $height to → $targetHeight in: $this")
|
||||||
TAG,
|
|
||||||
"animateHeight: duration = [" + duration + "], " +
|
|
||||||
"from " + height + " to → " + targetHeight + " in: " + this
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
val animator = ValueAnimator.ofFloat(height.toFloat(), targetHeight.toFloat())
|
val animator = ValueAnimator.ofFloat(height.toFloat(), targetHeight.toFloat())
|
||||||
animator.interpolator = FastOutSlowInInterpolator()
|
animator.interpolator = FastOutSlowInInterpolator()
|
||||||
animator.duration = duration
|
animator.duration = duration
|
||||||
animator.addUpdateListener { animation: ValueAnimator ->
|
|
||||||
val value = animation.animatedValue as Float
|
fun listenerAction(value: Int) {
|
||||||
layoutParams.height = value.toInt()
|
layoutParams.height = value
|
||||||
requestLayout()
|
requestLayout()
|
||||||
}
|
}
|
||||||
animator.addListener(
|
animator.addUpdateListener { listenerAction((it.animatedValue as Float).toInt()) }
|
||||||
onCancel = {
|
animator.addListener(onCancel = { listenerAction(targetHeight) }, onEnd = { listenerAction(targetHeight) })
|
||||||
layoutParams.height = targetHeight
|
|
||||||
requestLayout()
|
|
||||||
},
|
|
||||||
onEnd = {
|
|
||||||
layoutParams.height = targetHeight
|
|
||||||
requestLayout()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
animator.start()
|
animator.start()
|
||||||
return animator
|
return animator
|
||||||
}
|
}
|
||||||
|
|
||||||
fun View.animateRotation(duration: Long, targetRotation: Int) {
|
fun View.animateRotation(duration: Long, targetRotation: Int) {
|
||||||
if (MainActivity.DEBUG) {
|
if (MainActivity.DEBUG) {
|
||||||
Log.d(
|
Log.d(TAG, "animateRotation: duration = [$duration], from $rotation to → $targetRotation in: $this")
|
||||||
TAG,
|
|
||||||
"animateRotation: duration = [" + duration + "], " +
|
|
||||||
"from " + rotation + " to → " + targetRotation + " in: " + this
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
animate().setListener(null).cancel()
|
animate().setListener(null).cancel()
|
||||||
animate()
|
animate()
|
||||||
|
|
Loading…
Reference in a new issue