Jul 24, 2017 · 1 min read
The color filter animation isn’t ideal since there will be a flash where it will be completely white at the beginning. Here’s some code without having to resort to playing with alpha. Note that in the following code I’m actually changing the color filter of a views background, but the idea is identical. [Also note it’s in Kotlin]
fun changeTint(context: Context, view: View, colorFromResId: Int, colorToResId: Int, duration: Long = DURATION_MEDIUM): Animator {
val colorFrom = ContextCompat.getColor(context, colorFromResId)
val colorTo = ContextCompat.getColor(context, colorToResId)
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
colorAnimation.duration = duration // milliseconds
colorAnimation.addUpdateListener { animator -> view.background.setColorFilter(animator.animatedValue as Int, PorterDuff.Mode.SRC_ATOP) }
return colorAnimation
}