有哪些优点
- 继承自原生SeekBar,显示效果与原生相当
- 核心代码只有20行左右,无侵入,使用Api跟原生别无二致
- 兼容性好,不同于网上使用旋转实现的方案,某些场景下有各种各样的bug
上个效果图(参考MIUI12配色)
代码
/**
* author: mtdhllf
* time : 2020/04/28 9:57
* desc :
*/
class VerticalSeekBar : AppCompatSeekBar {
private var mOnSeekBarChangeListener: OnSeekBarChangeListener? = null
override fun setOnSeekBarChangeListener(l: OnSeekBarChangeListener) {
mOnSeekBarChangeListener = l
super.setOnSeekBarChangeListener(l)
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (!isEnabled) {
return false
}
parent.requestDisallowInterceptTouchEvent(true)
when (event!!.action) {
MotionEvent.ACTION_DOWN -> {
mOnSeekBarChangeListener?.onStartTrackingTouch(this)
val i = max - (max * event.y * 1f / height).toInt()
progress = i
if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
parent.requestDisallowInterceptTouchEvent(false)
mOnSeekBarChangeListener?.onStopTrackingTouch(this)
}
}
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
val i = max - (max * event.y * 1f / height).toInt()
progress = i
if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
parent.requestDisallowInterceptTouchEvent(false)
mOnSeekBarChangeListener?.onStopTrackingTouch(this)
}
}
}
return true
}
constructor(ctx: Context) : super(ctx)
constructor(ctx: Context, attrs: AttributeSet?) : this(ctx, attrs, 0)
constructor(ctx: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(ctx, attrs, defStyleAttr)
init {
thumb = null
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
mOnSeekBarChangeListener = null
}
}