LoginSignup
1
0

[JetpackCompose] 何かを開始する前にカウントダウンするアニメーション

Posted at

はじめに

  • 今回は簡単なJetpackComposeのサンプルコードになります

どのような内容か

  • 5秒前からカウントダウンし、最後にCallbackが呼ばれる
  • 数字はアニメーションで表示→消えるを繰り返してカウントダウンを行う

表示例

スクリーンショット 2024-02-02 13.45.37.png スクリーンショット 2024-02-02 13.45.27.png

コード

CountDown.kt
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun CountDown(
    onFinishedCountDownTimer: () -> Unit,
    modifier: Modifier = Modifier,
) {
    var count by remember { mutableStateOf(5) }
    LaunchedEffect(Unit) {
        do {
            delay(1_000)
        } while (count-- > 0)
        onFinishedCountDownTimer()
    }
    AnimatedContent(
        modifier = modifier.fillMaxSize(),
        targetState = count,
        label = "countDown",
    ) {
        Box(
            modifier = modifier.fillMaxSize(),
            contentAlignment = Alignment.Center,
        ) {
            Text(
                text = "$it",
                fontSize = 64.sp,
                color = Color.Black,
            )
        }
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0