Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.view.Gravity
- import android.view.View
- import android.widget.PopupWindow
- import android.widget.SeekBar
- import org.jetbrains.anko.dip
- /**
- * Wrapper adds floating view above seek bar. It follows thumb when user touches it.
- * Wrapper sets SeekBar.OnSeekBarChangeListener so if you need to track events from this listener,
- * set callbacks available in wrapper: [SeekBarFloatingWrapper.progressChanged],
- * [SeekBarFloatingWrapper.startTrackTouch], [SeekBarFloatingWrapper.stopTrackTouch]
- *
- * You can set custom animation style.
- * @see [PopupWindow]
- * @see [PopupWindow.setAnimationStyle]
- * @see [SeekBar]
- *
- * Created by Lucas on 2017-03-25.
- */
- class SeekBarFloatingWrapper {
- private lateinit var popup: PopupWindow
- lateinit var seekBar: SeekBar
- var width = 0
- set(value) {
- field = seekBar.context.dip(value)
- }
- var height = 0
- set(value) {
- field = seekBar.context.dip(value)
- }
- var progressChanged: (seekBar: SeekBar,
- progress: Int,
- fromUser: Boolean) -> Unit = { _, _, _ -> }
- var startTrackTouch: () -> Unit = {}
- var stopTrackTouch: () -> Unit = {}
- companion object {
- const val SIZE_UNCHANGED = -1
- const val DEFAULT_ANIMATION = 0
- }
- fun wrap(seekBar: SeekBar, contentView: View): SeekBarFloatingWrapper {
- this.seekBar = seekBar
- width = seekBar.context.dip(120)
- height = seekBar.context.dip(72)
- popup = createPopupWindow(contentView)
- this.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
- override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
- if (fromUser) {
- popup.update(calcPreviewPosition(seekBar, popup.width),
- seekBar.top,
- SIZE_UNCHANGED,
- SIZE_UNCHANGED)
- }
- progressChanged(seekBar, progress, fromUser)
- }
- override fun onStartTrackingTouch(seekBar: SeekBar) {
- if (!popup.isShowing) {
- popup.showAtLocation(popup.contentView,
- Gravity.NO_GRAVITY,
- calcPreviewPosition(seekBar, popup.width),
- seekBar.top)
- }
- startTrackTouch
- }
- override fun onStopTrackingTouch(seekBar: SeekBar) {
- if (popup.isShowing) popup.dismiss()
- stopTrackTouch
- }
- })
- return this
- }
- fun setAnimationStyle(animationStyle: Int) {
- if (animationStyle != DEFAULT_ANIMATION) popup.animationStyle = animationStyle
- }
- private fun createPopupWindow(content: View, animationStyle: Int = DEFAULT_ANIMATION): PopupWindow {
- val popup = PopupWindow()
- popup.contentView = content
- if (animationStyle != DEFAULT_ANIMATION) popup.animationStyle = animationStyle
- popup.width = width
- popup.height = height
- return popup
- }
- private fun calcPreviewPosition(seekBar: SeekBar, popupWidth: Int): Int {
- val scale = seekBar.progress / seekBar.max.toFloat()
- val parent = seekBar.parent as View
- val x = (parent.width - popupWidth) * scale
- return x.toInt()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement