在API版本16之前检测SeekBar中的拇指位置 [英] Detecting thumb position in SeekBar prior to API version 16
问题描述
基本上,我需要检测SeekBar中进度何时发生变化,并在拇指上方绘制一个文本视图以指示进度值.
Basically, I need to detect when the progress changes in the SeekBar and draw a text view on top of the thumb indicating the progress value.
我通过实现OnSeekBarChangeListener
来做到这一点
在public void onProgressChanged(SeekBar seekBar, int progress, boolean b)
方法上,我调用Rect thumbRect = seekBar.getThumb().getBounds();
确定拇指的位置.
I do this by implementing a OnSeekBarChangeListener
and on the public void onProgressChanged(SeekBar seekBar, int progress, boolean b)
method, I call Rect thumbRect = seekBar.getThumb().getBounds();
to determine where the thumb is positioned.
这工作得很好,但是显然getThumb()
仅在API级别16+(Android 4.1)中可用,导致在早期版本中出现NoSuchMethodError
.
This works perfectly fine, but apparently getThumb()
is only available in API level 16+ (Android 4.1), causing a NoSuchMethodError
on earlier versions.
任何想法如何解决此问题?
Any idea how to work around this issue?
推荐答案
我能够使用自己的类来获取Thumb:
I was able to use my own class to get the 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;
}
}
在活动中,此操作非常完美:
In the activity this works perfectly:
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
}
}
这篇关于在API版本16之前检测SeekBar中的拇指位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!