下面列出了com.bumptech.glide.GlideBuilder#setBitmapPool ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void applyOptions(Context context, GlideBuilder builder) {
//磁盘缓存
builder.setDiskCache(new DiskLruCacheFactory(context.getCacheDir().getAbsolutePath(), 50 * 1024 * 1024));
KLog.d("Glide", "glide cache file path >>> " + context.getCacheDir().getAbsolutePath());
//内存缓存
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
//设置比默认大小大1.5倍的缓存和图片池大小
int customMemoryCacheSize = (int) (1.5 * defaultMemoryCacheSize);
int customBitmapPoolSize = defaultBitmapPoolSize;
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
KLog.d("Glide", "bitmapPoolSize >>>>> " +
formatFileSize(context, customBitmapPoolSize) +
" / memorySize>>>>>>>> " +
formatFileSize(context, customMemoryCacheSize));
builder.setLogLevel(Log.ERROR);
}
@Override
public void applyOptions(final Context context, GlideBuilder builder) {
builder.setDiskCache(new DiskCache.Factory() {
@Override
public DiskCache build() {
// Careful: the external cache directory doesn't enforce permissions
return DiskLruCacheWrapper
.create(FileUtils.makeDirs(new File(Utils.getAppComponent().cacheFile(), "Glide")),
IMAGE_DISK_CACHE_MAX_SIZE);
}
});
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
//设置图片解码格式
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
//设置内存缓存大小
int maxMemory = (int) Runtime.getRuntime().maxMemory();//获取系统分配给应用的总内存大小
int memoryCacheSize = maxMemory / 8;//设置图片内存缓存占用八分之一
builder.setMemoryCache(new LruResourceCache(memoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(memoryCacheSize));
// 存放路径和缓存控件大小
int diskCacheSize = 1024 * 1024 * 30;
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheName, diskCacheSize)); // data/data/xx/cache/
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, diskCacheName, diskCacheSize)); //存放在外置文件浏览器
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDiskCache(new DiskCache.Factory() {
@Override
public DiskCache build() {
File pathIcons = new File(Configuration.PATH_CACHE_ICONS);
pathIcons.mkdirs();
return DiskLruCacheWrapper.get(pathIcons, DEFAULT_DISK_CACHE_SIZE);
}
});
final MemorySizeCalculator calculator = new MemorySizeCalculator(context);
final int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
builder.setMemoryCache(new LruResourceCache(defaultMemoryCacheSize / 2));
final int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
builder.setBitmapPool(new LruBitmapPool(defaultBitmapPoolSize / 2));
}
@Override public void applyOptions(Context context, GlideBuilder builder) {
builder.setDefaultRequestOptions(RequestOptions.formatOf(DecodeFormat.PREFER_RGB_565));
// disk cache config
//builder.setDiskCache(new ExternalCacheDiskCacheFactory(context));
// using defaults
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
// size for memory cache
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
builder.setMemoryCache(new LruResourceCache(defaultMemoryCacheSize));
// size for bitmap pool
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
builder.setBitmapPool(new LruBitmapPool(defaultBitmapPoolSize));
}
@Override
public void applyOptions(final Context context, GlideBuilder builder) {
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);// Bitmap格式转换到ARGB_8888
//内存缓存
MemorySizeCalculator memorySizeCalculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = memorySizeCalculator.getMemoryCacheSize();
int defalutBitmapPoolSize = memorySizeCalculator.getBitmapPoolSize();
builder.setMemoryCache(new LruResourceCache((int) (defalutBitmapPoolSize * 1.2)));//内部
builder.setBitmapPool(new LruBitmapPool((int) (defalutBitmapPoolSize * 1.2)));
//磁盘缓存
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, 1024 * 1024 * 10));//内部磁盘缓存
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, 10 * 1024 * 1024));//磁盘缓存到外部存储
//指定缓存目录1
String downLoadPath = Environment.getDownloadCacheDirectory().getPath();
builder.setDiskCache(new DiskLruCacheFactory(downLoadPath, defaultMemoryCacheSize));
//指定缓存目录2
builder.setDiskCache(new DiskCache.Factory() {
@Override
public DiskCache build() {
File cacheLocation = new File(FileUtils.getCacheDir(context), "GlideCache");
return DiskLruCacheWrapper.get(cacheLocation, 1024 * 1024 * 10);
}
});
}
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
final AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
builder.setDiskCache(() -> {
// Careful: the external cache directory doesn't enforce permissions
return DiskLruCacheWrapper.create(DataHelper.makeDirs(new File(appComponent.cacheFile(), "Glide")), IMAGE_DISK_CACHE_MAX_SIZE);
});
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
//将配置 Glide 的机会转交给 GlideImageLoaderStrategy,如你觉得框架提供的 GlideImageLoaderStrategy
//并不能满足自己的需求,想自定义 BaseImageLoaderStrategy,那请你最好实现 GlideAppliesOptions
//因为只有成为 GlideAppliesOptions 的实现类,这里才能调用 applyGlideOptions(),让你具有配置 Glide 的权利
BaseImageLoaderStrategy loadImgStrategy = appComponent.imageLoader().getLoadImgStrategy();
if (loadImgStrategy instanceof GlideAppliesOptions) {
((GlideAppliesOptions) loadImgStrategy).applyGlideOptions(context, builder);
}
}
@Override
public void applyOptions(Context context, GlideBuilder builder)
{
//修改内存容量和位图缓存池大小
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (MEMORY_CACHE_COUNT * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (MEMORY_CACHE_COUNT * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
//设置磁盘缓存
String diskCachePath = FCCache.getInstance().getImageCachePath();
int cacheSize = 0;
long availableSize = SdUtils.getAvailableExternalMemorySize();
if (availableSize < MAX_DISK_CACHE_SIZE)
cacheSize = (int) availableSize;
else
cacheSize = MAX_DISK_CACHE_SIZE;
builder.setDiskCache(new DiskLruCacheFactory(diskCachePath, cacheSize));
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
// 读写外部缓存目录不需要申请存储权限
File diskCacheFile = new File(context.getCacheDir(), "glide");
// 如果这个路径是一个文件
if (diskCacheFile.exists() && diskCacheFile.isFile()) {
// 执行删除操作
diskCacheFile.delete();
}
// 如果这个路径不存在
if (!diskCacheFile.exists()) {
// 创建多级目录
diskCacheFile.mkdirs();
}
builder.setDiskCache(() -> DiskLruCacheWrapper.create(diskCacheFile, IMAGE_DISK_CACHE_MAX_SIZE));
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
// 设置默认的加载占位图和加载出错图
builder.setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.image_loading).error(R.drawable.image_load_err));
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565));
int memoryCacheSizeBytes = 1024 * 1024 * 20;
builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
int bitmapPoolSizeBytes = 1024 * 1024 * 30;
builder.setBitmapPool(new LruBitmapPool(bitmapPoolSizeBytes));
int diskCacheSizeBytes = 1024 * 1024 * 100;
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// Define cache size and location
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskSize)); //Mobile disk
//builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, "cache", diskSize)); //sdcard disk
// The custom memory pool size and pictures
builder.setMemoryCache(new LruResourceCache(memorySize));
builder.setBitmapPool(new LruBitmapPool(memorySize));
// Define the image format
//builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
builder.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
builder.setDiskCache(new DiskCache.Factory() {
@Override
public DiskCache build() {
// Careful: the external cache directory doesn't enforce permissions
return DiskLruCacheWrapper.get(DataHelper.makeDirs(new File(appComponent.cacheFile(), "Glide")), IMAGE_DISK_CACHE_MAX_SIZE);
}
});
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
//将配置 Glide 的机会转交给 GlideImageLoaderStrategy,如你觉得框架提供的 GlideImageLoaderStrategy
//并不能满足自己的需求,想自定义 BaseImageLoaderStrategy,那请你最好实现 GlideAppliesOptions
//因为只有成为 GlideAppliesOptions 的实现类,这里才能调用 applyGlideOptions(),让你具有配置 Glide 的权利
BaseImageLoaderStrategy loadImgStrategy = appComponent.imageLoader().getLoadImgStrategy();
if (loadImgStrategy instanceof GlideAppliesOptions) {
((GlideAppliesOptions) loadImgStrategy).applyGlideOptions(context, builder);
}
}
@Override
public void applyOptions(final Context context, GlideBuilder builder) {
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));
ViewTarget.setTagId(R.id.glide_tag_id);
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
super.applyOptions(context, builder);
builder.setDiskCache(new DiskCache.Factory() {
@Nullable
@Override
public DiskCache build() {
// Careful: the external cache directory doesn't enforce permissions
return DiskLruCacheWrapper.get(DataHelper.makeDirs(
new File(RepositoryUtils.INSTANCE.obtainRepositoryComponent(context).cacheFile(), "Glide")),
IMAGE_DISK_CACHE_MAX_SIZE);
}
});
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context).build();
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
//将配置 Glide 的机会转交给 GlideImageLoaderStrategy,如你觉得框架提供的 GlideImageLoaderStrategy
//并不能满足自己的需求,想自定义 BaseImageLoaderStrategy,那请你最好实现 GlideAppliesOptions
//因为只有成为 GlideAppliesOptions 的实现类,这里才能调用 applyGlideOptions(),让你具有配置 Glide 的权利
BaseImageLoaderStrategy imageLoaderStrategy = ArmsUtils.INSTANCE.obtainArmsComponent(context).imageLoader().getStrategy();
if (imageLoaderStrategy instanceof GlideAppliesOptions) {
((GlideAppliesOptions) imageLoaderStrategy).applyGlideOptions(context, builder);
}
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// 指定位置在packageName/cache/glide_cache,大小为MAX_CACHE_DISK_SIZE的磁盘缓存
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, "glide_cache", ConfigConstants.MAX_CACHE_DISK_SIZE));
//指定内存缓存大小
builder.setMemoryCache(new LruResourceCache(ConfigConstants.MAX_CACHE_MEMORY_SIZE));
//全部的内存缓存用来作为图片缓存
builder.setBitmapPool(new LruBitmapPool(ConfigConstants.MAX_CACHE_MEMORY_SIZE));
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);//和Picasso配置一样
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// int deskacheize = 1024 * 1024 * 30;
int maxMemory = (int)Runtime.getRuntime().maxMemory();
int memoryCheSize = maxMemory / 8;
// builder.setDiskCache(new InternalCacheDiskCacheFactory(context, "glide", deskacheize));
builder.setDiskCache(new DiskLruCacheFactory(FileUtil.getCacheDir(),"glide", DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE));
builder.setMemoryCache(new LruResourceCache(memoryCheSize));
builder.setBitmapPool(new LruBitmapPool(memoryCheSize));
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
// memory cache
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
// disk cache
// set size & external vs. internal
int cacheSize100MegaBytes = 104857600;
builder.setDiskCache(
new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));
builder.setDiskCache(
new ExternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));
// set custom location
String downloadDirectoryPath = Environment.getDownloadCacheDirectory().getPath();
builder.setDiskCache(
new DiskLruCacheFactory(downloadDirectoryPath, cacheSize100MegaBytes));
// In case you want to specify a cache folder ("glide"):
//builder.setDiskCache(
// new DiskLruCacheFactory( downloadDirectoryPath, "glidecache", cacheSize100MegaBytes ) );
}
@Override
public void applyOptions(Context context, GlideBuilder glideBuilder) {
// 自定义内存缓存
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
glideBuilder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
glideBuilder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
// 自定义磁盘缓存
// set size & external vs. internal
int cacheSize100MegaBytes = 104857600;
glideBuilder.setDiskCache(new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));
// glideBuilder.setDiskCache(new ExternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));
// 选一个特定的存储目录
// String downloadDirectoryPath = Environment.getDownloadCacheDirectory().getPath();
// glideBuilder.setDiskCache(new DiskLruCacheFactory(downloadDirectoryPath, cacheSize100MegaBytes));
}