Android Seekbar拇指位置(像素)
如何获取SeekBar的当前拇指位置(以像素为单位) 目前我正试图在计算的基础上得到这个职位 屏幕宽度、搜索栏宽度、当前搜索进度和所有。但我无法从中得到确切的位置 有人知道吗Android Seekbar拇指位置(像素),android,seekbar,Android,Seekbar,如何获取SeekBar的当前拇指位置(以像素为单位) 目前我正试图在计算的基础上得到这个职位 屏幕宽度、搜索栏宽度、当前搜索进度和所有。但我无法从中得到确切的位置 有人知道吗 其编码如下所示 我想在SeekBar拇指上画一个组件 translation = calculateTranslation(this.deviceScreenWidth, seekBarWidth, seekBar1T.getMax(), Integer.valueOf(ConstantData.seekbar_fift
其编码如下所示 我想在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);
}
translation=calculateTranslation(this.deviceScreenWidth,seekBarWidth,seekBar1T.getMax(),Integer.valueOf(ConstantData.seekbar_fifth.toString()),topComponentWidth,0);
if(currentapiVersion
然后像这样计算翻译
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();
}
}
private float calculateTranslation(int screenWidth、int seekBarWidth、int seekMaprogress、int currentProgress、int componentWidth、int extraDifference){
双计算位置=0f;
计算位置=(双精度)((双精度)(seekBarWidth-((屏幕宽度seekBarWidth))/(双精度)seekMapProgress)*(双精度)当前进度;
calculatedPosition=calculatedPosition-(双精度)(组件宽度/2);
calculatedPosition=calculatedPosition+外差;
双d=新双d(计算位置);
if(d<0){
返回0.0f;
}否则如果(d+组件宽度>屏幕宽度){
返回屏幕宽度分量宽度;
}否则{
返回d.floatValue();
}
}
我这样做了:
int width = seekBar.getWidth()
- seekBar.getPaddingLeft()
- seekBar.getPaddingRight();
int thumbPos = seekBar.getPaddingLeft()
+ width
* seekBar.getProgress()
/ seekBar.getMax();
seekbar拇指有一个拇指偏移量,基本上是稍微向右移动。因此,您必须确保也考虑到这一点:
int thumbPos = seekbar.getMeasuredWidth() * seekbar.getProgress() / seekbar.getMax() - seekbar.getThumbOffset();
您可以使用
getBounds()
获取此信息
例如,下面的示例代码将TextView
与seekbar
拇指位置对齐
文本视图滑块指示器=。。。
Rect-bounds=seekBar.getThumb().getBounds();
sliderIndicator.setTranslationX(参见kbar.getLeft()+bounds.left);
如何在垂直方向上获得此功能seekbar@NaveenKumarM只需使用getHeight、getPaddingBottom和getPaddingTop即可。我不明白为什么这不起作用……据我所知,getThumbOffset()
计算拇指的宽度,所以,不要从位置中减去它。谢谢这个解决方案!在国际海事组织看来,这是一种可靠、平滑的解决方案。不需要第三方库,只要这样就可以了