티스토리 뷰

Kotlin

[Android] Coil 라이브러리 Gif load 방법

도토리흑미 2023. 5. 2. 14:34

보편적으로 사용하는 Glide에서도 gif 로드를 지원하지만

Glide 4.2.0 버전 이후부터 특정 디바이스에서는 버벅거리는 현상이 발생하는 것을 확인했습니다.

https://github.com/bumptech/glide/issues/2471

 

GIFs animate slowly with Glide 4.2 · Issue #2471 · bumptech/glide

Here is a screen capture of two gifs playing w/ Glide 3.7: Here is a screen capture of the same two gifs playing w/ Glide 4.2: GlideApp.with(context) .load(url) .diskCacheStrategy(DiskCacheStrategy...

github.com

 

진행하는 프로젝트에서 버전을 바꾸자니 변경해야될 부분이 많고.. 다운그레이드도 마땅치 않아서 

다른 라이브러리를 찾아봤습니다.

 

Coil 라이브러리 https://coil-kt.github.io/coil/README-ko/

 

README ko - Coil

README ko Coil은 Kotlin Coroutines으로 만들어진 Android 백앤드 이미지 로딩 라이브러리입니다. Coil 은: 빠르다: Coil은 메모리와 디스크의 캐싱, 메모리의 이미지 다운 샘플링, Bitmap 재사용, 일시정지/취

coil-kt.github.io

 

사용법은 간단했지만 최신 버전에서 compileSdk 버전을 33이상으로 사용해야해서

일부러 0.10.0 버전을 사용했습니다.

다만 공식 문서에서는 최신버전에 대한 안내만 있어서 

0.10.0 버전에서 Gif 사용하는 방법에 대해서 기록을 남기려 합니다.

 

공식 문서에 안내되어 있는 GIF 사용 방법

 https://coil-kt.github.io/coil/gifs/

 

GIFs - Coil

Gifs Unlike Glide, GIFs are not supported by default. However, Coil has an extension library to support them. To add GIF support, import the extension library: implementation("io.coil-kt:coil-gif:2.3.0") And add the decoders to your component registry when

coil-kt.github.io

 

v 0.10.0

app 수준 build.gradle에 라이브러리 추가

implementation "io.coil-kt:coil:0.10.0"
implementation "io.coil-kt:coil-gif:0.10.0"

 

컴포넌트 레지스트리에 디코더를 추가합니다.

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

현재 (23.05.02) 기준 2.3.0 버전에서는 componentRegistry 대신 components를 사용합니다.

 

사용할 ImageView에 imageLoader를 적용하여 사용합니다.

imageView.load(resId, imageLoader)

 

Glide를 사용하면서 특정 기기에서 버벅거릴 때 참고하시면 좋을 것 같습니다.