1

In Compose we have the method Canvas() to draw lines etc.

Is there a similar feature in Glance? If not, what would be the best way to replicate the Canvas functionality?

mars8
  • 770
  • 1
  • 11
  • 25
  • 1
    `RemoteViews` -- which is what Glance's compositions turn into -- does not support arbitrary widgets, nor directly passing images, drawing paths, the platform `Canvas` type, etc. You could try using regular Compose UI to render your desired image (via `Canvas()`) to a `Bitmap`, then use [`ImageProvider(Bitmap)`](https://developer.android.com/reference/kotlin/androidx/glance/package-summary#ImageProvider(android.graphics.Bitmap)) and `Image()` to display it in a Glance composable. – CommonsWare Oct 29 '22 at 20:48
  • @CommonsWare thanks, this is something that I have looked at before however it seems unusually hard to convert a [composable to a bitmap](https://proandroiddev.com/create-bitmaps-from-jetpack-composables-bdb2c95db51). Are you able to provide a simple example? This should suffice as a valid answer under the 'alternative' route. – mars8 Oct 29 '22 at 21:03
  • 1
    "it seems unusually hard to convert a composable to a bitmap" -- there are libraries for that, such as [this one](https://github.com/PatilShreyas/Capturable), [this one](https://github.com/KaustubhPatange/kapture), and [this one](https://github.com/SmartToolFactory/Compose-Screenshot). "Are you able to provide a simple example?" -- nope. I peeked at the Glance API and saw that they had `ImageProvider(Bitmap)`, which unlocked this approach, but I haven't tried it. If you get it working, post an answer to your question! – CommonsWare Oct 29 '22 at 21:28
  • @CommonsWare I am still looking into this approach. have asked another [question](https://stackoverflow.com/q/74252634/15597975) as an offshoot if you care to take a look. – mars8 Oct 30 '22 at 12:34
  • @CommonsWare for further information, I would like to use existing composables I have created in my app for the widgets too (remote views). when the user clicks on the widget, I am starting a worker, which needs to create the bitmap from the composable. These composables use `canvas()` and so cannot be created directly in glance. All approaches I have come across require starting an activity to do the "composable to bitmap" function. – mars8 Oct 30 '22 at 13:21
  • @mars8 where you able to draw the canvas in glance widget? – YLS Jan 03 '23 at 18:38

0 Answers0