在API版本16之前检测SeekBar中的拇指位置

基本上,我需要检测SeekBar中进度何时发生变化,并在拇指顶部绘制一个指示进度值的文本视图。

我这样做是通过实现OnSeekBarChangeListenerpublic void onProgressChanged(SeekBar seekBar, int progress, boolean b)方法,我调用Rect thumbRect = seekBar.getThumb().getBounds(); 确定拇指的位置。

这非常getThumb() ,但显然getThumb()仅在API级别16+(Android 4.1)中可用,在早期版本中导致NoSuchMethodError

知道如何解决这个问题吗?

我能够使用自己的类来获取Thumb:

MySeekBar.java

 package mobi.sherif.seekbarthumbposition; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.SeekBar; public class MySeekBar extends SeekBar { public MySeekBar(Context context) { super(context); } public MySeekBar(Context context, AttributeSet attrs) { super(context, attrs); } public MySeekBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } Drawable mThumb; @Override public void setThumb(Drawable thumb) { super.setThumb(thumb); mThumb = thumb; } public Drawable getSeekBarThumb() { return mThumb; } } 

在活动中,这完美地运作:

 package mobi.sherif.seekbarthumbposition; import android.app.Activity; import android.graphics.Rect; import android.os.Bundle; import android.util.Log; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends Activity implements OnSeekBarChangeListener { MySeekBar mSeekBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSeekBar = (MySeekBar) findViewById(R.id.seekbar); mSeekBar.setOnSeekBarChangeListener(this); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { Rect thumbRect = mSeekBar.getSeekBarThumb().getBounds(); Log.v("sherif", "(" + thumbRect.left + ", " + thumbRect.top + ", " + thumbRect.right + ", " + thumbRect.bottom + ")"); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } } 

希望这可以为其他人节省一些时间!

我创建了这个方法而不是自定义的seekBar:

 public int getSeekBarThumbPosX(SeekBar seekBar) { int posX; if (Build.VERSION.SDK_INT >= 16) { posX = seekBar.getThumb().getBounds().centerX(); } else { int left = seekBar.getLeft() + seekBar.getPaddingLeft(); int right = seekBar.getRight() - seekBar.getPaddingRight(); float width = (float) (seekBar.getProgress() * (right - left)) / seekBar.getMax(); posX = Math.round(width) + seekBar.getThumbOffset(); } return posX; } 

一个出色的解决方案! 谢谢。 添加它以使用自定义搜索条只需要修改你的xml是唯一有用的

   

android:thumbOffset =“8dp” – 是拇指的HALF,最好是spesify,因此文本中心和拇指不会不匹配

定位可能如下所示:

 int possition = (int) (seekBar.getX() //the beginning of the seekbar + seekBar.getThumbOffset() / 2 //the half of our thumb - the text to be above it's centre + ((MySeekBar) seekBar).getSeekBarThumb().getBounds().exactCenterX()); //position of a thumb inside the seek bar 

@Sherif elKhatib的答案很棒,但即使在API> = 16时也有缓存副本拇指的缺点。 我已对它进行了改进,以便它只在API < = 15上缓存Thumb Drawable ,并且它会覆盖SeekBar.java的方法,以避免两个方法在API> = 16上执行相同的操作。 唯一的缺点:它需要目标SDK> = 16,现在大多数应用都应该如此。

 public class ThumbSeekBar extends AppCompatSeekBar { private Drawable thumb; public ThumbSeekBar(Context context) { super(context); } public ThumbSeekBar(Context context, AttributeSet attrs) { super(context, attrs); } public ThumbSeekBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public Drawable getThumb() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { return super.getThumb(); } return thumb; } @Override public void setThumb(Drawable thumb) { super.setThumb(thumb); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { this.thumb = thumb; } } }