Hi guys is there a way to apply bitmap on the shap...
# compose-desktop
c
Hi guys is there a way to apply bitmap on the shapes we draw on canvas API ?, example if i
drawCircle(...)
how can I put a bitmap image in it so it looks like a circular image view
i
Will this help?
Copy code
val image = imageResource("androidx/compose/desktop/example/circus.jpg")

Canvas(Modifier.fillMaxSize()) {
    drawIntoCanvas { canvas ->
        canvas.withSave {
            canvas.clipPath(Path().apply {
                addOval(Rect(0f, 0f, 300f, 300f))
            })
            canvas.drawImage(image, Offset.Zero, Paint())
        }
    }
}
r
Path clipping can be expensive
It’s much better to instead use a bitmap shader to fill the shape
The shader can be set on the
Paint
object
🤓 1
t
@romainguy Can u give me an updated version of the above example with bitmap shader?
r
val p = Paint().apply {
shader = ImageShader(myImageBitmap)
}
🙏 1
And then when you call
drawCircle(…)
pass your
Paint
p
t
Understood. 👍 Thanks