基本用法:和Glide3一样
Glide.with(this).load(url).into(img);
设置占位图、缓存等参数:
RequestOptions options = new ReqeustOptions()
.placeholder(R,mipmap.loading)
.error(R.mipmap.error)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true);
Glide.with(this).load(url).apply(otions).into(img);
注意:1、Glide4增加了:
DiskCacheStrategy.AUTOMATIC: 表示让Glide根据图片资源智能地选择使用哪一种缓存策略(默认选项)。
2、指定加载的类型,比如asGif(),以前在load()后调用,现在是在load()之前调用
回调与监听:
1、into()传入自定义的target,和Glide3一样的用法:
SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
imageView.setImageDrawable(resource);
}
};
public void loadImage(View view) {
Glide.with(this)
.load("https://www.baidu.com/img/bd_logo1.png")
.into(simpleTarget);
}
2、preload()预加载:
Glide.with(this)
.load("https://www.baidu.com/img/bd_logo1.png")
.preload();
Glide.with(this)
.load("https://www.baidu.com/img/bd_logo1.png")
.into(imageView);
3、submit()对应着Glide3的downloadOnly():有两个方法重载:
submit():下载原始尺寸的图片
submit(int width, int height):指定下载图片的尺寸
4、listener()
Glide.with(this).load(gif_url).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
}).into(img);
使用Generated APIGenerated API是Glide 4中全新引入的一个功能,它的工作原理是使用注解处理器 (Annotation Processor) 来生成出一个API,在Application模块中可使用该流式API一次性调用到RequestBuilder,
RequestOptions和集成库中所有的选项。
简单的说就是和Glide3一模一样的流式接口。