版权声明:本文为xing_star原创文章,转载请注明出处!

本文同步自http://javaexception.com/archives/188

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后占位图和过渡动画冲突解决方案 https://www.jianshu.com/p/28f5bcee409f

关于ImageView的几个常见问题 http://javaexception.com/archives/173

Glide处理圆形ImageView http://javaexception.com/archives/182

如何使用Glide加载通知栏头像url http://javaexception.com/archives/19

												

Glide3升级到Glide4碰到的问题汇总以及部分代码修改的更多相关文章

  1. ElasticSearch 从2.2升级到6.2.4所碰到的问题汇总

    1.ID的问题. 以前创建索引API直接用URL加索引Post过去就行了,或者在Kibana的开发工具中提交命令 PUT /customer?pretty 但是发现这样即使生成了索引,在ES中预览能看 ...

  2. 关于eclipse导工程或移植工程常碰到的错误汇总

      在开发过程中,eclipse是使用得最多的IDE,但由于其开源且免费的性质决定了其不然有很多的BUG,在项目很赶的时期碰到某些很恶的错误很浪费时间,也很让人郁闷,现我总结一下我碰到的错误并总结下对 ...

  3. Android app targetSdk升级到27碰到的一个bug补充说明

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/203 完美解决google nexus设备全面屏主题cra ...

  4. node安装升级过程中遇到的问题汇总

    一.Node already installed, it's just not linked 第一步:sudo chown -R $(whoami) $(brew --prefix)/* 第二步:br ...

  5. 全新升级的AOP框架Dora.Interception[汇总,共6篇]

    多年之前利用IL Emit写了一个名为Dora.Interception(github地址,觉得不错不妨给一颗星)的AOP框架.前几天利用Roslyn的Source Generator对自己为公司写的 ...

  6. NET MVC 升级到5.1后,View视图中代码报错

    使用nuget将项目中MVC4 升级到MVC5,之后项目还可以正常编译运行, 但View视图中相关的很多代码都报错,比如: 1.@model找不到 2.@Html找不到,本该是System.Web.M ...

  7. 升级到Angular6后对老版本的RXJS代码做相应的调整

    还没有了解过RXJS6的童鞋,可以查看我的另外一篇博文,此篇博文主要是对于RXJS5升级到RXJS6的代码调整示例 RXJS5版本 在RXJS5上我们是这样写请求的 import 'rxjs/add/ ...

  8. 前端文档汇总(含代码规范、开发流程、知识分享,持续更新) front-end-Doc

    https://juejin.im/post/5b1205b1f265da6e1a602a62 https://juejin.im/post/5b1205b1f265da6e1a602a62 http ...

  9. 纸壳CMS(ZKEACMS)体验升级,快速创建页面,直接在页面中修改内容

    关于纸壳CMS 纸壳CMS又名 ZKEACMS Core 是ZKEACMS的 .net core 版本,可运行在 .net core 1.1 平台上.是一个开源的CMS. 纸壳CMS对于 ZKEACM ...

随机推荐

  1. while 循环,运算符,字符串的格式化练习

    1.判断下列逻辑语句的结果,一定要自己先分析 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 Ture ...

  2. pytorch实现yolov3(5) 实现端到端的目标检测

    torch实现yolov3(1) torch实现yolov3(2) torch实现yolov3(3) torch实现yolov3(4) 前面4篇已经实现了network的forward,并且将netw ...

  3. error: 'commit' is not possible because you have unmerged files.

    解决方案: 1.把修改的文件add下,如:git add bidder_mod/src/common/dragon_bidder_data.cc2.git commit

  4. Linux 下载文件命令(wget)

    wget是Linux最常用的下载命令, 一般的使用方法是: wget + 空格 + 要下载文件的url路径 例如: # wget http://www.linuxsense.org/xxxx/xxx. ...

  5. Java用户程序

    Java的用户程序分为两类:Java Application和Java Applet. 这两类程序在程序结构和执行机制上有一定的差异. Java Application是完整的程序,需要独立的Java ...

  6. Oracle 统计信息介绍

      统计信息自动执行需要以下条件满足: dba_autotask_task 字段status值ENABLED dba_autotask_client 字段status值ENABLED dba_auto ...

  7. 第一章: 初识Java

    计算机程序:计算机为完成某些功能产生的一系列有序指令集合. Java技术包括:JavaSE(标准版) JavaEE(企业版) ---JavaME(移动版) 开发Java程序步骤:1.编写 2.编译 3 ...

  8. C#async/await心得

    结论: 异步方法的方法签名要加 async,否则就算返回 Task 也是普通方法. 调用异步方法,可以加 await 或不加 await,两者方式都是马上返回,不加 await 得到的是 Task 对 ...

  9. 【Git】Found a swap file by the name ".git/.MERGE_MSG.swp"

    最近合并分支的时候总是遇到这个问题,导致合并之后还需要再提交一次--有点烦-- 解决方案: 在项目根目录(如/StudioProjects/demo/Leave)下,找到 .git/.MERGE_MS ...

  10. Linux学习之自动配置部署——初用expect

    主机A连接主机B 免密登陆 + 自动部署 expect实现自动的交互式任务 ——— send 向进程发送字符串(输入) ——— expect 从进程接受字符串 ——— spawn 启动新进程 ——— ...