Sergey Y.

08/18/2021, 2:40 PM
Working with custom widgets in Compose, I caught myself with constant
transformations. Because the Layout system works with
and the Canvas system accepts
. 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.

Anastasia Rozovskaya

08/18/2021, 5:01 PM
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

Andrey Kulikov

08/18/2021, 5:29 PM
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

Michael Paus

08/19/2021, 6:09 PM
Have you considered to apply a scale transform to the whole canvas before you start drawing which corresponds to the current density (scale factor)?