eygraber
12/12/2022, 4:29 AMModifier.pointerInput
to track a pointer over a bitmap and pull out the color of the underlying pixel. I also use an AnimatedVisibility
to show a Text
label under the position where the pointer is that shows the hex of the color.
Is it expected that using val foo by transition.animate*
in an AnimatedVisibilityScope
will cause the function to recompose even after the transition reaches the target state? If I remove the transition.animate*
calls the function only composes once, and when I put them in it recomposes on every pointer move.
Code in 🧵eygraber
12/12/2022, 4:30 AMvar selectedPosition by remember { mutableStateOf(Offset.Zero) }
var selectedColor by remember { mutableStateOf(Color.Unspecified) }
Box(modifier = Modifier.pointerInput {
selectedPosition = ...
selectedColor = ...
}) {
val isLabelVisible = selectedColor.isSpecified && selectedPosition != Offset.Zero
AnimatedVisibility(
visible = isLabelVisible,
enter = EnterTransition.None,
exit = ExitTransition.None
) {
ColorLabel(
position = { selectedPosition },
color = { selectedColor }
)
}
}
@Composable
fun AnimatedVisibilityScope.ColorLabel(
position: () -> Offset,
color: () -> Color
) {
val labelWidth by transition.animateDp {
if(it == EnterExitState.Visible) 100.dp else 0.dp
}
println("Recomposes on every pointer move if labelWidth is present")
ColorLabel(color, labelWidth)
}
tad
12/12/2022, 5:31 AMlabelWidth
in the call to ColorLabel
, and that will require transition.animateDp
to be called to provide that value. If you replace by transition.animateDp
with = transition.animateDp
, then the State<Dp>
won't be read immediately, and depending on where you read it in ColorLabel
, the call to ColorLabel
could be skipped.tad
12/12/2022, 5:36 AMeygraber
12/12/2022, 5:53 AM