美文网首页
Glide自定义缓存

Glide自定义缓存

作者: white_or_black | 来源:发表于2017-12-12 15:54 被阅读0次

1.自定义GlideModule

package com.example.administrator.outeoforderpicture;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.GlideModule;

/**
* Created by $wu on 2017-12-12 上午 10:06.
* 自定义GlideModule
*/

public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {

//设置图片的类型,
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);

//获取Glide计算过得bitmap缓冲池和内存的大小(不应该随意配置)
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int bitmapPoolSize = calculator.getBitmapPoolSize();
int memorySize = calculator.getMemoryCacheSize();

//设置图片的bitmap缓存池的大小
BitmapPool bitmapPool = new LruBitmapPool(bitmapPoolSize);
builder.setBitmapPool(bitmapPool);

//设置内存缓存的空间大小
MemoryCache memorySizeCache = new LruResourceCache(memorySize);
builder.setMemoryCache(memorySizeCache);

//外部缓存(Android/包名/cache/glide_cache)
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, "glide_cache", 40 * 1024 * 1024));
//内部(私有)缓存(data/data/包名/cache/image_manager_disk_cache)
// builder.setDiskCache(new InternalCacheDiskCacheFactory(context, 10 * 1024 * 1024));
//自定义缓存
//String downloadDirectoryPath = Environment.getExternalStorageDirectory().getPath();
//builder.setDiskCache(new DiskLruCacheFactory(downloadDirectoryPath, "glide_cache", 10 * 1024 * 1024));

}

@Override
public void registerComponents(Context context, Glide glide) {

}
}

2.在AndroidManifest中声明GlideModule

<meta-data
android:name="com.example.administrator.outeoforderpicture.MyGlideModule"
android:value="GlideModule" />

3.备注:

1.getCacheDir()的目录为私有缓存目录。       (data/data/包名/cache)
2.getExternalCacheDir()目录为外部缓存目录。 (Android/data/包名/cache)
3.Environment.getExternalStorageDirectory   为外部目录的根目录。
4.Environment.getExternalStoragePublicDirectory(Enviroment.Directory_Movies)  为外部目录的movies目录

相关文章

网友评论

      本文标题:Glide自定义缓存

      本文链接:https://www.haomeiwen.com/subject/fzrnixtx.html