Hello. I am very excited about the release of Comp...
# compose
s
Hello. I am very excited about the release of Compose Beta. I am reading new documentation and noticed on some topics values that represent size or offsets used as raw values without conversion from DP to Pixels. Was this intentional or by mistake? Thanks.
h
These float values work directly with pixels so they don't need conversion. You can always define your values in Dp and conver them using LocalDensity
I'm assuming AmbientDensity was renamed to LocalDensity, never checked it out after major rename.
t
It was renamed yep. All Ambient now are Local
Canvas values are pixel based. Density is used in View contexts only
l
These contexts should also have a density in scope already, so you shouldn’t need
LocalDensity
. For instance, the lambda for Canvas has density so you can use
40.dp.toIntPx()
for instance, if you have a dp value. In general, if you find yourself needing a density object, make sure you don’t already have one available before reaching for
LocalDensity.current
👍 2
s
I lead to the fact that in these places you cannot set values in raw pixels. Since the 5px or 40px on a phone with a screen of density 300ppi or 500ppi(for instance) will take different amount of space. In my understanding, the documentation must be as accurate as possible in such things. Imagine someone will just grab the code from animation documentation and probably see no animation there, because offset by 40px would take 1mm of space on high dencity display. @Halil Ozercan @Leland Richardson [G]
l
if you’re saying that you think the docs should be updated to use
.toIntPx()
or something then i think that’s good feedback. I think there are some cases where the math you are using is already in px but when it’s hard coded like this example is, dp is usually what you want
💯 1
❤️ 1
s
Yes, I believe the documentation must be as clear as possible
l
can you link me to this docs page?
s
Copy code
<https://developer.android.com/jetpack/compose/graphics#canvas>
<https://developer.android.com/jetpack/compose/animation#high-level-apis>
The rest of the documentation uses DP values explicitly.
Thank you, Leland
t
Are you saying that I would use
.toIntPx()
to set stroke width in the screenshot above?
l
.toPx()
perhaps instead because it is accepting floats. but yeah, that’s roughly the gist of it
to be clear, you would only do this if you were wanting the strokeWidth to be a density-independent value
👍 1