package com.base.scanqrclear.luma

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.base.scanqrclear.R

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

    private val paint = Paint().apply {
        isAntiAlias = true // 抗锯齿
        color = context.getColor(R.color.color_fedbbb)
        style = Paint.Style.STROKE
        strokeWidth = dpToPx(1).toFloat() // 设置边框宽度为1dp
    }

    private var circleRadius: Float = 0f

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        // 绘制圆
        circleRadius = (Math.min(width, height) - paint.strokeWidth) / 2
        canvas.drawCircle(width / 2f, height / 2f, circleRadius, paint)
    }

    fun setStrokeWidth(width: Int) {
        paint.strokeWidth = dpToPx(width).toFloat()
        invalidate() // 重绘View
    }

    // 将dp转换为px
    private fun dpToPx(dp: Int): Int {
        val density = resources.displayMetrics.density
        return (dp * density).toInt()
    }
}