Is it possible to access the size of a drawing env...
# compose
j
Is it possible to access the size of a drawing environment? In my specific use case I'm applying the
graphicsLayer
modifier to an
Image
. I need the size of the image respective to content scale in order to implement some customized gestures. I've tried
onSizeChanged
and
onGloballyPositioned
but those seem to be used for the layouts rather than graphics so it isn't called when transformations are made through
graphicsLayer
i
you could try
BoxWithConstraints
👀 1
z
Graphics layer transformations are included in the position and size calculations of a layout node, including all the calculations for pointer input, so if you have the modifier order set up correctly then what you're trying to do should work. Can you share your code?
j
Was using gestures to scale and rotate this image but so far it looks like
onSizeChanged
and
onGloballyPositioned
are only called initially. When scaling, rotating or offsetting the image it's not calling them.
Copy code
Image(
            modifier = Modifier
                .fillMaxSize()
                .onSizeChanged {
                    Log.v("onSizeChanged", it.toString())
                }
                .onGloballyPositioned {
                    Log.v("onGloballyPositioned", "Parent: " + it.boundsInParent())
                    Log.v("onGloballyPositioned", "Window: " + it.boundsInWindow())
                    Log.v("onGloballyPositioned", "Root: " + it.boundsInRoot())
                }
                .graphicsLayer {
                    scaleX = scale
                    scaleY = scale
                    rotationZ = rotation
                    translationX = offsetX
                    translationY = offsetY
                },
            painter = rememberImagePainter(photoUrl),
            contentDescription = stringResource(id = R.string.close_image)
        )
I could use the initial value and scale to compute the size. One issue though is the sizes are not of the painted image but of layout itself so it doesn't actually match the dimensions of the image.
z
Oh, so the values returned by LocalCoordinates should take the layer into account but the callback doesn't happen. I wonder if
onPlaced
will get invoked for those changes?
j
I tried that too but didn't seem to get called. It does get called when I use scale and offset modifiers instead but those affect the layout and don't give me the size of the painted image. 😢
z
j
Much appreciated! This will help with future photo galleries built with compose!