Advertisement
MattySkala

Untitled

Mar 15th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. /**
  2. * A base fragment for onboarding page.
  3. */
  4. abstract class OnboardingPageFragment<V> : Fragment() {
  5. companion object {
  6. const val KEY_VALUE = "value"
  7. const val KEY_ANIMATED = "animated"
  8. }
  9.  
  10. abstract var value: V
  11. var animated: Boolean = false
  12.  
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15.  
  16. if (savedInstanceState != null) {
  17. value = savedInstanceState.getSerializable(KEY_VALUE) as V
  18. animated = savedInstanceState.getBoolean(KEY_ANIMATED)
  19. }
  20. }
  21.  
  22. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  23. super.onViewCreated(view, savedInstanceState)
  24.  
  25. if (!animated) {
  26. view.post({
  27. initAnimation()
  28. })
  29. }
  30.  
  31. if (!animated && userVisibleHint) {
  32. onFragmentVisible()
  33. }
  34. }
  35.  
  36. override fun setUserVisibleHint(isVisibleToUser: Boolean) {
  37. super.setUserVisibleHint(isVisibleToUser)
  38.  
  39. if (isVisibleToUser && !animated && view != null) {
  40. onFragmentVisible()
  41. }
  42. }
  43.  
  44. override fun onSaveInstanceState(outState: Bundle) {
  45. outState.putSerializable(KEY_VALUE, value as Serializable)
  46. outState.putBoolean(KEY_ANIMATED, animated)
  47. }
  48.  
  49. open fun onFragmentVisible() {
  50. view?.post({
  51. startAnimation()
  52. })
  53. animated = true
  54. }
  55.  
  56. abstract fun initAnimation()
  57. abstract fun startAnimation()
  58. }
  59.  
  60.  
  61.  
  62. /**
  63. * A fragment for onboarding goal and fitness pages.
  64. */
  65. abstract class OnboardingButtonsFragment<V> : OnboardingPageFragment<V>() {
  66. var endWidth: Int = 0
  67.  
  68. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  69. return inflater.inflate(R.layout.fragment_onboarding_buttons, container, false)
  70. }
  71.  
  72. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  73. super.onViewCreated(view, savedInstanceState)
  74.  
  75. btn1.setOnClickListener {
  76. setButtonSelected(it)
  77. }
  78.  
  79. btn2.setOnClickListener {
  80. setButtonSelected(it)
  81. }
  82.  
  83. btn3.setOnClickListener {
  84. setButtonSelected(it)
  85. }
  86. }
  87.  
  88. abstract fun setButtonSelected(view: View)
  89.  
  90. override fun initAnimation() {
  91. val btns = arrayOf(btn1, btn2, btn3)
  92. val labels = arrayOf(btn1Label, btn2Label, btn3Label)
  93.  
  94. for (i in 0 until btns.size) {
  95. val btn = btns[i]
  96. val startWidth = context!!.dip(50)
  97. endWidth = btn.width
  98. val params = btn.layoutParams
  99. params.width = startWidth
  100. btn.layoutParams = params
  101.  
  102. val label = labels[i]
  103. label.alpha = 0f
  104. }
  105. }
  106.  
  107. override fun startAnimation() {
  108. val btns = arrayOf(btn1, btn2, btn3)
  109. val labels = arrayOf(btn1Label, btn2Label, btn3Label)
  110.  
  111. for (i in 0 until btns.size) {
  112. val btn = btns[i]
  113. val startWidth = context!!.dip(50)
  114.  
  115. val animator = ValueAnimator.ofInt(startWidth, endWidth)
  116. animator.startDelay = 100 + i * 50L
  117. animator.duration = 300
  118. animator.interpolator = AccelerateDecelerateInterpolator()
  119. animator.addUpdateListener { animation ->
  120. // TODO: make own view for better performance
  121. btn.layoutParams.width = animation.animatedValue as Int
  122. btn.requestLayout()
  123. }
  124. animator.start()
  125.  
  126. val label = labels[i]
  127. label.animate()
  128. .alpha(1f)
  129. .setDuration(200)
  130. .setStartDelay(200 + i * 50L)
  131. .start()
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement