package com.led.scroller.barrage.banner.ui.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

class CircleView : View {
    private var circleColor: Int = Color.BLACK
    private val paint: Paint = Paint()

    constructor(context: Context) : super(context) {
        init(null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet?) {
        // 初始化画笔
        paint.isAntiAlias = true // 设置抗锯齿
        paint.style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val centerX = width / 2.toFloat()
        val centerY = height / 2.toFloat()
        val radius = (width.coerceAtMost(height) / 2).toFloat()

        // 设置圆的颜色
        paint.color = circleColor

        // 绘制圆形
        canvas.drawCircle(centerX, centerY, radius, paint)
    }

    fun setCircleColor(color: Int) {
        circleColor = color
        invalidate() // 通知 View 进行重绘
    }

    fun setCircleColor(color: String) {
        circleColor = Color.parseColor(color)
        invalidate() // 通知 View 进行重绘
    }
}

