Created
February 26, 2020 17:11
-
-
Save AmaroNeto/153f3d3efca2e4f28affb0fa72ea5b6e to your computer and use it in GitHub Desktop.
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
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