Junior — Middle
Can you give examples of custom interface components you have created as custom Views?
sobes.tech AI
Answer from AI
An example of a custom interface component based on a custom View could be:
- A custom progress indicator with a unique animation and style that is not supported by the standard ProgressBar.
- A widget for displaying a rating in the form of stars with the ability to select and custom rendering.
- A custom circular chart with fill animation, drawn on a Canvas.
An example of a simple custom View that draws a circle with text:
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)
}
}