TIL #84

loci·2024년 7월 25일
0

TIL

목록 보기
80/103

glide vs coil

gif를 안드로이드에서 재생시킬 수 있는 방법을 찾아보니 glide와 coil을 사용해 구현할 수 있었다.
coil이 glide보다 성능이 약간 더 좋고 kotlin으로 구성되어있어 kotlin coroutine을 사용할 수 있고 가볍다. glide는 coil보다 더 많은 이미지형식을 지원하고 오랫동안 많이 사용되어 성숙한 라이브러리라고 한다.
둘 중에 어떤 것을 쓸지 고민했는데 일단 성능적으로는 큰차이가 안날것 같아고 glide가 오랫동안 쓰여졌다고하니 찾을 수 있는 정보량이 많아 사용하기 더 수월하다고 생각해서 glide로 사용하기로 했다.

gif 한번만 재생하기

private fun loadGif(isAnimationActive: Boolean, loadGif: Int, imageView: ImageView) {  
        Glide.with(this).asGif().load(loadGif)  
            .listener(object : RequestListener<GifDrawable> {  
                override fun onLoadFailed(  
                    e: GlideException?,  
                    model: Any?,  
                    target: Target<GifDrawable>?,  
                    isFirstResource: Boolean  
                ): Boolean {  
                    return false  
                }  
  
                override fun onResourceReady(  
                    resource: GifDrawable?,  
                    model: Any?,  
                    target: Target<GifDrawable>?,  
                    dataSource: DataSource?,  
                    isFirstResource: Boolean  
                ): Boolean {  
                    resource?.setLoopCount(1)  
                    return false  
                }  
  
            }).into(imageView)  
}

setLoopCount(1)로 몇번재생할지 설정

gif 멈추기

private fun loadGif(isAnimationActive: Boolean) {  
    if (isAnimationActive) {  
        Glide.with(this).asGif().load(R.raw.gacha1).into(binding.ivFirstGif)  
        firstAnimate = false  
    } else {  
        Glide.with(this).asBitmap().load(R.raw.gacha1).into(binding.ivFirstGif)  
        firstAnimate = true  
    }  
}

asBitmap()을 이용해 이미지를 변환해 로드하는 식으로 재생하고 멈추는 기능을 만들어준다.

profile
편리한 개발자

0개의 댓글