繁体   English   中英

SeekBar Thumb位置问题

[英]SeekBar Thumb position issue

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

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

截图

我现在不关心任何文本问题(不写一个,门廊等)。

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

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

问候。

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

结果:

结果

将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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM