搜索栏拇指位置的问题 [英] SeekBar Thumb position issue
问题描述
我试图做一个标记为拇指我的搜索栏。 我们的目标是每一次搜索栏位置的变化来定制拇指上面的文本。
I'm trying to do a "labeled" thumb for my Seekbar. The objective is to customize a text above the thumb every time the Seekbar position changes.
我在做这样的:
...
seekBar = (SeekBar) findViewById(R.id.bet_seek_bar);
seekBar.setMax(10);
setSeekBarLabel("0");
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
setSeekBarLabel(String.valueOf(progress));
}
});
}
private void setSeekBarLabel(String text)
{
BitmapDrawable thumb = Utils.writeOnBitmap(thumbBmp, text, 0, 0, thumbLablePaint);
seekBar.setThumb(thumb);
}
它运行后,并触摸了吧,我得到这样的:
After running it, and touch the bar, I'm getting this:
我不关心,现在任何文字的问题(不写的,porition,等等)。
I DON'T CARE right now about any text issue (not writing one, porition, etc).
我真的关心相对栏进度拇指位置。
I DO CARE about thumb position relative to the bar progress.
大拇指的位置应该是一个绿色的酒吧结束。我在想什么?
The thumb position should be where the green bar ends. What am I missing?
问候。
推荐答案
我已经结束了,从搜索栏延伸和覆盖onMeasure()和OnDraw的方法(),这是唯一的方法我发现这样的标签/拇指事情的工作。
I've ended up extending from SeekBar, and overriding the onMeasure() and onDraw methods(), that was the only way I've found to so the label/thumb thing work.
张贴到帮助他人:
...
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (labelBackground != null)
{
viewWidth = getMeasuredWidth();
barHeight = getMeasuredHeight();// returns only the bar height (without the label);
setMeasuredDimension(viewWidth, barHeight + labelBackground.getHeight());
}
}
@Override
protected synchronized void onDraw(Canvas canvas)
{
if (labelBackground != null)
{
barBounds.left = getPaddingLeft();
barBounds.top = labelBackground.getHeight() + getPaddingTop();
barBounds.right = barBounds.left + viewWidth - getPaddingRight() - getPaddingLeft();
barBounds.bottom = barBounds.top + barHeight - getPaddingBottom() - getPaddingTop();
progressPosX = barBounds.left + ((float) this.getProgress() / (float) this.getMax()) * barBounds.width();
labelPos.x = (int) progressPosX - labelOffset;
labelPos.y = getPaddingTop();
progressDrawable = getProgressDrawable();
progressDrawable.setBounds(barBounds.left, barBounds.top, barBounds.right, barBounds.bottom);
progressDrawable.draw(canvas);
labelTextPaint.getTextBounds(labelText, 0, labelText.length(), labelTextRect);
canvas.drawBitmap(labelBackground, labelPos.x, labelPos.y, labelBackgroundPaint);
canvas.drawText(labelText, labelPos.x + labelBackground.getWidth() / 2 - labelTextRect.width() / 2, labelPos.y + labelBackground.getHeight() / 2 + labelTextRect.height() / 2, labelTextPaint);
thumbX = (int) progressPosX - getThumbOffset();
thumbDrawable.setBounds(thumbX, barBounds.top, thumbX + thumbDrawable.getIntrinsicWidth(), barBounds.top + thumbDrawable.getIntrinsicHeight());
thumbDrawable.draw(canvas);
} else
{
super.onDraw(canvas);
}
}
结果:
这篇关于搜索栏拇指位置的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!