Android Seekbar thumb position in pixel

可紊 提交于 2019-12-07 01:56:50

问题


How can I get current position of thumb in pixel for SeekBar?

Currently I am trying to get the position on the bases of calculation.

Screen width, Seek Bar width, current seek progress and all. But I am not able to get exact position out of it.

Do any one have any idea?


Coding for same is as below.

I want to drow one component on top of SeekBar thumb.

translation = calculateTranslation(this.deviceScreenWidth, seekBarWidth, seekBar1T.getMax(), Integer.valueOf(ConstantData.seekbar_fifth.toString()), topComponentWidth, 0);
        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
            TranslateAnimation anim = new TranslateAnimation(translation - 1,
                    translation, 0, 0);
            anim.setFillAfter(true);
            anim.setDuration(0);
            edtTextRodUpDown.startAnimation(anim);  
        }else{
            edtTextRodUpDown.setTranslationX(translation);
        }

and calculate Translation like

    private float calculateTranslation(int screenWidth, int seekBarWidth, int seekMaxProgress, int currentProgress, int componentWidth, int extraDifference){
    double calculatedPosition = 0f;
    calculatedPosition = (double)((double)(seekBarWidth-((screenWidth-seekBarWidth))) / (double)seekMaxProgress) * (double)currentProgress;
    calculatedPosition = calculatedPosition - (double)(componentWidth/2);
    calculatedPosition = calculatedPosition + extraDifference;
    Double d = new Double(calculatedPosition);
    if(d < 0){
        return 0.0f;
    }else if(d + componentWidth > screenWidth){
        return screenWidth-componentWidth;
    }else{
        return d.floatValue();
    }
}

回答1:


I did this:

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



回答2:


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();



回答3:


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);



来源:https://stackoverflow.com/questions/20493577/android-seekbar-thumb-position-in-pixel

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