答案 0 :(得分:8)
如果我理解正确,您想要在沿着球体移动的实际滑块上方创建一个数字值。
您可以做的一件事是通过将文本“合并”到orb图像的可绘制文件上,在滑块的实际图像上方写入文本。
我假设您使用的是原始问题中提供的first tutorial
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);
}
}
这需要从您的资源中抽取一些文本,然后返回新的drawable。使用seekBar.getProgress();
获取搜索栏所需的值。
我建议稍微清理一下代码,因为现在每次触摸搜索栏时都会创建一个新的绘图对象,这非常糟糕。
当你点击orb时,你需要做更多的工作才能让它工作 ...
答案 1 :(得分:6)
XML代码
<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是填充或颜色全区。
this链接可以帮到您。
答案 2 :(得分:6)
为什么不写自定义搜索栏视图。
以下是一些示例代码,用于将文本放在搜索栏拇指滑块的中间,文本随滑块移动。应该很容易适应滑块顶部的打印文本。
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();
}
}
我希望这对你有用。
答案 3 :(得分:1)
如果要在图像中显示相同的外观,则必须在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" />
请检查上面代码中突出显示的属性。
答案 4 :(得分:0)
首先在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>
我给你一个link我希望它可以帮助你
对不起,我不需要解释更多,只需按照教程:)
问候,