如何获取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);
}
Run Code Online (Sandbox Code Playgroud)
并计算翻译
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();
}
}
Run Code Online (Sandbox Code Playgroud)
chw*_*chw 23
我这样做了:
int width = seekBar.getWidth()
- seekBar.getPaddingLeft()
- seekBar.getPaddingRight();
int thumbPos = seekBar.getPaddingLeft()
+ width
* seekBar.getProgress()
/ seekBar.getMax();
Run Code Online (Sandbox Code Playgroud)
您可以使用getBounds()来获取此信息..
例如,下面的示例代码将 aTextView与seekbar拇指位置对齐。
TextView sliderIndicator= ...
Rect bounds = seekBar.getThumb().getBounds();
sliderIndicator.setTranslationX(seekBar.getLeft() + bounds.left);
| 归档时间: |
|
| 查看次数: |
5995 次 |
| 最近记录: |