Untitled
unknown
kotlin
3 years ago
2.7 kB
8
Indexable
@Composable
fun Snow(size: IntSize) {
val coroutine = rememberCoroutineScope()
val transition = rememberInfiniteTransition()
val snowXOffsetList = mutableListOf<Float>()
val snowYOffsetList = mutableListOf<Float>()
val snowSizeList = mutableListOf<Float>()
val snowAlphaList = mutableListOf<Float>()
val numberOfSnows by remember { mutableStateOf(10) }
for (i in 0 until numberOfSnows) {
val snowSize by transition.animateFloat(
initialValue = remember { (5..15).random().toFloat() },
targetValue = remember { 0f },
animationSpec = infiniteRepeatable(
animation = tween(5000),
)
)
snowSizeList.add(snowSize)
val snowYOffset by transition.animateFloat(
initialValue = remember { (-40..10).random().toFloat() },
targetValue = remember {
if (stopwatchShowLabel) {
((size.height * 0.60).toInt()..((size.height * 0.70)).toInt()).random()
.toFloat()
} else {
((size.height * 0.80).toInt()..(size.height * 0.90).toInt()).random().toFloat()
}
},
animationSpec = infiniteRepeatable(
animation = tween(5000, easing = EaseInOut),
)
)
snowYOffsetList.add(snowYOffset)
val snowXOffset by transition.animateFloat(
initialValue = remember { (1..size.width).random().toFloat() },
targetValue = remember { (1..size.width).random().toFloat() },
animationSpec = infiniteRepeatable(
animation = tween(10000, easing = EaseInOut),
),
)
snowXOffsetList.add(snowXOffset)
val snowAlpha by transition.animateFloat(
initialValue = remember { 1f },
targetValue = remember { 0f },
animationSpec = infiniteRepeatable(
animation = tween(5000),
)
)
snowAlphaList.add(snowAlpha)
}
for (i in 0 until numberOfSnows) {
val radius = snowSizeList[i] / (snowYOffsetList[i] * 0.01 + 3).toFloat()
Canvas(
modifier = Modifier,
onDraw = {
drawCircle(
color = Color.White,
// alpha = snowAlphaList[i],
radius = if (radius > 15f) 15f else radius,
center = Offset(x = snowXOffsetList[i], y = snowYOffsetList[i]),
)
})
}
}Editor is loading...