Compose 实现Led效果

33 阅读1分钟
@Composable
private fun createAnimContent(){

    var parentWidth by remember {
        mutableStateOf(0)
    }
    var childWidth by remember {
        mutableStateOf(0)
    }
    val anim = remember {
        Animatable(0f)
    }

    LaunchedEffect(key1 = Unit,
        block = {
            anim.stop()
            anim.animateTo(0f)
            val duration = (secondsBase * 1000 / speedList[speedIndex]).toInt()
            anim.animateTo(1f, infiniteRepeatable(animation = tween(duration, easing = LinearEasing)))
        }
    )
    Box(modifier = Modifier
        .fillMaxSize()
        .background(
            color = colorList[colorBgIndex]
        )
        .onPlaced {
            parentWidth = it.size.width
        }
        .background(
            color = Color.White.copy(alpha = 0.5f),
            shape = RoundedCornerShape(8.dp),
        )
        .border(width = 1.dp, color = Color.White, shape = RoundedCornerShape(8.dp))
        .composed {
            if (childWidth <= parentWidth) {
                return@composed this.offset {
                    val xOffset =
                        ((parentWidth - (parentWidth + childWidth) * anim.value)).toInt()
                    IntOffset((xOffset), 0)
                }
            }
            return@composed this
        },
        contentAlignment = Alignment.CenterStart
    ){
        LazyRow(
            content = {
                item{
                    Text(text = content, modifier = Modifier
                        .wrapContentWidth()
                        .align(Alignment.CenterEnd)
                        .padding(end = 10.dp)
                        .composed {
                            if (childWidth > parentWidth) {
                                return@composed this.offset {
                                    val xOffset =
                                        ((parentWidth - (parentWidth + childWidth) * anim.value)).toInt()
                                    IntOffset((xOffset), 0)
                                }
                            }
                            return@composed this
                        }
                        .onPlaced {
                            childWidth = it.size.width
                        },

                        style = TextStyle(
                            fontSize = wordSizeList[wordSizeIndex].sp,
                            color = colorList[colorIndex],
                            fontFamily = FontFamily(Font(fontList[fontIndex].font))
                        ),
                        maxLines = 1
                    )
                }
            })
    }

}

videoGif.gif