Sobes.tech
Junior — Middle

Pouvez-vous donner des exemples de composants d'interface que vous avez créés sous forme de vues personnalisées?

sobes.tech IA

Réponse de l'IA

Примером собственного компонента интерфейса на базе кастомного View может быть:

  • Кастомный индикатор прогресса с уникальной анимацией и стилем, который не поддерживается стандартным ProgressBar.
  • Виджет для отображения рейтинга в виде звездочек с возможностью выбора и кастомной отрисовкой.
  • Кастомный круговой график с анимацией заполнения, где рисуется на Canvas.

Пример простого кастомного View, рисующего круг с текстом:

class CircleTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.BLUE
        style = Paint.Style.FILL
    }
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE
        textSize = 50f
        textAlign = Paint.Align.CENTER
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val radius = width.coerceAtMost(height) / 2f
        canvas.drawCircle(width / 2f, height / 2f, radius, paint)
        canvas.drawText("Hi", width / 2f, height / 2f + textPaint.textSize / 3, textPaint)
    }
}