Skip to content

Instantly share code, notes, and snippets.

@AmaroNeto
Created February 26, 2020 17:11
Show Gist options
  • Save AmaroNeto/153f3d3efca2e4f28affb0fa72ea5b6e to your computer and use it in GitHub Desktop.
Save AmaroNeto/153f3d3efca2e4f28affb0fa72ea5b6e to your computer and use it in GitHub Desktop.
package com.android.animationproject.utils
import android.animation.*
import android.view.View
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
object AnimatorUtils {
fun changeColor(
backgroundColor: (Int) -> Unit,
from: Int, to: Int,
time: Long
) = ValueAnimator().apply {
setIntValues(from, to)
setEvaluator(ArgbEvaluator())
addUpdateListener { valueAnimator -> backgroundColor(valueAnimator.animatedValue as Int) }
duration = time
}
fun resize(
view: View,
oldHeight: Int,
newHeight: Int,
time: Long
) =
ObjectAnimator.ofInt(oldHeight, newHeight).apply {
duration = time
interpolator = FastOutSlowInInterpolator()
addUpdateListener {
view.layoutParams.height = it.animatedValue as Int
view.requestLayout()
}
}
fun showWithFadeIn(view: View, duration: Long) =
ObjectAnimator.ofFloat(view, View.ALPHA, 1f).apply {
setDuration(duration)
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator?) {
super.onAnimationStart(animation)
view.alpha = 0f
view.visibility = View.VISIBLE
}
})
}
fun hideWithFadeOut(view: View, duration: Long) =
ObjectAnimator.ofFloat(view, View.ALPHA, 0f).apply {
setDuration(duration)
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
view.visibility = View.GONE
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment