1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class CircleView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.RED style = Paint.Style.FILL } private var centerX = 0f private var centerY = 0f private var radius = 0f override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) centerX = w / 2f centerY = h / 2f radius = minOf(w, h) / 2f - 20f.dp.toPx() } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) canvas.drawCircle(centerX, centerY, radius, paint) } }
|