Skip to content

Instantly share code, notes, and snippets.

@cse-ariful
Created November 14, 2022 03:16
Show Gist options
  • Save cse-ariful/4ca4ddb0055225559fe3d416dbdef5d8 to your computer and use it in GitHub Desktop.
Save cse-ariful/4ca4ddb0055225559fe3d416dbdef5d8 to your computer and use it in GitHub Desktop.
Extension Function to animate a views x,y and alpha. And get a callback after the animation completed
fun View.animateView(xTranslate: Float = 0f, yTranslate: Float = 0f, endAlpha: Float = 0f, duration: Long = 300, onEnd: (() -> Unit)? = null) =
try {
val listener = object : Animator.AnimatorListener {
override fun onAnimationStart(p0: Animator?) {
}
override fun onAnimationEnd(p0: Animator?) {
onEnd?.invoke()
}
override fun onAnimationCancel(p0: Animator?) {
}
override fun onAnimationRepeat(p0: Animator?) {
}
}
this.animate()
.translationX(width * xTranslate)
.translationY(height * yTranslate)
.alpha(endAlpha)
.setListener(listener)
.setDuration(duration)
.start()
}catch(ex:Exception){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment