I'm struggling to draw a Painter to a Bitmap. The ...
# compose
d
I'm struggling to draw a Painter to a Bitmap. The confusing thing is that the docs for painter say
In addition to providing the ability to draw into a specified bounded area,
Painter
provides a few high level mechanisms that consumers can use to configure how the content is drawn.
However, the entire public api is just the property
intrinsicSize
and the extension function
DrawScope.draw
. Confusingly
draw
doesn't take the painter as an argument, so I'm not sure how it could work. This is as far as I got:
Copy code
val painter = painterResource(R.drawable.marker)
    val size = painter.intrinsicSize
    val image = ImageBitmap(size.width.roundToInt(), size.height.roundToInt())
    val canvas = Canvas(image)
    CanvasDrawScope().draw(LocalDensity.current, LocalLayoutDirection.current, canvas, size) {
    }
n
the 
draw
 method on 
Painter
 is an extension method on 
DrawScope
 so you would do the following in your CanvasDrawScope usage:
Copy code
CanvasDrawScope().draw(LocalDensity.current, LocalLayoutDirection.current, canvas, size) {
        with(painter) {
            draw(size) // using default alpha and no colorfilter tinting
        }
d
Oh,/hits head/, I forgot about with. Sorry to waste your time.
n
No problem. I always end up forgetting using
with
to determine the proper scoping as well.
👍 1