Glide3升级到Glide4碰到的问题汇总以及部分代码修改
版权声明:本文为xing_star原创文章,转载请注明出处!
本文同步自
glide.3x的版本是3.7.0,glide4.x的版本是4.2.0
glide3.x中最基础的用法
glide.with(getactivity()).load(url).into(imageview)
那么在glide4.x中,其实还是一样的,最基本的用法不变
glide.with(context).load(url).into(imageview)
但是稍微复杂一点的用法就有很大的差异了,接下来我们一一列举。
接下来看一个稍微常规点的复杂用法
glide.with(baseapplication.getappcontext()) .load(url) .placeholder(r.drawable.xxx) .crossfade() .into(imageview);
升级到glide4后,更新为了
drawablecrossfadefactory drawablecrossfadefactory = new drawablecrossfadefactory.builder().setcrossfadeenabled(true).build(); glide.with(baseapplication.getappcontext()) .load(url) .apply(new requestoptions().placeholder(r.drawable.xxx)) .transition(drawabletransitionoptions.with(drawablecrossfadefactory)) .into(imageview);
glide3.x的链式调用,glide4.x的用法还是比较繁琐的
接下来记录踩得第一个坑
glide4.0后占位图和过渡动画冲突
在实际使用过程中发现升级到glide4之后,展位图跟过渡动画存在冲突,最后找到解决办法,glide4加载url的代码调整为了
drawablecrossfadefactory drawablecrossfadefactory = new drawablecrossfadefactory.builder().setcrossfadeenabled(true).build(); glide.with(baseapplication.getappcontext()) .load(url) .apply(new requestoptions().placeholder(r.drawable.xxx)) .transition(drawabletransitionoptions.with(drawablecrossfadefactory)) .into(imageview);
关键点在于
setcrossfadeenabled(true)
淡入淡出动画效果
其实跟上面的一样,glide3.x中
glide.with(baseapplication.getappcontext()) .load(url) .crossfade() .placeholder(r.drawable.xxx) .into(imageview);
用法是这样的
到glide4.x中
drawablecrossfadefactory drawablecrossfadefactory = new drawablecrossfadefactory.builder().setcrossfadeenabled(true).build(); glide.with(baseapplication.getappcontext()) .load(url) .transition(drawabletransitionoptions.with(drawablecrossfadefactory)) .apply(new requestoptions().placeholder(r.drawable.post)) .into(imageview);
需要使用的是transition方法以及apply方法。apply方法里面可以用来设置placeholder,errorr,centercrop等方法。这个地方跟glide3.x是不一样的,用起来可能会觉得别扭。
预加载问题
glide3.x是
glide.with(baseapplication.getappcontext()) .load(url) .diskcachestrategy(diskcachestrategy.source);
升级到glide4.x之后,有所调整,用的是preload方法
glide.with(baseapplication.getappcontext()) .load(url) .preload();
自定义bitmaptransformation
升级后有几个方法发生了变更, 在我们自定义bitmaptransformation实现一些特定的圆角等需求中,glide3.x中只需要实现getid方法, 而在glide4.x中,需要重写equals方法,以及hashcode方法,还有updatediskcachekey。
我们以glideroundtransform为例,看看两个版本的细微差异:
glide3.x的代码如下:
public class glideroundtransform extends bitmaptransformation { private static float radius = 0f; public glideroundtransform(context context) { this(context, 4); } public glideroundtransform(context context, int dp) { super(context); this.radius = displayutils.dip2px(dp); } @override protected bitmap transform(bitmappool pool, bitmap totransform, int outwidth, int outheight) { return roundcrop(pool, totransform); } private static bitmap roundcrop(bitmappool pool, bitmap source) { if (source == null) return null; bitmap result = pool.get(source.getwidth(), source.getheight(), bitmap.config.argb_8888); if (result == null) { result = bitmap.createbitmap(source.getwidth(), source.getheight(), bitmap.config.argb_8888); } canvas canvas = new canvas(result); paint paint = new paint(); paint.setshader(new bitmapshader(source, bitmapshader.tilemode.clamp, bitmapshader.tilemode.clamp)); paint.setantialias(true); rectf rectf = new rectf(0f, 0f, source.getwidth(), source.getheight()); canvas.drawroundrect(rectf, radius, radius, paint); return result; } @override public string getid() { return getclass().getname() + math.round(radius); } }
public class glideroundtransform extends bitmaptransformation { private static final string id = "com.star.wall.glide.glideroundtransform"; private float radius = 0f; public glideroundtransform(context context) { this(context, 4); } public glideroundtransform(context context, int dp) { super(context); this.radius = displayutils.dip2px(dp); } @override protected bitmap transform(bitmappool pool, bitmap totransform, int outwidth, int outheight) { return roundcrop(pool, totransform); } private bitmap roundcrop(bitmappool pool, bitmap source) { if (source == null) return null; bitmap result = pool.get(source.getwidth(), source.getheight(), bitmap.config.argb_8888); if (result == null) { result = bitmap.createbitmap(source.getwidth(), source.getheight(), bitmap.config.argb_8888); } canvas canvas = new canvas(result); paint paint = new paint(); paint.setshader(new bitmapshader(source, bitmapshader.tilemode.clamp, bitmapshader.tilemode.clamp)); paint.setantialias(true); rectf rectf = new rectf(0f, 0f, source.getwidth(), source.getheight()); canvas.drawroundrect(rectf, radius, radius, paint); return result; } @override public boolean equals(object o) { if (o instanceof glideroundtransform) { glideroundtransform other = (glideroundtransform) o; return radius == other.radius; } return false; } @override public int hashcode() { return (id + "_" + radius).hashcode(); } @override public void updatediskcachekey(messagedigest messagedigest) { messagedigest.update((id + "_" + radius).getbytes()); } }
如果还有其他的自定义transform需求,可以参考上面的代码作为模板,进行调整。
对于只支持设置imageview.setimagedrawable方法的view
加载url的代码glide3.x中是
glide.with(this) .load(url) .into(new simpletarget<glidedrawable>() { @override public void onresourceready(glidedrawable resource, glideanimation<? super glidedrawable> glideanimation) { stvinfo.setlefticon(resource); } });
glide4.x中是
glide.with(this) .load(url) .into(new simpletarget<drawable>() { @override public void onresourceready(drawable resource, transition<? super drawable> transition) { stvinfo.setlefticon(resource); } });
这一块的关键点是simpletarget,通过实现这个抽象类的特定方法,我们可以获取到drawable,拿到了drawable就可以给imageview设置图片源了,glide3.x和glide4.x的区别在于一个是glidedrawable,一个是drawable.
同步代码中,获取bitmap
在glide3.x中
bitmap bitmap = glide.with(baseapplication.getappcontext()) .load(url).asbitmap() .into(target.size_original, target.size_original) .get();
asbitmap后,调用get()方法,就能够获取到bitmap了,而在glide4.x中,还得调整下代码。
bitmap bitmap = glide.with(baseapplication.getappcontext()).asbitmap().load(url) .apply(new requestoptions().override(target.size_original, target.size_original)).submit().get();
可以观察下,这两个差异还挺大的,glide4.x里面是先asbitmap,再load(url),还有就是通过submit().get()的方式获取到bitmap
包含centercrop,thumbnail,placeholder,error等常用方法的例子
glide3中是
glide.with(this) .load(url) .centercrop() .thumbnail(0.1f) .placeholder(r.drawable.icon_pic_default) .error(r.drawable.icon_pic_default) .into(imageview);
而glide4中是
glide.with(this) .load(url) .apply(new requestoptions().centercrop().placeholder(r.drawable.icon_pic_default).error(r.drawable.icon_pic_default)) .thumbnail(0.1f) .into(imageview);
未完待续。
补充资料:
glide4.0后占位图和过渡动画冲突解决方案
关于imageview的几个常见问题
glide处理圆形imageview
如何使用glide加载通知栏头像url