zjupure / GlideWebpDecoder

A Glide WebpDecoder Intergration Library for decoding and displaying webp images
Apache License 2.0
740 stars 91 forks source link

Question: is it possible to decode the frames of GIF/WEBP and get callbacks for playing them in specific threads? #115

Closed AndroidDeveloperLB closed 9 months ago

AndroidDeveloperLB commented 11 months ago

I want to play the animated GIF/WEBP in the background, and also get the callback of a bitmap being ready in a different thread of my choice (can be UI thread, can be a different one instead).

Is it possible?

zjupure commented 9 months ago

You can use glide custom target to receive webp frames and implement some code as WebpDrawable.java

AndroidDeveloperLB commented 9 months ago

@zjupure Not sure I understand how you mean is the best way to do it.

This is how I do it, but is it the best?

Glide.with(context.applicationContext).load(filePath).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(object : CustomTargetEx<Drawable>() { 
...
override fun onResourceReady(drawable: Drawable, transition: Transition<in Drawable>?) {
    when (drawable) {
        is GifDrawable -> {
            val bitmap =
                Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888) 
            val canvas = Canvas(bitmap)
            drawable.setBounds(0, 0, bitmap.width, bitmap.height)
            drawable.setLoopCount(GifDrawable.LOOP_FOREVER)
            val callback = object : CallbackEx() { 
                override fun invalidateDrawable(who: Drawable) {
                    if (listener != null) { 
                        who.draw(canvas)
                        handler.post {
                            listener?.onGotFrame(bitmap, drawable.frameIndex, drawable.frameCount)
                        } 
                    } 
                }
            }
            drawable.callback = callback
            drawable.start()
        }

Similar to WebpDrawable.

It creates a new Bitmap this way on each frame and "rides on" the Drawable as it plays, so maybe it's not a good thing?