Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide callback when animated GIF is done playing #606

Closed
rundavidrun opened this issue Dec 4, 2020 · 6 comments · Fixed by #676
Closed

Provide callback when animated GIF is done playing #606

rundavidrun opened this issue Dec 4, 2020 · 6 comments · Fixed by #676
Labels
enhancement New feature or request

Comments

@rundavidrun
Copy link

Provide a mechanism for the caller to be notified when an animated GIF has completed all loops. ( Similar to Glide V4's onAnimationEnd listener - see bumptech/glide#3438 )

For example,

binding.myImageView.load(R.drawable.some_gif_animation) {
   repeatCount(0)
   onAnimationComplete = { /* do something */ }
}
@rundavidrun rundavidrun added the enhancement New feature or request label Dec 4, 2020
@colinrtwhite
Copy link
Member

👍 Makes sense. In the meantime as a work-around, MovieDrawable and AnimatedImageDrawable both implement Animatable2/Animatable2Compat which allows you to set a listener for start/end events.

@rundavidrun
Copy link
Author

@colinrtwhite - I'm not sure how to get access to the MovieDrawable when using load. Any pointers?

@sagar-viradiya
Copy link
Contributor

@rundavidrun Head on to https://coil-kt.github.io/coil/targets/

The idea is to use target callbacks and in the success callback, you can typecast Drawable to MovieDrawable or AnimatedImageDrawable depending on which API level you are loading the image.

@rundavidrun
Copy link
Author

Thanks. I still can't make this work. I have a gif stored in the drawable folder as animation.gif. It just loads the first frame of the animation and never calls the callback. I'm using:

val callback = object : Animatable2Compat.AnimationCallback() {
    override fun onAnimationEnd(drawable: Drawable) {
        Log.d("******", "animation end!!")
    }
}
val request = ImageRequest.Builder(this)
        .data(R.drawable.animation)
        .target(
                onSuccess = { result ->
                    Log.d("******", "got onSuccess")
                    val movieDrawable = result as MovieDrawable
                    movieDrawable.setRepeatCount(0)
                    movieDrawable.registerAnimationCallback(callback)
                    binding.fullScreenImage.load(movieDrawable)
                }
        ).build()
val imageLoader = ImageLoader.Builder(this)
        .componentRegistry {
            if (SDK_INT >= 28) {
                add(ImageDecoderDecoder())
            } else {
                add(GifDecoder())
            }
        }
        .build()
imageLoader.enqueue(request)

@sagar-viradiya
Copy link
Contributor

@rundavidrun At the end of onSuccess, you are calling load which is nothing but an extension on ImageView and it internally creates a new request. Set the drawable like this binding.fullScreenImage.setImageDrawable(result). This should fix your issue.

@rundavidrun
Copy link
Author

@sagar-viradiya - using binding.fullScreenImage.setImageDrawable(result) still just shows the first frame of the animation. I'm looking forward to getting the update once your PR is integrated. Thanks for writing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants