Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import android.view.Gravity
  2. import android.view.View
  3. import android.widget.PopupWindow
  4. import android.widget.SeekBar
  5. import org.jetbrains.anko.dip
  6.  
  7. /**
  8. * Wrapper adds floating view above seek bar. It follows thumb when user touches it.
  9. * Wrapper sets SeekBar.OnSeekBarChangeListener so if you need to track events from this listener,
  10. * set callbacks available in wrapper: [SeekBarFloatingWrapper.progressChanged],
  11. * [SeekBarFloatingWrapper.startTrackTouch], [SeekBarFloatingWrapper.stopTrackTouch]
  12. *
  13. * You can set custom animation style.
  14. * @see [PopupWindow]
  15. * @see [PopupWindow.setAnimationStyle]
  16. * @see [SeekBar]
  17. *
  18. * Created by Lucas on 2017-03-25.
  19. */
  20. class SeekBarFloatingWrapper {
  21.  
  22. private lateinit var popup: PopupWindow
  23.  
  24. lateinit var seekBar: SeekBar
  25. var width = 0
  26. set(value) {
  27. field = seekBar.context.dip(value)
  28. }
  29. var height = 0
  30. set(value) {
  31. field = seekBar.context.dip(value)
  32. }
  33.  
  34. var progressChanged: (seekBar: SeekBar,
  35. progress: Int,
  36. fromUser: Boolean) -> Unit = { _, _, _ -> }
  37. var startTrackTouch: () -> Unit = {}
  38. var stopTrackTouch: () -> Unit = {}
  39.  
  40. companion object {
  41. const val SIZE_UNCHANGED = -1
  42. const val DEFAULT_ANIMATION = 0
  43. }
  44.  
  45. fun wrap(seekBar: SeekBar, contentView: View): SeekBarFloatingWrapper {
  46. this.seekBar = seekBar
  47. width = seekBar.context.dip(120)
  48. height = seekBar.context.dip(72)
  49. popup = createPopupWindow(contentView)
  50. this.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
  51. override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
  52. if (fromUser) {
  53. popup.update(calcPreviewPosition(seekBar, popup.width),
  54. seekBar.top,
  55. SIZE_UNCHANGED,
  56. SIZE_UNCHANGED)
  57.  
  58. }
  59. progressChanged(seekBar, progress, fromUser)
  60. }
  61.  
  62. override fun onStartTrackingTouch(seekBar: SeekBar) {
  63. if (!popup.isShowing) {
  64. popup.showAtLocation(popup.contentView,
  65. Gravity.NO_GRAVITY,
  66. calcPreviewPosition(seekBar, popup.width),
  67. seekBar.top)
  68. }
  69. startTrackTouch
  70. }
  71.  
  72. override fun onStopTrackingTouch(seekBar: SeekBar) {
  73. if (popup.isShowing) popup.dismiss()
  74. stopTrackTouch
  75. }
  76. })
  77. return this
  78. }
  79.  
  80. fun setAnimationStyle(animationStyle: Int) {
  81. if (animationStyle != DEFAULT_ANIMATION) popup.animationStyle = animationStyle
  82. }
  83.  
  84. private fun createPopupWindow(content: View, animationStyle: Int = DEFAULT_ANIMATION): PopupWindow {
  85. val popup = PopupWindow()
  86. popup.contentView = content
  87. if (animationStyle != DEFAULT_ANIMATION) popup.animationStyle = animationStyle
  88. popup.width = width
  89. popup.height = height
  90. return popup
  91. }
  92.  
  93. private fun calcPreviewPosition(seekBar: SeekBar, popupWidth: Int): Int {
  94. val scale = seekBar.progress / seekBar.max.toFloat()
  95. val parent = seekBar.parent as View
  96. val x = (parent.width - popupWidth) * scale
  97. return x.toInt()
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement