Detecting thumb position in SeekBar prior to API version 16

后端 未结 4 1996
清酒与你
清酒与你 2021-02-07 11:11

Basically, I need to detect when the progress changes in the SeekBar and draw a text view on top of the thumb indicating the progress value.

I do this by implementing a

4条回答
  •  迷失自我
    2021-02-07 12:08

    Hopefully this can save some hours for someone else!

    I created this method instead of a custom seekBar:

    public int getSeekBarThumbPosX(SeekBar seekBar) {
        int posX;
        if (Build.VERSION.SDK_INT >= 16) {
            posX = seekBar.getThumb().getBounds().centerX();
        } else {
            int left = seekBar.getLeft() + seekBar.getPaddingLeft();
            int right = seekBar.getRight() - seekBar.getPaddingRight();
            float width = (float) (seekBar.getProgress() * (right - left)) / seekBar.getMax();
            posX = Math.round(width) + seekBar.getThumbOffset();
        }
        return posX;
    }
    

提交回复
热议问题