CustomSeekBar.java 5.97 KB
package com.wd.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;

import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatSeekBar;

import com.wd.base.log.Logger;
import com.wd.fastcoding.base.R;

public class CustomSeekBar extends AppCompatSeekBar {
    private Paint mTextPaint;
    private Paint mLinePaint;
    private RectF mRectF;


    private Drawable mThumb;


    private float mMaxShowValue; //需要显示的最大值
    private int mPrecisionMode;//精度模式
    private int mViewWidth;
    private int mCenterX;
    private int mCenterY;
    private int mThumbHeight;
    private int mThumbWith;
    private SongTimeCallBack songTimeCallBack;

    /**
     * 长滑块标识
     */
    boolean longThumbFlag = false;

    public CustomSeekBar(Context context) {
        this(context, null);
    }


    public CustomSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DigitalThumbSeekbar);
        mMaxShowValue = typedArray.getFloat(R.styleable.DigitalThumbSeekbar_maxShowValue, getMax());//获取最大显示值
        mPrecisionMode = typedArray.getInt(R.styleable.DigitalThumbSeekbar_PrecisionMode, 0);//进度模式
        typedArray.recycle();//释放

        //设置滑块样式
        mThumb = context.getResources().getDrawable(R.mipmap.seekbar_select);
        setThumb(mThumb);
        setThumbOffset(0);
        initPaint();//初始化画笔
    }

    private void initPaint() {
        //文字画笔
        mTextPaint = new Paint();
        mTextPaint.setARGB(255, 255, 255, 255);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mThumb.getMinimumHeight() * 5.2f / 9);//文字大小与滑块高度的比 5.5:9
        mTextPaint.setTextAlign(Paint.Align.CENTER); // 设置文本对齐方式,居中对齐

        //进度画笔
        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //父容器传过来的宽度的值
        mViewWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
//        Log.d("aaa",String.valueOf(mViewWidth));
        //根据滑块的尺寸确定大小 布局文件中的android:layout_height="" 任何设置不会改变绘制的大小
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(mThumb.getMinimumHeight(), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //获取滑块坐标
        Rect thumbRect = getThumb().getBounds();
        mCenterX = thumbRect.centerX();//中心X坐标
        mCenterY = thumbRect.centerY(); //中心Y坐标
        mThumbHeight = thumbRect.height();//滑块高度
        mThumbWith = thumbRect.width();//滑块宽度
        //绘制进度条
        drawRect(canvas);
        //绘制进度文字
        drawProgress(canvas);



    }

    /**
     * 绘制进度条
     * @param canvas
     */
    private void drawRect(Canvas canvas) {
        int lineHight = 2;
        //绘制左边的进度
        mRectF = new RectF();
        mRectF.left = 10;
        mRectF.right = mCenterX;
        mRectF.top = mCenterY - lineHight;
        mRectF.bottom = mCenterY + lineHight;
        mLinePaint.setColor(getResources().getColor(R.color.res_color_common_C11));
        canvas.drawRoundRect(mRectF, lineHight, lineHight, mLinePaint);

        //绘制右边剩余的进度
        mRectF.left= mCenterX+mThumbWith/2.0f-7;
        mRectF.right = mViewWidth-7;
        mRectF.top = mCenterY - lineHight;
        mRectF.bottom = mCenterY + lineHight;
        mLinePaint.setARGB(255,0,0,0);
        canvas.drawRoundRect(mRectF, lineHight, lineHight, mLinePaint);
    }


    /**
     * 绘制显示的进度文本
     * @param canvas
     */
    private void drawProgress(Canvas canvas) {
        String progressText = "";//要画的文字
        if (songTimeCallBack != null) {
            //将要画的时间对外提供
            progressText = songTimeCallBack.getDrawText();
        }
        //测量文字高度
        Rect bounds = new Rect();
        mTextPaint.getTextBounds(progressText, 0, progressText.length(), bounds);
        int mTextHeight = bounds.height();//文字高度
//      float mTextWidth = mTextPaint.measureText(progress);
        canvas.drawText(progressText, mCenterX, mCenterY + mTextHeight/2, mTextPaint);
    }
    public interface SongTimeCallBack {
        String getSongTime(int progress);

        String getDrawText();
    }

    public void setSongTimeCallBack(SongTimeCallBack songTimeCallBack) {
        this.songTimeCallBack = songTimeCallBack;
        if(songTimeCallBack != null){
            String drawText = songTimeCallBack.getDrawText();
            int length = drawText.length();
            Logger.t("CustomSeekBar").d("drawTextLength========>"+drawText.length());
            if(!longThumbFlag && length > 13){
                longThumbFlag = true;
                //设置滑块样式(总时长超过1小时,滑块背景使用长的)
                mThumb = getResources().getDrawable(R.mipmap.seekbar_select_long);
                setThumb(mThumb);
                setThumbOffset(0);
                initPaint();//初始化画笔
            }
        }
    }
}