Compose 学习帧动画

1.介绍:飞行棋项目需要掷骰子动画,故学着用Compose做了一个简单帧动画。此为学习笔记,大佬勿喷。

2.效果展示:

Composable 组件动画可以在Android Studio 中预览,

骰子模型是我自己用Blender建模做的,凑合看哈哈。

点击Play会播放一次动画,总时长设置为1s。

最后为了偷懒,直接将当前帧设置为了第一帧来做到重播,因此有些不流畅。

3.代码

@Composable
fun DiceAnimation() {
    val frameList = listOf(
        R.drawable.frame1,
        R.drawable.frame2,
        R.drawable.frame3,
        R.drawable.frame4,
        R.drawable.frame5,
        R.drawable.frame6,
        R.drawable.frame7,
        R.drawable.frame8,
        R.drawable.frame9,
        R.drawable.frame10,
        R.drawable.frame11,
        R.drawable.frame12,
        R.drawable.frame13,
        R.drawable.frame14,
        R.drawable.frame15,

    )
    var currentFrame = remember { Animatable(0f) }
    var isPlaying by remember { mutableStateOf(false) }
    LaunchedEffect(isPlaying) {
        if (isPlaying) {
            currentFrame.animateTo(
                targetValue = (frameList.size - 1).toFloat(),
                animationSpec = repeatable(
                    animation = keyframes {
                        durationMillis = 1000  // the total animation duration
                    },
                    repeatMode = RepeatMode.Restart,
                    iterations = 1
                )
            )
            isPlaying=false
            currentFrame.snapTo(0f)

        }

    }


    Box(
        modifier = Modifier
            .size(200.dp),
        contentAlignment = Alignment.Center
    ) {
        Column {
            Image(
                painter = painterResource(id = frameList[currentFrame.value.toInt()]),
                contentDescription = null,
                contentScale = ContentScale.Fit
            )
            TextButton(
                modifier = Modifier
                    .size(80.dp),
                onClick = {

                    isPlaying = true
                }
            ) {
                Text(text = "Play")
            }
        }

    }
}

这只是很简单的示例,仅供参考。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值