您的位置:首页 > 理论基础 > 计算机网络

glide缓存

2016-06-23 14:05 309 查看
1,缓存清理(子线程)
Glide.get(this).clearDiskCache();
Glide.get(this).clearMemory();

2,内存缓存
.skipMemoryCache( true )
跳过内存缓存。这意味着不会把图像在内存缓存中。重要的是要理解,这只会影响内存缓存!滑翔仍将利用磁盘高速缓存,
以避免另一个网络请求。
还好知道滑翔将所有图像资源默认缓存到内存中。因此,skipMemoryCache(false)不是必需的。

3,磁盘缓存
.diskCacheStrategy()
如果你一个图像,坐在相同的URL,但正在迅速改变,你可能希望禁用磁盘缓存。
你可以改变磁盘缓存.diskCacheStrategy()方法。与.skipMemoryCache()方法,它接受一个enum与不同的值,而不是一个简单的布尔。
如果你想禁用磁盘缓存请求时,使用DiskCacheStrategy枚举值。
Glide  

    .with( context )

    .load( eatFoodyImages[0] )

    .diskCacheStrategy( DiskCacheStrategy.NONE )

    .skipMemoryCache( true )

    .into( imageViewInternet );

   

    DiskCacheStrategy.NONE 没有缓存
DiskCacheStrategy.SOURCE 缓存只有原来的全分辨率图像
DiskCacheStrategy.RESULT 缓存只有最终的图像,在降低分辨率(也可能是转换)
DiskCacheStrategy.ALL 缓存所有

4,自定义缓存,使用了Glide Modules
1》计算默认缓存大小
MemorySizeCalculator calculator = new MemorySizeCalculator(context);  
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();  
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();  
2》自定义缓存大小
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);  
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);  
3》实现GlideModule接口
public class CustomCachingGlideModule implements GlideModule {  

    @Override public void applyOptions(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 );//设置图片池

       

       

        int cacheSize100MegaBytes = 104857600;

        builder.setDiskCache(

            new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes)//设置磁盘缓存

        );

       

        String downloadDirectoryPath = Environment.getDownloadCacheDirectory().getPath(); 
builder.setDiskCache(  

        new DiskLruCacheFactory( downloadDirectoryPath, cacheSize100MegaBytes )//设置磁盘缓存以及缓存路径
);

    }

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

        // nothing to do here

    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息