检查动画是否已完成Glide Android

时间:2018-06-25 10:40:08

标签: android android-glide

我正在使用下面的侦听器代码

 .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    GifDrawable gifDrawable = null;
                    Handler handler = new Handler();
                    if (resource instanceof GifDrawable) {
                        gifDrawable = (GifDrawable) resource;

                        int duration = 0;
                        GifDecoder decoder = gifDrawable.getDecoder();
                        for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
                            duration += decoder.getDelay(i);
                        }

                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                                startActivity(intent);
                                SplashScreenActivity.this.finish();
                            }
                        }, (duration + 3000));

                    }

                    return false;
                }
            })

在这里我无法通过

访问getDecoder()
 GifDecoder decoder = gifDrawable.getDecoder();

如何检查动画是否完成。为什么不能在这里访问getDecoder 我正在使用以下下滑依赖

implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'

预先感谢

1 个答案:

答案 0 :(得分:0)

Glide.with(context) // replace with 'this' if it's in activity
        .load(url)
        .into(new DrawableImageViewTarget(memeBackground) {
            public void onResourceReady(Drawable resource, @Nullable Transition<? super Drawable> transition) {
                if (resource instanceof GifDrawable) {
                    ((GifDrawable) resource).registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
                        @Override
                        public void onAnimationEnd(Drawable drawable) {
                            super.onAnimationEnd(drawable);
                            log.D("animation is completed");
                        }
                    });
                    super.onResourceReady(resource, transition);
                }}});