自定义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;
        }
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迪霸LZTXDY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值