Truck Art — Android Jetpack Compose Canvas

Jetpack Compose Graphics

Canvas(
modifier = Modifier.fillMaxWidth().height(300.dp),
onDraw = {
//draw here
}
)
Canvas(Modifier.fillMaxWidth().height(300.dp)) {
//draw here
}

1. Draw Line

drawLine(
color = Color.Black,
start = Offset(0f, center.y),
end = Offset(size.width,center.y),
strokeWidth = 5f
)

Output

drawLine(
color = Color.Black,
start = Offset(0f, center.y),
end = Offset(size.width,center.y),
pathEffect = PathEffect.dashPathEffect(
floatArrayOf(10f, 20f),
phase = 25f
),
strokeWidth = 5f
)

2. Draw Circle

drawCircle(
color = Color.Black,
radius = 100f
)

Output

drawCircle(
color = Color.Black,
radius = 100f,
style = Stroke(width = 5f)
)

3. Draw Rectangle

val size = 200f
drawRect(
color = Color.Black,
topLeft = Offset(center.x - size / 2, center.y - size / 2),
size = Size(size, size)
)

4. Draw Diamond Shape

fun DrawScope.createPath(): Path {
val path = Path()
val angle = 2.0 * Math.PI / 4
val radius = 100
path.moveTo(
x = size.width/2 + (radius * cos(0.0)).toFloat(),
y = center.y + (radius * sin(0.0)).toFloat()
)
for (i in 1 until 4) {
path.lineTo(
x = size.width/2 + (radius * cos(angle * i)).toFloat(),
y = center.y + (radius * sin(angle * i)).toFloat()
)
}
path.close()

return path
}
drawPath(
path = createPath(),
color = Color.Black
)

Output

Android Engineer @Callsign — I write about new things in Android.