Android小技巧|记一次偶然发现的进度条View

今天本来要写一个时钟,写到一半,看到刻度,突然发现可以写成进度条,就改了,超级简单。以下是写的代码:

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 = 0constructor(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) } } }

【Android小技巧|记一次偶然发现的进度条View】可以参考,很巧妙。

    推荐阅读