简体   繁体   中英

How do I know when a Webp/Gif animation has completed in Fresco

I am using the following code to display a Gif, and I need a callback to know when the Webp/Gif animation has completed:

DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setUri(uri)
    .setAutoPlayAnimations(true)
    .build();
mSimpleDraweeView.setController(controller);

This was added in Fresco version 1.4.0 + which includes a new implementation for animated GIFs and animated WebPs.

According to the solution to Fresco issue#181 , you set a ControllerListener and attach an animation listener:

DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setUri(uri)
    .setAutoPlayAnimations(true)
    .setControllerListener(new BaseControllerListener() {
        @Override
        public void onFinalImageSet(String id, @Nullable Object imageInfo, @Nullable Animatable animation) {
            if (animation != null) {
                AnimatedDrawable2 animatedDrawable = (AnimatedDrawable2) animation;
                animatedDrawable.setAnimationListener(new AnimationListener() {
                    @Override
                    public void onAnimationStart(AnimatedDrawable2 drawable) {
                        //
                    }

                    @Override
                    public void onAnimationStop(AnimatedDrawable2 drawable) {
                        // Called when the animation is stopped for the given drawable.
                        // Unless you manually stop the animation, this is where you get notified the animation has "completed".
                    }

                    @Override
                    public void onAnimationReset(AnimatedDrawable2 drawable) {
                        //
                    }

                    @Override
                    public void onAnimationRepeat(AnimatedDrawable2 drawable) {
                        //
                    }

                    @Override
                    public void onAnimationFrame(AnimatedDrawable2 drawable, int frameNumber) {
                        //
                    }
                });
            }
        }
    })
    .build();
mSimpleDraweeView.setController(controller);

And these are the dependencies in the build.gradle (Module level):

// Fresco
implementation 'com.facebook.fresco:fresco:1.11.0'

// For animated GIF support
implementation 'com.facebook.fresco:animated-gif:1.11.0'

// For WebP support, including animated WebP
implementation 'com.facebook.fresco:animated-webp:1.11.0'
implementation 'com.facebook.fresco:webpsupport:1.11.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM