0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Composeで画像を横スクロールしながら表示する

Posted at

はじめに

今回はJetpackComposeを使って画像の横スクロールをしてみたいと思います

コード

Compose関数ないで下記を定義し、スクロールさせたい画像のModifier.offset { IntOffset(offsetX.value.toInt(), 0) }を定義すれば完了です

 val offsetX = remember { Animatable(0f) }

    val screenWidth = LocalConfiguration.current.screenWidthDp.dp
    val pxWidth = with(LocalDensity.current) { screenWidth.toPx() }

    LaunchedEffect(Unit) {
        // 画面の外から左端までの移動をループ
        while (true) {
            // アニメーションの初期位置を右端にリセット
            offsetX.snapTo(pxWidth)

            offsetX.animateTo(
                targetValue = -pxWidth,
                animationSpec = tween(
                    durationMillis = 20000, // 20秒で移動
                    easing = LinearEasing
                )
            )
        }
    }

最後に

方向や時間さえ変えて仕舞えば他にも応用がきくので、備忘録として残しておきます

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?