In the XML

   <androidx.appcompat.widget.AppCompatSeekBar
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:id="@+id/seekbar"
            android:layout_width="match_parent"
            android:layout_height="50dp" />
Copy the code

        val dp25 = SizeUtils.dp2px(25F)
        // Draw the background with the rounded corners half the widthseekbar.background = GradientDrawable().apply { shape = GradientDrawable.RECTANGLE cornerRadius = dp25.toFloat() color =  ColorStateList.valueOf(resources.getColor(R.color.colorPrimary)) }// Set the padding to the radius of the slider. So when progress is 0 and 100, the slider will just sit on the sides.
        // Otherwise, the slider will stick out and look ugly.
        seekbar.setPadding(dp25, 0, dp25, 0)
        // Offset, I have custom drawable, if there is a picture may be used
        seekbar.thumbOffset = 0
        // As above, this can be used when the slider displays transparency
        seekbar.splitTrack = true
        // Drawable of slider
        seekbar.thumb = GradientDrawable().apply {
            shape = GradientDrawable.OVAL
            color = ColorStateList.valueOf(Color.GRAY)
            val dp50 = SizeUtils.dp2px(50f)
            setSize(dp50, dp50)
        }
        // Verify that the slider disallows direct clicking, only dragging
        seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            var progress = seekbar.progress // Default progress
            var started = false // Not started by default
            override fun onProgressChanged(seekBar: SeekBar? , progressValue:Int, fromUser: Boolean) {
                if(! started) {// Find the size of the slider in the progress range, within this range to respond
                    // seekbar.max-tofloat ()/seekbar.width
                    / / seekbar. Thumb. IntrinsicWidth / 2 times the radius of the ball, is the threshold
                    val threshold = seekbar.max.toFloat()/seekbar.width*seekbar.thumb.intrinsicWidth/2
                    // The absolute value of the original progress and the current progress is less than the threshold and is user-operated, change the mark
                    if (abs(progressValue - progress) < threshold) {
                        if (fromUser) {
                            started = true}}else {
                        seekbar.progress = progress
                        return}}if (started) {
                    progress = progressValue
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?). {
                LogUtils.d("on start touch ${seekBar? .progress}")
// if(abs(seekbar? .progress? :0 - progress) >10){
// seekbar? .progress = progress
/ /}
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?). {
                LogUtils.d("on stop touch")
                started = false}})// For other logic, such as how the slider slides to the head and how the slider changes the image, set 'setThumb' yourself.
        // Slide your head, usually with the seekbar disabled. And so on, add your own 'setEnabled'

Copy the code