Working with custom widgets in Compose, I caught m...
# compose
s
Working with custom widgets in Compose, I caught myself with constant
dp->px
and
px->dp
transformations. Because the Layout system works with
Dp
and the Canvas system accepts
Px
. Are there any recommendations or best practices on how to reduce the conversion of these units? Reduce repeatability of the code. The more they used in the code, the more it adds noise. 🧵
Imagine if I had a complex widget with a dozen more properties.
a
Just an opinion, but I thought this is a neat extension function and you doesn’t need anything simpler. The way out may be to write your own Composable and pass Dp instead of Px right away so that you would not be converting it to Dp inside the implementation.
👍 1
👍🏼 1
a
usually you want to always work with Dps in the composition and convert to pixels right inside the draw block. If you want to save on allocating a Stroke on every redraw you can use Modifier.drawWithCache, convert dp to px and allocate a Stroke inside that block
👍🏼 1
👍 4
m
Have you considered to apply a scale transform to the whole canvas before you start drawing which corresponds to the current density (scale factor)?