如果我理解正确,您想在实际滑块上方创建一个数字值,该数字会随着轨道移动而变化。
您可以通过将文本直接“合并”到轨道图像的可绘制文件中,在实际滑块图像上方编写文本。
我假设您正在使用您原始问题中提供的第一个教程。
public class CustomSeekBar extends Activity {
SeekBar mybar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_seek_bar);
mybar = (SeekBar) findViewById(R.id.seekBar1);
mybar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//add here your implementation
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//add here your implementation
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int value = seekBar.getProgress(); //this is the value of the progress bar (1-100)
//value = progress; //this should also work
String valueString = value + ""; //this is the string that will be put above the slider
seekBar.setThumb(writeOnDrawable(R.drawable.thumbler_small, value));
}
});
}
public BitmapDrawable writeOnDrawable(int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.BLACK); //Change this if you want other color of text
paint.setTextSize(20); //Change this if you want bigger/smaller font
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint); //Change the position of the text here
return new BitmapDrawable(bm);
}
}
seekBar.getProgress();
从您的seekbar中获取所需值。
我建议清理代码,因为现在每次触摸seekbar时都会创建一个新的paint对象,这真的很糟糕。
要使其仅在单击orb时起作用,您需要执行更多操作... <SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/seek_bar"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:thumb="@drawable/thumbler_small"
android:indeterminate="false" />
seek_bar.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+android:id/SecondaryProgress"
android:drawable="@drawable/first"/>
<item
android:id="@+android:id/progress"
android:drawable="@drawable/second"/>
</layer-list>
第一个drawable为空或显示不完整的颜色区域。
第二个drawable填充或着色了整个区域。
这个链接可以帮助你更多。
error: Error: No resource found that matches the given name (at 'id' with value '@+android:id/SecondaryProgress')
。 - Maveňツ为什么不编写自定义的进度条视图。
以下是一些示例代码,可以将文本放置在进度条拇指滑块的中间,并且该文本随着滑块移动。应该很容易适应打印滑块上方的文本。
public class CustomSeekBar extends SeekBar {
private Paint textPaint;
private Rect textBounds = new Rect();
private String text = "";
public CustomSeekBar(Context context) {
super(context);
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
// First draw the regular progress bar, then custom draw our text
super.onDraw(canvas);
// Now progress position and convert to text.
text = Integer.toString(getProgress());
// Now get size of seek bar.
float width = getWidth();
float height = getHeight();
// Set text size.
textPaint.setTextSize((height * 3) / 4);
// Get size of text.
textPaint.getTextBounds(text, 0, text.length(), textBounds);
// Calculate where to start printing text.
float position = (width / getMax()) * getProgress();
// Get start and end points of where text will be printed.
float textXStart = position - textBounds.centerX();
float textXEnd = position + textBounds.centerX();
// Check does not start drawing outside seek bar.
if(textXStart < 0) textXStart = 0;
if(textXEnd > width)
textXStart -= (textXEnd - width);
// Calculate y text print position.
float yPosition = (height / 2) - textBounds.centerY(); //- (textBounds.bottom / 2);
canvas.drawText(text, textXStart, yPosition, textPaint);
}
public synchronized void setTextColor(int color) {
super.drawableStateChanged();
textPaint.setColor(color);
drawableStateChanged();
}
我希望你能从中受益。
如果你想让SeekBar的外观与图片中展示的相同,你需要在xml文件中为SeekBar提供minHeight和maxHeight两个属性。
例如:<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/seek_bar"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:thumb="@drawable/thumbler_small"
**android:minHeight = "20dp"
android:maxHeight = "20dp"**
android:indeterminate="false" />
首先在您的drawable文件夹中创建一个XML文件(如果没有,请创建一个新的)
然后粘贴此代码或按照tutorial尽快完成此操作
repeatbottombackground.xml
<bitmap android:dither="true" android:src="@drawable/bottom_bar_repeat" android:tilemode="repeat" xmlns:android="http://schemas.android.com/apk/res/android">
</bitmap>
seek_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/seek_thumb_pressed" android:state_pressed="true" android:state_window_focused="true">
<item android:drawable="@drawable/seek_thumb_pressed" android:state_focused="true" android:state_window_focused="true">
<item android:drawable="@drawable/seek_thumb_pressed" android:state_selected="true" android:state_window_focused="true">
<item android:drawable="@drawable/seek_thumb_normal">
</item></item></item></item></selector>
我给你一个链接,希望它能帮到你。
抱歉我没有更多的解释,只需按照教程操作即可 :)
祝好!