Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback when GIF finish first loop

I need a callback when the Gif finish animating

@Composable
fun AsyncGIFLoader(
) {


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

    Image(
        painter = rememberAsyncImagePainter(
            ImageRequest.Builder(context).data(data = R.drawable.mygif).apply(block = {
                size(Size.ORIGINAL)
            }).build(), imageLoader = imageLoader
        ),
        contentDescription = null,
        modifier = Modifier.fillMaxWidth(),
    )
}

i tried target that displays when image loader shows onSuccess, onError but i it didn't show when Gif finish animating, it calls when the gif image loads successfully.

is there a way to detect when the Gif finish animating?

like image 681
dev mobile Avatar asked Jul 30 '26 19:07

dev mobile


1 Answers

The ImageRequest.Builder has an onAnimationEnd method (when using io.coil-kt:coil-gif):

rememberAsyncImagePainter(
    model = ImageRequest.Builder(context)
        .data(data = R.drawable.mygif)
        .size(Size.ORIGINAL)
        .onAnimationEnd { /*...*/ }
        .build(),
    imageLoader = imageLoader,
)

From the docs:

Set the callback to be invoked at the end of the animation if the result is an animated Drawable.

like image 69
Leviathan Avatar answered Aug 01 '26 09:08

Leviathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!