[英]Android Seekbar thumb position in pixel
如何获取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();
}
}
我这样做了:
int width = seekBar.getWidth()
- seekBar.getPaddingLeft()
- seekBar.getPaddingRight();
int thumbPos = seekBar.getPaddingLeft()
+ width
* seekBar.getProgress()
/ seekBar.getMax();
搜索栏拇指有一个拇指偏移,这基本上是向右轻微移动。 所以你必须确保你也考虑到这一点:
int thumbPos = seekbar.getMeasuredWidth() * seekbar.getProgress() / seekbar.getMax() - seekbar.getThumbOffset();
您可以使用getBounds()
来获取此信息。
下面的示例代码例如是将TextView
与搜索seekbar
拇指位置对齐。
TextView sliderIndicator= ... Rect bounds = seekBar.getThumb().getBounds(); sliderIndicator.setTranslationX(seekBar.getLeft() + bounds.left);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.