Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import android.view.View
  2. import android.view.ViewGroup
  3. import android.view.animation.Animation
  4. import android.view.animation.Transformation
  5.  
  6. /**
  7. * Created by andrii.kovalchuk on 3/27/17.
  8. */
  9.  
  10. class DropDownAnimation(
  11. private val view: View,
  12. var down: Boolean,
  13. private val durationAnimation: Long
  14. ) : Animation() {
  15.  
  16. init {
  17. this.duration = durationAnimation
  18. }
  19.  
  20. override fun applyTransformation(interpolatedTime: Float, transformation: Transformation?) {
  21. when {
  22. down -> expand(interpolatedTime, transformation)
  23. else -> collapse(interpolatedTime, transformation)
  24. }
  25. }
  26.  
  27. override fun willChangeBounds(): Boolean {
  28. return true
  29. }
  30.  
  31. private fun expand(interpolatedTime: Float, transformation: Transformation?) {
  32. view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
  33. val targetHeight = view.measuredHeight
  34. // Older versions of android (pre API 21) cancel animations for views with a height of 0.
  35. view.layoutParams.height = 1
  36. view.visibility = View.VISIBLE
  37.  
  38. view.layoutParams.height = if (interpolatedTime == 1f)
  39. ViewGroup.LayoutParams.WRAP_CONTENT
  40. else
  41. (targetHeight * interpolatedTime).toInt()
  42. view.requestLayout()
  43. }
  44.  
  45. private fun collapse(interpolatedTime: Float, transformation: Transformation?) {
  46. val initialHeight = view.measuredHeight
  47.  
  48. if (interpolatedTime == 1f) {
  49. view.visibility = View.GONE
  50. } else {
  51. view.layoutParams.height = initialHeight - (initialHeight * interpolatedTime).toInt()
  52. view.requestLayout()
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement