How to play GIF in android

后端 未结 8 828
死守一世寂寞
死守一世寂寞 2020-11-30 05:08

Hello stackoverflow I\'m trying to develop an android application to play my own GIF, here is the code snippet

MainActivity.java

8条回答
  •  清歌不尽
    2020-11-30 05:19

    You can also use Coil library, which gives you a bit less control over Gif images than Glide does. But still gives simple implementation if you just want to load the GIF once and show. Probably, if you want to repeat that you need to trigger the drawable again or just call the load function once again. Anyway even if calling load again the resources should be cached under the hood(something to be investigated, not 100% sure). Everything you need is adding dependency:

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

    And specifying your decoding mechanism:

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

    That's all go-ahead and load images/gifs directly in your image views:

    app_logo.load("needed_gif_url")
    

    Don't forget to pass the needed image loader to each request where you need your decoder to be used, as for GIFs. Or just make your custom imageLoader as default for each request:

    Coil.setImageLoader(imageLoader)
    

提交回复
热议问题