test

class DevCircleView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val sweepAngle = 10f
    private var radius = 0
    private val paint = Paint(ANTI_ALIAS_FLAG)
    private val paintFrame = Paint(ANTI_ALIAS_FLAG)
    private val pointCenter = Point()

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val paddingH = paddingLeft + paddingRight
        val paddingV = paddingTop + paddingBottom
        val width = w - paddingH
        val height = h - paddingV
        radius = width.coerceAtMost(height) / 2
        pointCenter.x = paddingH / 2 + radius
        pointCenter.y = paddingV / 2 + radius
    }

    override fun onDraw(canvas: Canvas) {
        drawCircle(canvas)
        drawArc(canvas)
    }

    private fun drawArc(canvas: Canvas) {
        paintFrame.color = Color.BLACK
        paintFrame.style = Paint.Style.STROKE
        paintFrame.strokeWidth = 6f * Resources.getSystem().displayMetrics.density
        paintFrame.strokeCap = Paint.Cap.ROUND
        pointCenter.x.toFloat()
        canvas.drawArc(
            RectF(
                (pointCenter.x - radius).toFloat(), (pointCenter.y - radius).toFloat(),
                (pointCenter.x + radius).toFloat(), (pointCenter.y + radius).toFloat()
            ),
            -(sweepAngle / 2 + 90f) + offsetAngle, sweepAngle + offsetSweepAngle, false, paintFrame
        )
    }

    private fun drawCircle(canvas: Canvas) {
        paint.color = 0xFFEEEEEE.toInt()
        paint.style = Paint.Style.FILL_AND_STROKE
        paint.strokeWidth = 6f * Resources.getSystem().displayMetrics.density
        canvas.drawCircle(pointCenter.x.toFloat(), pointCenter.y.toFloat(), radius.toFloat(), paint)
    }

    private val animator = ValueAnimator.ofInt(0, 360)

    init {
        initAnimate()
    }

    private var offsetAngle = 0
    private var offsetSweepAngle = 0
    private fun initAnimate() {
        animator.apply {

            duration = 1200
            repeatMode = RESTART
            repeatCount = -1
            interpolator = LinearInterpolator()
            addUpdateListener {
                offsetAngle = it.animatedValue as Int
                if (offsetAngle <= 180) {
                    offsetSweepAngle = offsetAngle / 3
                } else {
                    offsetSweepAngle = (360 - offsetAngle) / 3
                }
                invalidate()
            }
        }
    }

    fun startAnimate() {
        animator.start()
    }

}

你可能感兴趣的:(test)