Android——Glide加载Gif图片监听加载回调函数onResourceReady问题

Glide相比Picasso来说有一个优势不错就是可以很方便的加载Gif图。
比如这样:

Glide.with(activity).load(gifUrl).placeholder(R.drawable.plugmain_no_banner).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imgEg);

如果图片是gif格式,那么可以自动显示GIf动画,很不错。
但是如果现在需要进入页面的时候先显示加载转圈,等gif图片加载ok的时候取消转圈,这个时候我们就需要监听加载回调函数了,网上大部分是这样的。

Glide.with(activity).load(gifMakeTypeResponse.getGifExampleUrl()).
               placeholder(R.drawable.plugmain_no_banner).
               diskCacheStrategy(DiskCacheStrategy.SOURCE).
               into(new SimpleTarget<GlideDrawable>() {
                   @Override
                   public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {

                       imgEg.setBackground(resource);
                   }
               });

对于静态图来说,这个很对,没啥问题,但是我们会发现gif图不动了。

这是为啥?我们跟一下glide的源码into进去看看,然后我们会跟进到GlideDrawableImageViewTarget的onResourceReady函数,我们看下如果只是传了一个imageview人家内部是咋处理的。

/**
  * {@inheritDoc}
  * If no {@link com.bumptech.glide.request.animation.GlideAnimation} is given or if the animation does not set the
  * {@link android.graphics.drawable.Drawable} on the view, the drawable is set using
  * {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
  *
  * @param resource {@inheritDoc}
  * @param animation {@inheritDoc}
  */
 @Override
 public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
     if (!resource.isAnimated()) {
         //TODO: Try to generalize this to other sizes/shapes.
         // This is a dirty hack that tries to make loading square thumbnails and then square full images less costly
         // by forcing both the smaller thumb and the larger version to have exactly the same intrinsic dimensions.
         // If a drawable is replaced in an ImageView by another drawable with different intrinsic dimensions,
         // the ImageView requests a layout. Scrolling rapidly while replacing thumbs with larger images triggers
         // lots of these calls and causes significant amounts of jank.
         float viewRatio = view.getWidth() / (float) view.getHeight();
         float drawableRatio = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight();
         if (Math.abs(viewRatio - 1f) <= SQUARE_RATIO_MARGIN
                 && Math.abs(drawableRatio - 1f) <= SQUARE_RATIO_MARGIN) {
             resource = new SquaringDrawable(resource, view.getWidth());
         }
     }
     super.onResourceReady(resource, animation);
     this.resource = resource;
     resource.setLoopCount(maxLoopCount);
     resource.start();
 }

最主要看最后两句:

resource.setLoopCount(maxLoopCount);
resource.start();

原来GlideDrawable resource对象对于gif图片等动图有相关处理,这样我们就这样改一下:

Glide.with(activity).load(gifMakeTypeResponse.getGifExampleUrl()).
               placeholder(R.drawable.plugmain_no_banner).
               diskCacheStrategy(DiskCacheStrategy.SOURCE).
               into(new SimpleTarget<GlideDrawable>() {
                   @Override
                   public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                       if (resource.isAnimated()) {
                           resource.setLoopCount(GifDrawable.LOOP_FOREVER);
                           resource.start();
                       }
                       imgEg.setBackground(resource);
                       loadingDailog.dismiss();
                   }
               });

这样bug就fixed了。

Basillee wechat