【问题标题】:SeekBar Thumb position issueSeekBar 拇指位置问题
【发布时间】:2012-12-02 23:29:03
【问题描述】:

我正在尝试为我的 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);
    }

运行后,触摸栏,我得到了这个:

我现在不关心任何文本问题(不写一个、部分等)。

我关心拇指相对于进度条的位置。

拇指位置应该是绿条结束的地方。我错过了什么?

问候。

【问题讨论】:

标签: android user-interface seekbar


【解决方案1】:

我最终从 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);
        }
    }

结果:

【讨论】:

  • 其他变量声明在哪里...你能发布完整的解决方案吗?
  • @Cheborra:我无法在上面设置数字和圆形图像。你能指导我吗?或者你能发布完整的代码吗?提前感谢您的帮助。
  • 我建议您将代码发布在问题中,以便我可以查看并更好地帮助您。
  • 使用此链接获取变量声明stackoverflow.com/questions/18574271/…
  • labelText 变量在哪里定义?还有如何使用上面的代码?
【解决方案2】:

将 int progress 传递给将 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() 时得到了相同的结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
相关资源
最近更新 更多