Remember an accidental progress bar View

Today I was going to write a clock, halfway through, and when I saw the scale, I suddenly realized that it could be written as a progress bar, so I changed it. It's super simple. The following is the code written:

class LoadingView : View {
    private val mPaint = Paint()
    var colors = arrayOf(
        Color.rgb(0x00, 0xFF, 0x7F), Color.rgb(0x00, 0xFF, 0xCC), Color.rgb(0x00, 0xFF, 0xAA)
        , Color.rgb(0x00, 0xF5, 0xFF), Color.rgb(0x00, 0xC5, 0xCD), Color.rgb(0x00, 0xB2, 0xEE)
    )
    var curColor = 0

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attr: AttributeSet?) : this(context, attr, 0)
    constructor(context: Context, attr: AttributeSet?, style: Int) : super(context, attr, style)

    init {
        mPaint.strokeWidth = 20f
        mPaint.strokeCap = BUTT
        mPaint.textSize = 50f
        mPaint.style = Paint.Style.FILL_AND_STROKE
        Thread {
            kotlin.run {
                while (true) {
                    if (curColor > 1) curColor -= 1
                    invalidate()
                    SystemClock.sleep(100)
                }
            }
        }.start()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.translate(width / 2.toFloat(), height / 2.toFloat())
        for (i in 0..359) {
            if (i % 30 == 0) {
                mPaint.color = colors[curColor % 6]
                canvas?.drawLine(20.toFloat(), 50.toFloat(), 25.toFloat(), 50.toFloat(), mPaint)
                curColor += 1
            }
            canvas?.rotate(1f)
        }
    }
}

Can refer to it, very clever.

Guess you like

Origin blog.csdn.net/markiyang/article/details/112985456