Android Seekbar拇指在像素中的位置

时间:2013-12-10 11:43:08

标签: android seekbar

如何获取SeekBar的当前拇指位置?

目前我正在尝试以计算为基础获得该职位。

屏幕宽度,搜索条宽度,当前搜索进度等等。但我无法从中获得确切的位置。

有人有任何想法吗?


编码相同如下。

我想在SeekBar拇指上涂上一个组件。

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

并计算翻译

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

3 个答案:

答案 0 :(得分:20)

我这样做了:

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

答案 1 :(得分:2)

搜索栏拇指有一个拇指偏移,这基本上是向右轻微移动。所以你必须确保你也考虑到这一点:

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

答案 2 :(得分:1)

您可以使用getBounds()获取此信息..

下面的示例代码是将TextViewseekbar拇指位置对齐。

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

相关问题