orangy
Brush
object? At least for SolidColor
?Chris Sinco [G]
06/10/2022, 8:45 PMorangy
orangy
orangy
Chris Sinco [G]
06/11/2022, 3:12 AMSolidColor
, which returns a Brush
@Preview
@Composable
fun AnimateColor() {
var toggle by remember { mutableStateOf(false) }
val animatedColor by animateColorAsState(
targetValue = if (toggle) Color.Yellow else Color.Magenta,
animationSpec = tween(2000)
)
MaterialTheme {
Surface {
Box(
Modifier
.clickable { toggle = !toggle }
.aspectRatio(1f)
.background(SolidColor(animatedColor))
.padding(20.dp),
contentAlignment = Alignment.Center
) {
Text(
"Box with changing colors",
textAlign = TextAlign.Center,
style = MaterialTheme.typography.h3
)
}
}
}
}
Chris Sinco [G]
06/11/2022, 3:13 AManimated if solid color, and not animated in all other casesDo you have a visual example of what you are trying to do? Wondering if animating the values passed into Brush would suffice.
Berkeli Alashov
06/11/2022, 4:26 AMorangy
@Composable
fun updateButtonAppearanceTransition(appearance: ButtonAppearance): AppearanceTransitionState {
val transition = updateTransition(appearance)
val background = mutableStateOf(appearance.background) // TODO: animate background color
val shapeStroke = transition.animateShapeStroke(label = "AnimateShapeStroke") { it.shapeStroke }
val haloStroke = transition.animateShapeStroke(label = "AnimateHaloStroke") { it.haloStroke }
return AppearanceTransitionState(background, shapeStroke, haloStroke)
}
When it comes to built-in stuff, like colors it’s simple. But I can’t find how to deal with the Brush hereorangy
@Composable
inline fun <S> Transition<S>.animateSolidColor(
label: String = "SolidBrushAnimation",
targetValueByState: @Composable() (state: S) -> SolidColor?
): State<SolidColor> {
val color by animateColor(label = "$label.color", transitionSpec = { defaultAnimationSpec() }) {
targetValueByState(it)?.value ?: Color.Unspecified
}
return remember { derivedStateOf { SolidColor(color) } }
}
And then using it in the composite transition