Miguel Coleto
09/21/2020, 8:21 AM@Composable
fun Dice(
number: DiceNumber,
isEnabled: Boolean = true,
modifier: Modifier = Modifier
) {
val diceNumberDrawable = when (number) {
DiceNumber.One -> R.drawable.dice_one
DiceNumber.Two -> R.drawable.dice_two
DiceNumber.Three -> R.drawable.dice_three
DiceNumber.Four -> R.drawable.dice_four
DiceNumber.Five -> R.drawable.dice_five
DiceNumber.Six -> R.drawable.dice_six
}.run { vectorResource(id = this) }
val rotation = animatedFloat(0f)
val randomSpins = remember(number.value) { Random.nextInt(2, 4) }
val randomDuration = remember(number.value) { Random.nextInt(600, 1200) }
rotation.animateTo(
targetValue = 360f * randomSpins,
anim = tween(
durationMillis = randomDuration,
easing = CubicBezierEasing(0.3f, 0.9f, 1f, 1f)
)
)
Image(
asset = diceNumberDrawable,
alpha = if (isEnabled) 1f else 0.4f,
modifier = modifier
.size(80.dp)
.drawLayer(rotationZ = rotation.value)
)
}
Yann Badoual
09/21/2020, 8:26 AMInt
that you increment each time you press the roll button. And use this int as a key to the remember
calls instead of number.value
. This way each roll will reset the value remembered and should re-trigger the animateTo
callMiguel Coleto
09/21/2020, 8:33 AMTimo Drick
09/24/2020, 12:31 AMchunk.forEach { number ->
key(number) {
Dice(
number = number,
isEnabled = number.value >= minimumResult,
modifier = Modifier.weight(1f)
)
}
}
But in theory this should not be necessary.Miguel Coleto
09/24/2020, 5:49 AMTimo Drick
09/24/2020, 1:34 PMMiguel Coleto
09/24/2020, 4:28 PMTimo Drick
09/24/2020, 4:42 PM