Skip to content

Instantly share code, notes, and snippets.

@decodeandroid
Last active May 17, 2024 17:30
Show Gist options
  • Select an option

  • Save decodeandroid/21409dd2a654bcd6f804172e42c5a407 to your computer and use it in GitHub Desktop.

Select an option

Save decodeandroid/21409dd2a654bcd6f804172e42c5a407 to your computer and use it in GitHub Desktop.
Learn Android Canvas in Jetpack Compose
@Composable
fun NewAndroidBot() {
//import Canvas
androidx.compose.foundation.Canvas(
modifier = Modifier
.background(Color.White) //set background color of canvas
.fillMaxSize() //canvas will take full screen size
) {
//draw the head
drawArc(
startAngle = 0f,
sweepAngle = -180f, //rotate 180 degrees anticlockwise
useCenter = true,
color = androidColor,
size = Size(width / 2f, width / 2f),
topLeft = Offset(width.times(.25f), height.times(.3f))
)
//left eye
drawCircle(
color = Color.White,
center = Offset(width.times(.35f), height.times(.38f)),
radius = 25f
)
//right eye
drawCircle(
color = Color.White,
center = Offset(width * 0.65f, height * 0.38f),
radius = 25f
)
//left Antenna
rotate(
degrees = -20f,
pivot = Offset(width.times(.4f), height.times(.4f))
) {
drawRoundRect(
color = androidColor,
size = Size(28f, 150f),
cornerRadius = CornerRadius(50f),
topLeft = Offset(
width.times(.4f),
height.times(.26f)
)
)
}
//right Antenna
rotate(
degrees = 20f,
pivot = Offset(width.times(.6f), height.times(.35f))
) {
drawRoundRect(
color = androidColor,
size = Size(28f, 150f),
cornerRadius = CornerRadius(50f),
topLeft = Offset(
width.times(.6f),
height.times(.26f)
)
)
}
//belly
val wavesPath = Path().apply {
moveTo(
x = width * 0.25f,
y = height * 0.415f
)
lineTo(
x = width * 0.75f,
y = height * 0.415f
)
lineTo(
x = width * 0.75f,
y = height * 0.57f
)
quadraticBezierTo(
width * 0.75f, height * 0.62f,
width * 0.7f, height * 0.62f
)
lineTo(
x = width * 0.3f,
y = height * 0.62f
)
quadraticBezierTo(
width * 0.25f, height * 0.62f,
width * 0.25f, height * 0.57f
)
close()
}
drawPath(
path = wavesPath,
color = androidColor,
)
//left hand
drawRoundRect(
color = androidColor,
size = Size(100f, 300f),
cornerRadius = CornerRadius(50f),
topLeft = Offset(
width.times(.14f),
height.times(.43f)
)
)
//right hand
drawRoundRect(
color = androidColor,
size = Size(100f, 300f),
cornerRadius = CornerRadius(50f),
topLeft = Offset(
width * 0.77f,
height * 0.43f
)
//left leg
drawRoundRect(
color = androidColor,
size = Size(100f, 200f),
cornerRadius = CornerRadius(50f),
topLeft = Offset(
width.times(.33f),
height.times(.6f)
)
)
//right leg
drawRoundRect(
color = androidColor,
size = Size(100f, 200f),
cornerRadius = CornerRadius(50f),
topLeft = Offset(width.times(.57f), height.times(.6f))
)
//draw text
with(drawContext.canvas.nativeCanvas) {
val textPaint = TextPaint().apply {
isAntiAlias = false
style = android.graphics.Paint.Style.FILL
textSize = 90f
color = android.graphics.Color.WHITE
}
drawText(
"I'm",
width.times(.44f), height.times(.48f),
textPaint
)
drawText(
"Android",
width.times(.35f), height.times(.54f),
textPaint
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment