Android Seekbar thumb position in pixel

守給你的承諾、 提交于 2019-12-05 07:47:45

I did this:

int width = seekBar.getWidth()
            - seekBar.getPaddingLeft()
            - seekBar.getPaddingRight();
int thumbPos = seekBar.getPaddingLeft()
            + width
            * seekBar.getProgress()
            / seekBar.getMax();

The seekbar thumb has a thumb offset, which is basically a slight shift to the right. So you have to make sure that you take this into account as well:

int thumbPos = seekbar.getMeasuredWidth() * seekbar.getProgress() / seekbar.getMax() - seekbar.getThumbOffset();

You can use the getBounds() to get this info..

this sample code below is for instance aligning a TextView with the seekbar thumb position.

TextView sliderIndicator= ... Rect bounds = seekBar.getThumb().getBounds(); sliderIndicator.setTranslationX(seekBar.getLeft() + bounds.left);

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!