拇指上的垂直搜索栏绘制进度

问题描述 投票:0回答:1

我制作了垂直搜寻杆。现在,我想在seekbar拇指上显示进度。我成功实现了此功能,但现在我的文本显示在seekbar拇指下方。我想在seekbar拇指上绘制文本如何实现此目的?我也添加我的代码

public class TextThumbSeekBar extends SeekBar {
    public TextThumbSeekBar(Context context) {
        super(context);
    }
    private int mThumbSize;
    private TextPaint mTextPaint;
    public TextThumbSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mThumbSize = 50;

        mTextPaint = new TextPaint();
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(50);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    public TextThumbSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        mThumbSize = 50;

        mTextPaint = new TextPaint();
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(50);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    public synchronized void setProgress(int progress)  // it is necessary for calling setProgress on click of a button
    {
        super.setProgress(progress);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
    }
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        String progressText = String.valueOf(getProgress());
        c.drawText(progressText, getThumb().getBounds().centerY(), getHeight() - getThumb().getBounds().centerX(), mTextPaint);
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                super.onTouchEvent(event);
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}

这里是我在xml中使用此类的方式

<demo.smart.com.smartchargeboxdemoapp.TextThumbSeekBar
            android:max="100"
            android:thumb="@drawable/shape_seek_bar_text_thumb"
            android:layout_gravity="center"
            android:id="@+id/seekbar"
            android:layout_width="30dp"
            android:layout_height="match_parent" />

这是我得到的结果enter image description here

android android-canvas seekbar
1个回答
0
投票

尽管问题很旧,但在这种情况下,我还是将thumbTint透明化了,在drawText之前我做了'drawRect'来绘制不会旋转的拇指。希望我能帮上忙。

© www.soinside.com 2019 - 2024. All rights reserved.