What would be the compose way of grabbing these wi...
# compose-android
c
What would be the compose way of grabbing these window values?
Copy code
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int widthDp = (int) (metrics.widthPixels / metrics.density);
int heightDp = (int) (metrics.heightPixels / metrics.density);
Trying to convert the following to a compose app https://github.com/googlemaps/android-maps-utils/blob/main/demo/src/main/java/com/google/maps/android/utils/demo/VisibleClusteringDemoActivity.java
Would I maybe do this?
Copy code
val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp
val screenWidth = configuration.screenWidthDp
or should I use boxWithConstraints?
For more color. I'm trying to follow the advice suggested here: https://github.com/googlemaps/android-maps-utils/issues/1199#issuecomment-1858446063
r
Don’t ask for the default display metrics
It won’t work in multi-display environments
Instead go through context->resources->displayMetrics
1
or even better, don’t rely on screen dimensions but only layout dimensions
👍 1
1
c
so BoxWithConstraints would be able to give me those "layout" dimens, right?
r
A simple custom layout would do
c
hm. lemme see if i can whip something up. i haven't written many custom layouts.
in my current case, I'm loading a google map view in an AndroidView. Does AndroidView allow me to quickly grab width and height in dp?
r
What width/height and when?
c
Just whatever satifies this method lol
I think screenWidth and screenHeight are probably bad names. Like you said, this should probably be mapWidth and mathHeight
r
Right. In the View world, the way to get this information is by checking the View’s width/height after layout (either via a custom View, or using layout listeners for instance). In Compose you can use
Modifier.onGloballyPositioned
c
Cool. this seems to work for now. I suppose I might try a custom layout later, but hopefully this fixes perf on lower end devices
r
You’re using a Canvas just to get the size?
it would be better to use
onGloballyPositioned
on the
AndroidView
(or
Surface
)
z
onSizeChanged
should work too, much cheaper than
onGloballyPositioned
if you’re just reading the size
👆 2
💡 1
c
Cool. I will go for onSizeChanged.
a
Is there any reason you can’t use Google Maps for compose here? nvm, the gh issue seems to address my question
e
@shikasd Based on this comment, is there an open issue for rendering compose into a bitmap? I know some work was done around this recently but I vaguely remember it was for latest androidversions only kinda thing
s
There's some experimental primitive: https://developer.android.com/jetpack/compose/graphics/draw/modifiers#composable-to-bitmap It has some limitations around hardware accelerated drawings (e.g. shadows are not working yet iirc)
thank you color 1
c
@andrew yeah I just haven't (for this project) been able to convert yet to gmaps compose. and the PM doesn't wanna carve out time for the update. so stuck with android view at the moment.
Re: rendering compose to a bitmap. sounds interesting. i dont fully understand the perf effects of that. but sounds cool.
s
right now, each marker/cluster is a separate view, which is pretty expensive
c
Oooh. Gotcha. Yeah that makes sense that it'd be bad.
Wait. Even if I use AndroidView each cluster is a separate view? (I'm not currently using gmaps- compose in this project)
a
It sounds like you don’t need the actual window size values here, but if you do, I’d recommend
WindowMetricsCalculator
via a snippet like https://github.com/google/accompanist/pull/1576 in Compose. You almost never want the physical screen size from
displayMetrics
, since your app could be in a smaller window, and
LocalConfiguration.current.screenWidthDp/screenHeightDp
are not the size of the window for a few reasons.
c
Yeah, personally I wish the example in google maps showed a modern way of grabbing that value, but you can't have everything in life. 😄 Appreciate all of the things I learned in this thread. xposted back into that github page so hopefully it helps someone else out.
111 Views