android - 每当我单击按钮/JetPack/Kotlin 时,在可组合函数中推送动画以重新开始

标签 android android-jetpack-compose android-animation android-jetpack android-jetpack-compose-material3

**每次用户单击按钮时,我想在框中显示不同的图标,并且过渡应该从其初始状态重新开始**

fun card(iconColor: Color){
    val animateShape = remember { Animatable(50f) }
    Card(
        modifier = Modifier
            .size(100.dp)
            .padding(5.dp)
            .border(width = Dp(animateShape.value), White, shape = CircleShape),
        shape = CircleShape,
        elevation = 5.dp,
        backgroundColor = BlueGrey,
    ) {
            Icon(
                imageVector = Icons.Default.DoneAll,
                contentDescription = null,
                tint = iconColor,
                modifier = Modifier
                    .padding(12.dp)

            )
        }
    LaunchedEffect(iconColor) {
        animateShape.animateTo(
            targetValue = 2f,
            animationSpec = repeatable(
                animation = tween(
                    durationMillis = 3000,
                    easing = LinearEasing,
                    delayMillis = 500
                ),
                iterations = 1
            )
        )
    }
}```

最佳答案

我不太确定,但你在寻找这样的东西吗?每次点击它都会用 scaleIn/scaleOut 动画更改 Icon

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun SwitchIconOnClick() {

    var iconState by remember { mutableStateOf("State_1") }

    AnimatedContent(
        targetState = iconState,
        contentAlignment = Alignment.Center,
        transitionSpec  = {
            scaleIn(animationSpec = tween(durationMillis = 200)) with
                    scaleOut(animationSpec = tween(durationMillis = 200))
        }
    ) { state ->

        Box(
            modifier = Modifier
                .clip(CircleShape)
                .size(50.dp)
                .clickable {
                    when (state) {
                        "State_1" -> {
                            iconState = "State_2"
                        }
                        "State_2" -> {
                            iconState = "State_3"
                        }
                        "State_3" -> {
                            iconState = "State_1"
                        }
                    }
                },
            contentAlignment = Alignment.Center
        ) {
            Icon(imageVector =  when (state) {
                "State_1" -> {
                    Icons.Rounded.NotificationAdd
                }
                "State_2" -> {
                    Icons.Rounded.ClosedCaption
                }
                "State_3" -> {
                    Icons.Rounded.Delete
                }
                else -> {
                    Icons.Rounded.DataArray
                }
            },
            contentDescription = null
            )
        }
    }
}

enter image description here

关于android - 每当我单击按钮/JetPack/Kotlin 时,在可组合函数中推送动画以重新开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74080089/

相关文章:

android - 将 ForeignCollection 转换为 ArrayList - ORMLite、Gson 和 Android

kotlin - Jetpack Compose Row - 绘制子元素 sibling

android - 如何在 Jetpack Compose 中获取带有刀柄外部 Activity 的 View 模型

android - rememberLauncherForActivityResult 抛出 Launcher 尚未在 Jetpack Compose 中初始化

Android 转换 : Go to other scene without animation on orientation change

android - 图库选定位置动画

android-animation - 使用 Object AnimatorAndorid 进行对角线平移

android - 如何用 Fab 打开 NavigationDrawer?

java - 访问服务器计算机上的数据库的 Android 应用程序

android marker remove()方法不起作用