Glide自定义缓存
程序员文章站
2022-05-19 15:31:02
...
依赖
implementation 'com.github.bumptech.glide:glide:4.9.0’annotationProcessor ‘com.github.bumptech.glide:compiler:4.9.0’
//创建一个glide的类
在适配器里写
Glide.with(context).load(list.get(i).pic).diskCacheStrategy(DiskCacheStrategy.ALL).into(myViewHolder.imageView);```
package com.example.www.lifangxian20190520;
import android.content.Context;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.Log;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
@GlideModule
public final class MyGlide extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// 设置缓存大小为20mb
int memoryCacheSizeBytes = 1024 * 1024 * 20; // 20mb
// 设置内存缓存大小
builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
Log.i("dt",context.getCacheDir().getAbsolutePath());
// 根据SD卡是否可用选择是在内部缓存还是SD卡缓存
//去文件管理器android/data/报名/自定义的文件名/缓存文件
ExternalPreferredCacheDiskCacheFactory cacheDiskCacheFactory = new ExternalPreferredCacheDiskCacheFactory(context, "ShopCar", memoryCacheSizeBytes);
builder.setDiskCache(cacheDiskCacheFactory);
}
// 针对V4用户可以提升速度
@Override
public boolean isManifestParsingEnabled() {
return false;
}
}