Tom Taila
Tom Taila
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
}

    Tom Taila

    Written by

    Tom Taila

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade