如何正确去掉SeekBar的Thumb和按压效果

324 阅读1分钟

1.setThumb(null)

先将thumb置空或者透明,但实际上,拖动SeekBar时仍旧会有个按压效果,如下图所示

image.png

2.setBackground(null)

只需要将背景置空,即不会有按压效果了

3.原因分析

拖动SeekBar显示按压效果,故从onTouchEvent看起,其中基本上都会调用trackTouchEvent:

private void trackTouchEvent(MotionEvent event) {
    // 省略部分代码

    setHotspot(x, y);
    setProgressInternal(Math.round(progress), true, false);
}

注意其中的setHotspot方法:

private void setHotspot(float x, float y) {
    final Drawable bg = getBackground();
    if (bg != null) {
        bg.setHotspot(x, y);
    }
}

Debug SeekBar可知,背景为RippleDrawable,可知按压效果为RippleDrawable绘制的,故将背景置空(setBackground(null))即可生效。