SeekBar滑块位置问题

5
我将尝试为我的SeekBar创建一个“带标签”的拇指。 目标是每次SeekBar位置更改时自定义文本在拇指上方。
我正在做以下操作:
        ...
        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);
    }

运行后,触摸条后,我得到了这个: screenshot 我现在不关心任何文本问题(不写一个、porition等)。
我关心拇指位置相对于条形进度的位置。
拇指位置应该在绿色条结束的地方。我错过了什么吗?
问候。

很棒的Github库 https://github.com/techery/progresshint - Maor Hadad
2个回答

16

我最终扩展了SeekBar,并覆盖了onMeasure()和onDraw() 方法,这是我发现的唯一使标签/滑块工作的方法。

发布它以帮助他人:

...
    @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);
        }
    }

结果:

result


2
其他变量声明在哪里?你能发完整的解决方案吗? - Pankaj Kumar
@Cheborra:我无法设置数字和圆形图像。你能给我指点一下吗?或者你可以贴出完整的代码吗?提前感谢你的帮助。 - Mahaveer Muttha
我建议您在问题中发布您的代码,这样我就可以看到并更好地帮助您。 - Cheborra
使用此链接获取变量声明:http://stackoverflow.com/questions/18574271/create-custom-seekbar-in-android/21373650#21373650 - Thushara
labelText变量在哪里定义的? 另外,如何使用上述代码? - Shrikant

0
将int进度传递到设置drawable到seekBar的方法中,并使用setProgress()方法:
private void setSeekBarLabel(String text, int progress)
{
    BitmapDrawable thumb = Utils.writeOnBitmap(thumbBmp, text, 0, 0, thumbLablePaint);
    seekBar.setThumb(thumb);
    seekBar.setProgress(progress);
}

并将第二个参数添加到调用方法中:

setSeekBarLabel(String.valueOf(progress), progress);

不好意思,加上setProgress()后我得到了相同的结果。 - Cheborra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接