我想让客户搜索栏像这样的图像:
5 回答
如果我理解正确,您想在沿球体移动的实际滑块上方创建一个数值。
您可以做的一件事是通过将文本直接“合并”到球体图像的可绘制文件上,在滑块的实际图像上方写入文本。
我假设您正在使用您在原始问题中提供的第一个教程
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();
从搜索栏获取所需的值。
我建议稍微清理一下代码,因为现在每次触摸搜索栏时它都会创建一个新的绘制对象,这真的很糟糕。
您需要做更多的事情才能使其仅在单击球体时才起作用...
为什么不编写自定义搜索栏视图。
这是一些示例代码,用于将文本放在搜索栏拇指滑块的中间,并且文本随滑块移动。应该很容易适应滑块顶部的打印文本。
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();
}
}
我希望这对你有用。
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是填充或着色整个区域。
此链接对您有更多帮助。
如果您想要与图像中显示的外观相同,则必须在 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" />
请检查上面突出显示的代码中的属性。
您好,首先在您的可绘制文件夹中创建一个 xml 文件(如果没有创建一个新文件夹)
并粘贴此代码或按照教程尽快完成此操作
重复底部背景.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>
我给你一个链接我希望它可以帮助你
对不起,我不必解释更多,只需按照教程进行操作即可:)
问候,