Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. import android.graphics.Rect
  2. import android.support.v4.view.animation.LinearOutSlowInInterpolator
  3. import android.transition.*
  4. import android.view.View
  5. import android.view.ViewGroup
  6. import android.view.animation.Interpolator
  7.  
  8. /**
  9. * Created by Nick Cruz on 4/23/17
  10. */
  11. class PropagatingTransition(
  12. val sceneRoot: ViewGroup,
  13. var startingView: View? = null,
  14. val transition: Transition = Fade(Fade.IN),
  15. duration: Long = 600,
  16. interpolator: Interpolator = LinearOutSlowInInterpolator(),
  17. propagation: TransitionPropagation = CircularPropagation()) {
  18.  
  19. val targets: Collection<View>
  20.  
  21. init {
  22. targets = (0 until sceneRoot.childCount).map { sceneRoot.getChildAt(it) }
  23. transition.interpolator = interpolator
  24. transition.duration = duration
  25.  
  26. // Setting the transition's propagation. By default we will receive a CircularPropagation.
  27. transition.propagation = propagation
  28. }
  29.  
  30. fun start() {
  31. if (startingView == null && sceneRoot.childCount > 0) {
  32. startingView = sceneRoot.getChildAt(0)
  33. }
  34.  
  35. // Setting the transition's epicenter for its propagation. By default the epicenter shall
  36. // be the first child in the view, but a view can be passed in with this class's
  37. // constructor.
  38. transition.epicenterCallback = (startingView ?: sceneRoot).asEpicenter()
  39.  
  40. // Before starting the transition, hide the elements in the view.
  41. targets.forEach { it.visibility = View.INVISIBLE }
  42.  
  43. // Begin a delayed transition. After calling this, the TransitionManager will be waiting for
  44. // any changes in the layout and then animate those changes when they occur based on its
  45. // given transition.
  46. TransitionManager.beginDelayedTransition(sceneRoot, transition)
  47.  
  48. // Set the targets to be visible, which starts the transition.
  49. targets.forEach { it.visibility = View.VISIBLE }
  50. }
  51.  
  52. /**
  53. * @return Returns this view as an epicenter callback. In order to work with the Transition
  54. * framework, we retrieve the global visible Rect of the View in order to establish that as the
  55. * starting view.
  56. */
  57. private fun View.asEpicenter() : Transition.EpicenterCallback {
  58. val viewRect = Rect()
  59. getGlobalVisibleRect(viewRect)
  60. return object : Transition.EpicenterCallback() {
  61. override fun onGetEpicenter(transition: Transition?): Rect = viewRect
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement