TextThumbSeekBar1.java 4.09 KB
package com.sw.laryngoscope.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.SeekBar;

import com.sw.laryngoscope.R;

@SuppressLint("AppCompatCustomView")
public class TextThumbSeekBar1 extends SeekBar {

    // 画笔
    private Paint mPaint;
    // 进度文字位置信息
    private Rect mProgressTextRect = new Rect();
    // 滑块按钮宽度
    private int mThumbWidth = dp2px(20);

    private int mSeekBarMin;
    public TextThumbSeekBar1(Context context) {
        this(context, null);
    }

    public TextThumbSeekBar1(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.seekBarStyle);
        initData();
    }

    private void initData() {
        mPaint = new TextPaint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(getResources().getColor(R.color.color_0aede0));
        mPaint.setTextSize(sp2px(18));

        // 如果不设置padding,当滑动到最左边或最右边时,滑块会显示不全
        setPadding(mThumbWidth / 2, 0, mThumbWidth / 2, 0);
    }

    public TextThumbSeekBar1(Context context, AttributeSet attrs, int defStyleAttr){
        super(context,attrs,defStyleAttr);
        initData();
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String txt = "100%";
        mPaint.getTextBounds(txt, 0, txt.length(), mProgressTextRect);
        canvas.drawText(txt, getWidth() - mProgressTextRect.width(), getHeight() - mProgressTextRect.height() + 10, mPaint);

        // 进度文字位置信息
        String progressText = getProgress() + "%";
        mPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);

        // 进度百分比
        float progressRatio = (float) getProgress() / getMax();
        // thumb偏移量
        float thumbOffset = (mThumbWidth - mProgressTextRect.width()) / 2 - mThumbWidth * progressRatio;
        float thumbX = getWidth() * progressRatio + thumbOffset;
        //这里修改文字的范围,上或下
        float thumbY = getHeight() / 2f - mProgressTextRect.height() / 2f - dp2px(10);
        int thumbPos = getMeasuredWidth() * getProgress()/getMax() - getThumbOffset();
        canvas.drawText(progressText, thumbPos, thumbY, mPaint);
        /*String progressText = getProgress() + "%";
        mPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);

        //The height of the displacement area of our bar (the height - the padding - the thumb size)
        int bottomPadding = getPaddingLeft() - getThumbOffset();
        int topPadding = getPaddingRight() - getThumbOffset();
        int height = getHeight() - bottomPadding - topPadding;

        float progressRatio = (float) getProgress() / getMax();
        float thumbOffset = getThumb().getIntrinsicWidth() * (.5f - progressRatio);

        // In our rotated canvas, positive x is higher, positive Y is more to the left
        float thumbX = progressRatio * height + bottomPadding + thumbOffset;
        float thumbY = getWidth() / 2f;
        //Since we rotate the canvas again when drawing the text, we need to translate the coords for the text
        float textX = thumbY - mProgressTextRect.width() / 2f;
        float textY = -( thumbX - mProgressTextRect.height()/2f );
        canvas.drawText(progressText, textY, textX, mPaint);*/
    }

    public void setMix(int min){
        mSeekBarMin=min;
    }

    /**
     * dp转px
     *
     * @param dp dp值
     * @return px值
     */
    public int dp2px(float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    /**
     * sp转px
     *
     * @param sp sp值
     * @return px值
     */
    private int sp2px(float sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                getResources().getDisplayMetrics());
    }
}