1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.zx.xiaohui.helper
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.MotionEvent
class SeekbarWithThumbTouch @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : androidx.appcompat.widget.AppCompatSeekBar(context, attrs, defStyleAttr) {
private var thumbCall: (() -> Unit)? = null
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
super.onTouchEvent(event)
}
MotionEvent.ACTION_MOVE -> {
super.onTouchEvent(event)
}
MotionEvent.ACTION_UP -> if (isClickThumb(event, thumb.bounds)) {
if (thumbCall!=null) {
thumbCall!!()
}
}
}
return true
}
/**
* 设置点击事件只在thumb上有效
*
* @param event 点击事件
* @param rect thumb
*/
private fun isClickThumb(event: MotionEvent, rect: Rect): Boolean {
val x = event.x
val y = event.y
//根据左边距和thumb偏移量来确定thumb位置
val left = rect.left - thumbOffset + paddingLeft.toFloat()
val right = left + rect.width()
return x in left..right && y >= rect.top && y <= rect.bottom
}
fun setThumbClick(a:() -> Unit) {
this.thumbCall = a
}
}
|