自定义SeekBar



```kotlin
package com.allynav.rtk.farm.view.ui

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.text.TextPaint
import android.util.AttributeSet
import android.util.TypedValue


class TextSeekBar : androidx.appcompat.widget.AppCompatSeekBar {
    constructor(context: Context?) : this(context, null) {
        initData()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
        initData()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {
        initData()
    }

    var isShow = false

    // 画笔
    private var mPaint: Paint? = null

    // 进度文字位置信息
    private val mProgressTextRect: Rect = Rect()

    // 滑块按钮宽度
    private val mThumbWidth = dp2px(50f)

    private var mSeekBarMin = 0
//    fun TextThumbSeekBar(context: Context?) {
//        this(context, null)
//    }

//    fun TextThumbSeekBar(context: Context?, attrs: AttributeSet?) {
//        this(context, attrs, R.attr.seekBarStyle)
//        initData()
//    }

    private fun initData() {
        mPaint = TextPaint()
        (mPaint as TextPaint).isAntiAlias = true
        (mPaint as TextPaint).color = Color.WHITE // 画笔颜色
        (mPaint as TextPaint).textSize = sp2px(16f)

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

//    fun TextThumbSeekBar(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) {
//        super(context, attrs, defStyleAttr)
//    }

    @Synchronized
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        // 进度文字位置信息
//        val progressText = "+$progress"
        val progressText = formatLongToTimeStr(progress.toLong())

        mPaint?.getTextBounds(progressText, 0, progressText.length, mProgressTextRect)

        // 进度百分比
        val progressRatio = progress.toFloat() / max
        // thumb偏移量
        val thumbOffset: Float =
            (mThumbWidth - mProgressTextRect.width()) / 2 - mThumbWidth * progressRatio
        val thumbX = width * progressRatio + thumbOffset
        //这里修改文字的范围,上或下
//        val thumbY: Float = height / 2f - mProgressTextRect.height() / 2f - dp2px(8f)
        val thumbY: Float = height / 2f - mProgressTextRect.height() / 2f
        mPaint?.let { canvas.drawText(progressText, thumbX, thumbY, it) }
        if (isShow) {
            invalidate()
            isShow = false
        }
    }

    fun setMix(min: Int) {
        mSeekBarMin = min
    }

    /**
     * dp转px
     *
     * @param dp dp值
     * @return px值
     */
    private fun dp2px(dp: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dp,
            resources.displayMetrics
        ).toInt()
    }

    /**
     * sp转px
     *
     * @param sp sp值
     * @return px值
     */
    private fun sp2px(sp: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP, sp,
            resources.displayMetrics
        ).toInt().toFloat()
    }

    /**
     * 时间转换
     */
    fun turnTime(timeNum: Long) {
    }

    /**
     * 时间转换&&毫秒数转时分秒
     * @param l Long
     * @return String
     */
    private fun formatLongToTimeStr(l: Long): String {
        var hour = 0;
        var minute = 0;
        var second = 0;
        second = l.toInt() / 1000;
        if (second > 60) {
            minute = second / 60;
            second %= 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute %= 60;
        }
        return (getTwoLength(hour) + ":" + getTwoLength(minute) + ":" + getTwoLength(second));
    }

    /**
     * 时间不够时拼接0
     * @param data Int
     * @return String
     */
    private fun getTwoLength(data: Int): String {
        if (data < 10) {
            return "0$data";
        } else {
            return "" + data;
        }
    }


}


你可能感兴趣的:(安卓,安卓,kotlin)