四、Glide4的使用:
基本用法:
1、在app/build.gradle中添加如下依赖:
dependencies {
implementation 'com.github.bumptech.glide:glide:4.4.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'
}
2、在权限声明中需要添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
3.加载图片:(先With,再load,再into)
String url = "http://guolin.tech/book.png";
Glide.with(this).load(url).into(imageView);
特殊用法:
1、占位图:(在使用的时候很可能看不到占位图,因为Glide有非常强大的缓存机制,下载过的图片Glide会自动把它缓存下来,所以加载的时候直接读取,比较快)同时可以指定其图片大小。
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.error) //表示error时的占位图
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
2、Glide的缓存分为两种形式:
内存缓存:防止应用重复将图片数据读取到内存中
硬盘缓存:防止应用重复从网络或者其他地方重复下载和读取数据
3、可指定加载格式:(gif等支持)
Glide.with(this)
.load("http://guolin.tech/test.gif")
.into(imageView);
网友评论