Created
March 12, 2018 05:11
-
-
Save dshen6/024192cc2c3ca63503ff74331c10cebe to your computer and use it in GitHub Desktop.
Android TransitionManagerUtil
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
import android.annotation.TargetApi | |
import android.support.v4.view.animation.FastOutSlowInInterpolator | |
import android.support.v7.widget.RecyclerView | |
import android.transition.ChangeBounds | |
import android.transition.Fade | |
import android.transition.Transition | |
import android.transition.TransitionManager | |
import android.transition.TransitionSet | |
import android.view.View | |
import android.view.ViewGroup | |
import android.webkit.WebView | |
import android.widget.ListView | |
@TargetApi(19) | |
object TransitionHelper { | |
private const val DURATION_MS = 300L | |
private val INTERPOLATOR = FastOutSlowInInterpolator() | |
@JvmOverloads | |
fun beginDelayedTransition(container: View, listener: Transition.TransitionListener? = null, transitionSet: TransitionSet = createDefaultTransitionSet(), vararg ignored: ViewGroup = arrayOf()) { | |
if (container !is ViewGroup) { | |
throw IllegalStateException("Expected ViewGroup, got " + container) | |
} | |
if (listener != null) { | |
transitionSet.addListener(listener) | |
} | |
if (ignored.isNotEmpty()) { | |
for (view in ignored) { | |
transitionSet.excludeChildren(view, true) | |
} | |
} | |
// Excluding all ListViews/RecyclerViews from animations. Generally no need to animate them + very dangerous to do so | |
transitionSet.excludeTarget(ListView::class.java, true) | |
transitionSet.excludeTarget(RecyclerView::class.java, true) | |
transitionSet.excludeTarget(WebView::class.java, true) | |
TransitionManager.beginDelayedTransition(container, transitionSet) | |
} | |
private fun createDefaultTransitionSet(): TransitionSet { | |
val changeBounds = ChangeBounds().apply { | |
duration = DURATION_MS | |
interpolator = INTERPOLATOR | |
} | |
val fadeOut = Fade(Fade.OUT).apply { | |
duration = DURATION_MS | |
interpolator = INTERPOLATOR | |
} | |
val fadeIn = Fade(Fade.IN).apply { | |
duration = DURATION_MS | |
interpolator = INTERPOLATOR | |
} | |
return TransitionSet().apply { | |
ordering = TransitionSet.ORDERING_TOGETHER | |
addTransition(fadeOut) | |
addTransition(changeBounds) | |
addTransition(fadeIn) | |
} | |
} | |
class TransitionListenerAdapter : Transition.TransitionListener { | |
override fun onTransitionStart(transition: Transition) {} | |
override fun onTransitionEnd(transition: Transition) {} | |
override fun onTransitionCancel(transition: Transition) {} | |
override fun onTransitionPause(transition: Transition) {} | |
override fun onTransitionResume(transition: Transition) {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment