how can I get the view of the current composable o...
# compose
l
how can I get the view of the current composable only ? I tried LocalView.current but it gets the whole screen.
a
What's the "view*?
l
I mean for example I have to get the android.view.View of only Composable2 in this layout : Column { Composable1() Composable2() Composable3() }
a
Oh, I believe there is no such thing as Android View for a Composable. Thre is root ComposeView, all the hierarchy is rendered there.
s
What are you planning to do with this view? Chances are you're attacking the problem in a different approach than what's common in Compose.
l
If you need a view that contains a Composable, you can use AndroidView to place a view in Compose, and set that view as a ComposeView that holds the composable you want. It's a bit odd to use Compose -> Android View -> Compose, but it gets you a View with the Compose content you need.
l
@sindrenm To make screenshots and get images of views
@Landry Norris I thought about it but I'm in compose multiplatform project
l
You can expect/actual an implementation that uses what I suggested on Android and ImageComposeScene on iOS/desktop. From there, you can add a lambda that provides the screenshot.
r
Composables are not backed by Views (well they can be, but only as an implementation detail depending on the API level and there's no 1:1 relationship anyway)
l
From my understanding, Composables are backed by a canvas. Is there any chance that a future release could give us access to this canvas, or is it intended as an implementation detail that can change?
r
What do you mean get access to the canvas? It's just like with Views, there's no one canvas
(each View has its own Canvas, and in Compose it's not 1:1 between composables and Canvas but it's not a single Canvas either)
Note that a Canvas isn't a backing surface in itself, it's just an interface that receives draw calls. Canvases are backed, on Android, by a Surface (android.view.Surface), a Bitmap, or a RenderNode/Display list
(and render nodes are just things that record drawing commands, later replayed against a Surface)
l
In drawWithCanvas, you can get access to a nativeCanvas, which is a bitmap on Android if I remember correctly. Does this back most built-in composables, such as buttons and text?
r
No a Canvas isn't a Bitmap
t
Maybe this Thread is helpful? It deals with how one can get a Bitmap from a composable. There exists a library for what you're trying to achieve (Just saw that you're using multiplatform) Question is, whether the retargeting feature mentioned in the Thread will also be available in Compose multiplatform 🤔
r
Canvas is just an interface to draw, amongst other things, into a Bitmap
And when you get one via nativeCanvas in Compose you are getting a Canvas that records into a RenderNode (by default)
s
Alternatively, you can use the PixelCopy API, get the region on the screen of your composable function, and PixelCopy will capture everything on that part of the screen and return a bitmap to you. The downside is that it's just a screenshot of the whole window, so it captures everything that's drawn under and over your composable widget. As far as I know, in the alpha version of Compose 1.6.0, you can replace DrawScope with your own, which will then draw into a bitmap. If I'm not mistaken.
r
Yes there's a retargeting API
728 Views