Glide 加载网络图片
1、权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、添加依赖
implementation 'com.github.bumptech.glide:glide:4.8.0'
3、加载网络图片
Glide.with(v).load(picUrl).placeholder(R.mipmap.ic_loading).error(R.mipmap.ic_load_failure).into(ivPic);
4、图片缓存
//禁止缓存
Glide.with(this).load(imageUrl).skipMemoryCache(true).into(imageView);
//清除缓存,必须在UI线程中调用
Glide.get(context).clearMemory();
//设置缓存
Glide.with(this).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
DiskCacheStrategy.ALL:缓存源资源和转换后的资源
DiskCacheStrategy.NONE:不作任何磁盘缓存
DiskCacheStrategy.SOURCE:缓存源资源
DiskCacheStrategy.RESULT:缓存转换后的资源
5、Cookies 设置
保存cookies
public class SaveCookiesInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
HashSet<String> cookies = new HashSet<>();
for (String header : originalResponse.headers("Set-Cookie")) {
cookies.add(header);
}
UserKeeper.setCookies(cookies);
}
return originalResponse;
}
}
设置 Cookies
HashSet<String> preferences = UserKeeper.getCookies();
LazyHeaders.Builder builder = new LazyHeaders.Builder();
for (String cookie : preferences) {
builder.addHeader("Cookie", cookie);
}
GlideUrl cookie = new GlideUrl(url, builder.build());
Glide.with(context).load(cookie).apply(options).into(view);
6、Gilde 设置 Tag 报错问题
//清单文件中加入
<application
android:name=".MyApplication"
......>
</application>
public class MyApplication extends Application {
@Override public void onCreate() {
super.onCreate();
ViewTarget.setTagId(R.id.glide_tag);
}
}
//创建资源文件:src/main/values/ids.xml
<resources>
<item type="id" name="glide_tag" />
</resources>
7、混淆设置
##Glide
-dontwarn com.bumptech.glide.**
-keep class com.bumptech.glide.**{*;}
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}