Build: Custom Drawing Canvas
This screen shows a custom drawing using Canvas. It draws a blue circle and a red rectangle on a white background.
Target UI
--------------------- | | | ( ) | | | | [ ] | | | | | ---------------------
--------------------- | | | ( ) | | | | [ ] | | | | | ---------------------
package com.example.customdrawing 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 CustomDrawingView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : View(context, attrs) { private val paint = Paint() override fun onDraw(canvas: Canvas) { super.onDraw(canvas) // TODO: Add your drawing code here } }
package com.example.customdrawing 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 CustomDrawingView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : View(context, attrs) { private val paint = Paint().apply { isAntiAlias = true } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) // Set background color to white canvas.drawColor(Color.WHITE) // Draw blue circle paint.color = Color.BLUE val circleRadius = width.coerceAtMost(height) / 6f val circleX = width / 2f val circleY = circleRadius + 20f canvas.drawCircle(circleX, circleY, circleRadius, paint) // Draw red rectangle below the circle paint.color = Color.RED val rectLeft = width / 4f val rectTop = circleY + circleRadius + 20f val rectRight = width * 3 / 4f val rectBottom = rectTop + circleRadius canvas.drawRect(rectLeft, rectTop, rectRight, rectBottom, paint) } }
We override onDraw to do custom drawing on the canvas.
First, we fill the background with white using canvas.drawColor(Color.WHITE).
Then, we draw a blue circle centered horizontally near the top. We calculate the radius as one-sixth of the smaller dimension and position it with some padding.
Below the circle, we draw a red rectangle spanning the middle half of the width and with height equal to the circle's radius, positioned with some spacing.
We use a single Paint object and change its color before each shape.
--------------------- | | | ( ) | | | | [ ] | | | | | ---------------------
onDraw() method in Android custom views?onDraw()onDraw() method is called when the view needs to display its content.onDraw() usesonDraw() draws on Canvas [OK]onDraw() is for drawing only [OK]onDraw() with event handlersonDraw() manages layoutonDraw() loads images automaticallyapply allows setting color and style properly.apply to set Paint properties [OK]apply block to set Paint properties cleanly [OK]onDraw() code snippet, what shape will be drawn?
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val paint = Paint().apply { color = Color.BLUE; style = Paint.Style.FILL }
canvas.drawCircle(100f, 100f, 50f, paint)
}drawCircle draws a circle at (100f, 100f) with radius 50f.drawCircle + FILL + blue = filled blue circle [OK]onDraw() method:
override fun onDraw(canvas: Canvas) {
val paint = Paint()
paint.color = Color.GREEN
canvas.drawRect(50f, 50f, 150f, 150f)drawRect method requires a Paint object as the last parameter to know how to draw.drawRect with only coordinates, missing the Paint parameter.drawRect needs Paint argument [OK]onDraw() implementations achieves this correctly?Pass the test and this pattern moves unknown