自定义Android progressbar

先看效果


Dingtalk_20221102150140.jpg

主要有5种颜色:
1.整体背景色
2.progressbar的背景色
3.进度圆点的颜色
4.进度颜色的开始和结束颜色

代码

package com.example.mydemo2

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View

/**
 * @author xiaozhi
 * @CreateDate 2022/3/2 2:23 下午
 * @Description
 */
open class SampleProgressView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : View(context, attrs, defStyleAttr, defStyleRes) {
    private var mHeight = 0
    private var mProgressWidth = 0
    private var mProgressHeight = 0
    private var mMaxProgress = 100f
    private var mCurrentProgress = 0
    private var textMargin = 0
    private val startColor="#1EF1C6"//开始颜色
    private val endColor="#00B79B"//结束的颜色
    //底色背景图
    private var mProgressPaint = Paint().apply {
        color = Color.parseColor("#131313")
        style = Paint.Style.FILL
    }
    private var mCompleteProgressPaint = Paint().apply {
        style = Paint.Style.FILL
        color = Color.parseColor(endColor)
    }
    private var mCirClePaint = Paint()

    private var mTextPaint = Paint().apply {
        color = Color.WHITE
        isAntiAlias = true
        textAlign = Paint.Align.LEFT
    }

    private var helperPaint = Paint().apply {
        color = Color.parseColor(endColor)
    }
    //private lateinit var bitmap: Bitmap
    private val color1 = Color.parseColor(startColor)
    private val color2 = Color.parseColor(endColor)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mTextPaint.textSize = dip2px(10).toFloat()
        //bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_seekbar)
        mProgressWidth = MeasureSpec.getSize(widthMeasureSpec)
        mHeight = dip2px(20)
        mProgressHeight = dip2px(12)
        textMargin = dip2px(5)
        setMeasuredDimension(mProgressWidth, mHeight)
    }

    /**
     * dp 2 px
     *
     * @param dpVal
     */
    private fun dip2px(dpVal: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpVal.toFloat(), resources.displayMetrics
        ).toInt()
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val progress = mCurrentProgress / mMaxProgress
        val spaceH = (mHeight - mProgressHeight) / 2f  //上间距

        val rectProgress = RectF(0f, spaceH, mProgressWidth.toFloat(), mHeight - spaceH)
//底部框
        canvas.drawRoundRect(rectProgress, mHeight / 2f, mHeight / 2f, mProgressPaint)

        val completeLength = (mCurrentProgress / mMaxProgress) * mProgressWidth.toFloat()
        val rectCompleteProgress = RectF(0f, spaceH, completeLength, mHeight - spaceH)

//渐变色进度条
        mCompleteProgressPaint.shader = LinearGradient(
            rectCompleteProgress.left,
            rectCompleteProgress.bottom / 2,
            rectCompleteProgress.right, rectCompleteProgress.bottom / 2,
            color1,
            color2,
            Shader.TileMode.CLAMP
        )
//进度框
        canvas.drawRoundRect(
            rectCompleteProgress,
            mHeight / 2f,
            mHeight / 2f,
            mCompleteProgressPaint
        )
        if(progress<1){
            val rectCompleteProgressHelper =
                RectF(rectCompleteProgress.right-mHeight/2f,
                    rectCompleteProgress.top,
                    rectCompleteProgress.right,
                    rectCompleteProgress.bottom)
           
            canvas.drawRect(
                rectCompleteProgressHelper,
                helperPaint
            )
        }

        val changeRange = mProgressWidth  //圆心游标的移动长度范围
        val circleLength = (mCurrentProgress / mMaxProgress) * changeRange.toFloat() + mProgressHeight/2
        //canvas.drawBitmap(bitmap,null, Rect((circleLength - mHeight / 2).toInt(), 0,(circleLength + mHeight / 2).toInt(),mHeight),mCirClePaint)
        mCirClePaint.color = -0x1//白色圆点
        canvas.drawCircle(circleLength, mHeight/2.toFloat(), mProgressHeight/2.toFloat(), mCirClePaint)

    }


    fun setCurrentProgress(progress: Int) {
        mCurrentProgress = progress
        postInvalidate()
    }

    fun setMaxProgress(maxProgress: Float) {
        mMaxProgress = maxProgress
    }
}

调用


你可能感兴趣的:(自定义Android progressbar)