Skip to content

Instantly share code, notes, and snippets.

@matejsemancik
Last active April 2, 2019 14:27
Show Gist options
  • Select an option

  • Save matejsemancik/fbfa01fe0538bcdcfd3b13ee42d8138f to your computer and use it in GitHub Desktop.

Select an option

Save matejsemancik/fbfa01fe0538bcdcfd3b13ee42d8138f to your computer and use it in GitHub Desktop.
package com.thefuntasty.pitnyrezim.ui.onboarding.chat
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.DecelerateInterpolator
import android.view.animation.TranslateAnimation
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
class SampleAnimator : SimpleItemAnimator() {
companion object {
const val FROM_Y = 0.4f
const val TO_Y = 0f
const val FROM_X = 0f
const val TO_X = 0f
const val FROM_ALPHA = 0f
const val TO_ALPHA = 1f
const val ANIM_DURATION_MS = 400L
}
override fun animateAdd(holder: RecyclerView.ViewHolder): Boolean {
val translateAnim = TranslateAnimation(
Animation.RELATIVE_TO_SELF, FROM_X,
Animation.RELATIVE_TO_SELF, TO_X,
Animation.RELATIVE_TO_SELF, FROM_Y,
Animation.RELATIVE_TO_SELF, TO_Y
).apply {
duration = ANIM_DURATION_MS
interpolator = DecelerateInterpolator(1.5f)
}
val alphaAnim = AlphaAnimation(FROM_ALPHA, TO_ALPHA).apply {
duration = ANIM_DURATION_MS
interpolator = DecelerateInterpolator()
}
val set = AnimationSet(false).apply {
addAnimation(translateAnim)
addAnimation(alphaAnim)
setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(animation: Animation) = Unit
override fun onAnimationStart(animation: Animation) = Unit
override fun onAnimationEnd(animation: Animation) = dispatchAddFinished(holder)
})
}
holder.itemView.startAnimation(set)
return false
}
override fun animateRemove(holder: RecyclerView.ViewHolder) = false
override fun animateMove(
holder: RecyclerView.ViewHolder,
fromX: Int,
fromY: Int,
toX: Int,
toY: Int
) = false
override fun animateChange(
oldHolder: RecyclerView.ViewHolder,
newHolder: RecyclerView.ViewHolder,
fromLeft: Int,
fromTop: Int,
toLeft: Int,
toTop: Int
) = false
override fun runPendingAnimations() = Unit
override fun endAnimation(item: RecyclerView.ViewHolder) = Unit
override fun endAnimations() = Unit
override fun isRunning() = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment