Glide踩坑记

来源:互联网 发布:元数据由什么存储 编辑:程序博客网 时间:2024/06/08 01:22

Glide踩坑全集

第一篇 Glide图片和默认图替换过程中,默认图被拉伸了一下

现象描述:
今天遇到一个比较坑的问题,用Glide加载图片被拉伸了,找了半天才发现跟Glide设置占位图有关,如果占位图要比原图大,
图片就直接被拉伸了.以下为出问题的代码:

Glide.with(mContext)     .load(imgUrl)     .placeholder(R.drawable.img_defualt)     .into(iv_show);

解决方法:
这是替换过程中一个动画阶段,Glide提供一个方法,果断 dontAnimate(), ‘0.0’不要动画了 ,修改后代码:

Glide.with(mContext)     .load(imgUrl)     .placeholder(R.drawable.img_defualt)     .dontAnimate()     .into(iv_show);

第二篇 Glide加载图片偏绿或者质量偏低

现象描述:
用Glide加载图片感觉便绿色,普通出现这种问题一般都是压缩造成的.

解决方法:
1.缓存原图 2.修改bitmap的编码(Glide默认编码为RGB_565 替换成ARGB_8888),这样一般能解决大部分问题,
(不过这样一来,Glide的加载速度自然就下降了,毕竟增加一倍的解码内存,不是有特殊需求,不要随便使用),以下为示例代码:

//1.缓存原图Glide.with(mContext)     .load(imgUrl)     .diskCacheStrategy(DiskCacheStrategy.SOURCE)     .into(iv_show);
//修改Bitmap的编码为ARGB_8888public class GlideConfig implements GlideModule {    @Override    public void applyOptions(Context context, GlideBuilder builder) {        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);    }    @Override    public void registerComponents(Context context, Glide glide) {    }}//在manifest.xml配置上以上信息        <meta-data            android:name="com.xxx.xxx.GlideConfig"            android:value="GlideModule"/>

第三篇 Glide混淆脚本没加导致的Crash

现象描述:
在加了第二篇提到的一些配置,其中GlideModule是需要在manifest中指定的,debug版本一直没事,realease版本各种Crash,报错信息:

java.lang.IllegalArgumentException: Unable to find GlideModule to find GlideModule implementation ```一开始我就想到了混淆脚本,结果遇到了两个坑:1. 网上提供的混淆脚本包名是Glide之前的包名  2. Glide内部混淆没关系,但是实现了GlideModule接口的类不能混淆,因为manifest中明确的指明了包名+类名的,混淆了自然就找不到了解决方法:    1.加上必要的混淆脚本:``` java-keep public class * implements  com.bumptech.glide.module.GlideModule-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {    **[] $VALUES;    public *;}-keep class com.bumptech.** {    *;}<div class="se-preview-section-delimiter"></div>

第四篇 Glide无法给Gone的ImageView设置图片

现象描述:
因为有一个需求,其实就是广告,位置是个ImageView,没有拉到广告之前,我设置的是Gone,拉到图片之后,设置上图片,然后设置为可见,结果广告拉到了,但是对应位置没有任何反应,图片地址是可以正确打开的,以下为示例代码:

RequestListener callback = new RequestListener() {                @Override                public boolean onException(Exception e, Object model, Target target, boolean isFirstResource) {                                      return false;                }                @Override                public boolean onResourceReady(Object resource, Object model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {                    iv.setVisibility(View.VISIBLE);                    return false;                }            };iv.setVisibility(View.GONE);//如果你的ImageViewVisibility为Gone,以上回调没有作用,改成INVISIBLE即可Glide.with(mContext).load(url)                    .listener(callback)                    .into(iv);

解决方法:
经过我测试,首先两个回调根本没有执行,网上搜到了一个国外程序员的回答

What is the size of the ImageView declared in XML? Glide needs to know how big an image the target wants. And does so using a layout listener if the size is not ready yet. Having a hidden ImageView doesn’t go through a layout to save processor cycles for obvious reasons, so it may not have a size yet.
You can try .override(w, h) to specify the size yourself or use explicit sizing (100dp).

他指出Glide需要知道我们控件的大小,我没有去试验加上override(w,h)是否有效,我把代码中的GONE改成INVISIBLE就可以了,希望以上能帮到你们

1 0