如何制作带有进度文本作为缩略图的垂直搜索栏

How to make vertical seekbar with progress text as thumb

我想创建一个垂直搜索栏,在拇指附近显示进度。

我通过覆盖 Seekbar class 并旋转 canvas 创建了垂直搜索栏。但是我不知道怎么让文字靠近拇指

 @Override
protected final void onDraw(@NonNull final Canvas c) {
    if (null == mPaint) {
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(50);
    }
    c.rotate(ROTATION_ANGLE);
    c.translate(-getHeight(), 0);
    super.onDraw(c);
    Rect rect = getThumb().getBounds();
    c.drawText(getProgress()+"%", rect.exactCenterX(), rect.exactCenterY(), mPaint);
}

所以问题是如果我像这样绘制文本,文本也会旋转。 那么如何解决这个问题?

与此同时,我尝试了一些自定义视图实现,但我对此一窍不通。

更新 Khemraj 的回答

实际上改变拇指是 Khemraj 所说的简单技巧。但问题是当 旋转 canvas 时,它的所有内容都会旋转(简单逻辑)。更新拇指的时候也会反映这个问题。所以简单的事情就是做一个旋转的 CustomTextView.

Method to make thumb

 public Drawable getThumb(int progress) {
    int width = getWidth();

    ((TextView) mThumbView.findViewById(R.id.tvProgress)).setText(String.format(Locale.getDefault(), "%d%%", progress));
    mThumbView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(mThumbView.getMeasuredWidth(), mThumbView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    mThumbView.layout(0, 0, mThumbView.getMeasuredWidth(), mThumbView.getMeasuredHeight());
    mThumbView.draw(canvas);
    return new BitmapDrawable(getResources(), bitmap);
}

The layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">

<ImageView
    android:layout_marginTop="30dp"
    android:id="@+id/imageView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/seek_thumb_gray"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.samsung.lighting.presentation.ui.custom_views.RotatedTextView
    android:id="@+id/tvProgress"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:gravity="center"
    android:text="20%"
    android:textColor="#000000"
    android:textSize="12sp"
    app:angle="90"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView6" />
</android.support.constraint.ConstraintLayout>

The RotatedTextView

public class RotatedTextView extends AppCompatTextView {
private int mRotationAngle = 90;


@Override
protected void onDraw(Canvas canvas) {
    if (mRotationAngle == 90) {
        canvas.rotate(mRotationAngle);
        canvas.translate(0, -getWidth());
    } else if (mRotationAngle == -90) {
        canvas.rotate(mRotationAngle);
        canvas.translate(-getHeight(), 0);
    }
    super.onDraw(canvas);
} 
}

所以首先我们将文本旋转到 90 或 -90 度,并将拇指设置为 VerticalSeekBar,然后在垂直搜索栏中将 canvas 旋转到 90 或 -90 度。所以最后我们会得到实际的结果

我在这里发布了工作示例。

谢谢,Khemraj