LoginSignup
0
1

More than 1 year has passed since last update.

JetpackCompose Coil で GIF

Last updated at Posted at 2022-08-01

使い回しのきく基本的な最小構成で。

implementation "io.coil-kt:coil-compose:2.1.0"
implementation "io.coil-kt:coil-gif:2.1.0"
val url = "https://media.giphy.com/media/DhstvI3zZ598Nb1rFf/giphy-downsized-large.gif"

val imageRequest = ImageRequest.Builder(LocalContext.current)
  .data(url)
  .crossfade(true)
  .build()

val imageLoader = ImageLoader.Builder(LocalContext.current)
  .components {
    add(if (SDK_INT >= 28) ImageDecoderDecoder.Factory() else GifDecoder.Factory())
  }
  .build()

AsyncImage(
  model = imageRequest,
  imageLoader = imageLoader,
  contentDescription = null
)

ProgressIndicator は常にあるほうがいい。

Reflector Recording.gif

@Composable
fun AsyncImageGif(
  url: String
) {
  val context = LocalContext.current

  val imageRequest = ImageRequest.Builder(context)
    .data(url)
    .crossfade(true)
    .build()

  val imageLoader = ImageLoader.Builder(context)
    .components {
      add(if (SDK_INT >= 28) ImageDecoderDecoder.Factory() else GifDecoder.Factory())
    }
    .build()

  SubcomposeAsyncImage(
    model = imageRequest,
    contentDescription = null,
    imageLoader = imageLoader,
    modifier = Modifier.fillMaxWidth(),
    loading = {
      CircularProgressIndicator(
        modifier = Modifier.requiredSize(48.dp),
      )
    },
    error = {
      Icon(Icons.Filled.Error, null)
    }
  )
}

Any? な引数て使いづらいな。

👉 【最新情報】3つの画像読み込みライブラリ Glide / Picasso / Coil - JetpackCompose 対応の状況

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