Glide 使用问题汇总———2017/06/12更新

GitHub开源地址

使用Glide加载Gif和常见图片格式时出现的常见问题:

一、离线缓存策略的配置

.diskCacheStrategy(DiskCacheStrategy.ALL)

如下几种:

    /** Caches with both {@link #SOURCE} and {@link #RESULT}. */
    ALL(true, true),//全部都缓存
    /** Saves no data to cache. */
    NONE(false, false),//全都不缓存
    /** Saves just the original data to cache. */
    SOURCE(true, false),//只缓存资源
    /** Saves the media item after all transformations to cache. */
    RESULT(false, true);//只缓存结果


二、图片加载的监听

方法

Glide.with(this).load(advertisementBean.getImageUrl()).diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    Times(TIME_OUT);
                    tv_countdown.setVisibility(View.VISIBLE);
                    return false;
                }
            }).into(iv_advert_img);

问题:当外层的View的Visibility属性GONE时,OnResourceReady方法体内部分代码不会被执行


这是作者给的解释:

Glide加载图片 into到ImageView中,必须要知道ImageView的宽、高,他才能知道怎么加载图片,所以监听加载过程时要确保ImageView不是GONE,否则你会发现OnResourceReady方法体内的部分代码永远不被执行...


Update 16-10-20

case:

服务器返回图片 url Glide 无法加载,Glide 是不会走onResourceReady,而是直接走onException,所以在onResourceReady做的相关操作,在onException的时候也要在方法体内做好对应的处理。


Update 16-12-02

自定义圆形占位图及使用ImageView+GLide加载圆形图片

Glide.with(ctx).load(TaskBeanNew.getBusinessImg()).asBitmap().placeholder(R.drawable.defult_head).centerCrop().into(new BitmapImageViewTarget(viewHolder.img_icon) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(ctx.getResources(), resource);
                    /**
                     * 加载圆形图片
                     */
                    circularBitmapDrawable.setCircular(true);
                    viewHolder.img_icon.setImageDrawable(circularBitmapDrawable);
                }


                @Override
                public void onLoadStarted(Drawable placeholder) {
//                            super.onLoadStarted(placeholder);
                    /**
                     * 覆盖原生方法,加载圆角占位图
                     */
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(ctx.getResources(), ((BitmapDrawable) placeholder).getBitmap());
                    circularBitmapDrawable.setCircular(true);
                    viewHolder.img_icon.setImageDrawable(circularBitmapDrawable);
                }

                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                    super.onLoadFailed(e, errorDrawable);
                    /**
                     * 覆盖原生方法,加载圆角占位图
                     */
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(ctx.getResources(), ((BitmapDrawable) errorDrawable).getBitmap());
                    circularBitmapDrawable.setCircular(true);
                    viewHolder.img_icon.setImageDrawable(circularBitmapDrawable);
                }
            });

Update 17-03-08

代码如下:

    public void loadRoundImage(final Context mContext, String url, final ImageView mImageView, int placeHolderResourceId) {
        if (null == mContext)
            return;
        Glide.with(mContext).load(url).asBitmap().placeholder(placeHolderResourceId).centerCrop().into(new BitmapImageViewTarget(mImageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                mImageView.setImageDrawable(circularBitmapDrawable);
            }


            @Override
            public void onLoadStarted(Drawable placeholder) {
                // super.onLoadStarted(placeholder);
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(mContext.getResources(), ((BitmapDrawable) placeholder).getBitmap());
                circularBitmapDrawable.setCircular(true);
                mImageView.setImageDrawable(circularBitmapDrawable);
            }


            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                 super.onLoadFailed(e, errorDrawable);
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(mContext.getResources(), ((BitmapDrawable) errorDrawable).getBitmap());
                circularBitmapDrawable.setCircular(true);
                mImageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }


自定义圆形图片,占位图,错误图;


问题:

部分图片加载失败后 onLoadFailed 会重复走多次,且Exception 为 null,询问作者,但是没有给予答复,原因尚不明

解决办法

Glide.with(mContext) 使用的上下文采用Application的上下文 Glide.with(mContext.getApplicationContext())  解决;

 

17/06/12

问题:

加载大分辨率图片时,ImageView的scaleType设置不当,导致使用Glide时出现OOM

解决办法

ImageView的scaleType的问题,当设置为fitXY时,虽然ImageView显示那么点尺寸,但是,但是Glide加载图片时,却是以全分辨率加载的,于是加载几张,就OOM了。
改成fitCenter或者centerCrop(试了一下fitStart、fitEnd也行,总之看需求了),就好了,会自动缓存小图,滚动起来也非常流畅。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值