Search in sources :

Example 1 with DiskCache

use of com.bumptech.glide.load.engine.cache.DiskCache in project MVPFrames by RockyQu.

the class GlideConfiguration method applyOptions.

@Override
public void applyOptions(Context context, GlideBuilder builder) {
    final AppComponent component = AppUtils.obtainAppComponentFromContext(context);
    builder.setDiskCache(new DiskCache.Factory() {

        @Override
        public DiskCache build() {
            // Careful: the external cache directory doesn't enforce permissions
            return DiskLruCacheWrapper.get(FileUtils.makeDirs(new File(component.getCacheFile(), "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));
    // 图片框架最终会走到 GlideImageLoader 的 load 方法中进行处理
    // 如果你想自己自定义图片框架或自己实现 Glide BaseImageLoader 你需要实现 GlideAppliesOptions 接口
    BaseImageLoader baseImageLoader = component.getImageLoader().getImageLoader();
    if (baseImageLoader instanceof GlideAppliesOptions) {
        ((GlideAppliesOptions) baseImageLoader).applyGlideOptions(context, builder);
    }
}
Also used : BaseImageLoader(com.tool.common.widget.imageloader.BaseImageLoader) AppComponent(com.tool.common.di.component.AppComponent) MemorySizeCalculator(com.bumptech.glide.load.engine.cache.MemorySizeCalculator) LruBitmapPool(com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool) LruResourceCache(com.bumptech.glide.load.engine.cache.LruResourceCache) DiskCache(com.bumptech.glide.load.engine.cache.DiskCache) File(java.io.File)

Example 2 with DiskCache

use of com.bumptech.glide.load.engine.cache.DiskCache in project glide by bumptech.

the class ExternallyClearedDiskCacheTest method loadFromCache_afterDiskCacheDeleted_doesNotFail.

@Test
public void loadFromCache_afterDiskCacheDeleted_doesNotFail() {
    final DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024);
    cache.get(mock(Key.class));
    deleteRecursively(cacheDir);
    Glide.init(context, new GlideBuilder().setDiskCache(new Factory() {

        @Override
        public DiskCache build() {
            return cache;
        }
    }));
    Drawable drawable = concurrency.get(Glide.with(context).load(raw.canonical).submit());
    assertThat(drawable).isNotNull();
}
Also used : Drawable(android.graphics.drawable.Drawable) Factory(com.bumptech.glide.load.engine.cache.DiskCache.Factory) DiskCache(com.bumptech.glide.load.engine.cache.DiskCache) Key(com.bumptech.glide.load.Key) Test(org.junit.Test)

Example 3 with DiskCache

use of com.bumptech.glide.load.engine.cache.DiskCache in project glide by bumptech.

the class ExternallyClearedDiskCacheTest method get_afterDeleteDirectoryOutsideGlideAndClose_doesNotThrow.

@Test
public void get_afterDeleteDirectoryOutsideGlideAndClose_doesNotThrow() {
    DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024);
    cache.get(mock(Key.class));
    deleteRecursively(cacheDir);
    cache.clear();
    cache.get(mock(Key.class));
}
Also used : DiskCache(com.bumptech.glide.load.engine.cache.DiskCache) Key(com.bumptech.glide.load.Key) Test(org.junit.Test)

Example 4 with DiskCache

use of com.bumptech.glide.load.engine.cache.DiskCache in project MVPArms by JessYanCoding.

the class GlideConfiguration method applyOptions.

@Override
public void applyOptions(Context context, GlideBuilder builder) {
    builder.setDiskCache(new DiskCache.Factory() {

        @Override
        public DiskCache build() {
            // Careful: the external cache directory doesn't enforce permissions
            File cacheDirectory = new File(DataHelper.getCacheFile(UiUtils.getContext()), "Glide");
            return DiskLruCacheWrapper.get(DataHelper.makeDirs(cacheDirectory), IMAGE_DISK_CACHE_MAX_SIZE);
        }
    });
    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));
}
Also used : MemorySizeCalculator(com.bumptech.glide.load.engine.cache.MemorySizeCalculator) LruBitmapPool(com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool) LruResourceCache(com.bumptech.glide.load.engine.cache.LruResourceCache) DiskCache(com.bumptech.glide.load.engine.cache.DiskCache) File(java.io.File)

Example 5 with DiskCache

use of com.bumptech.glide.load.engine.cache.DiskCache in project glide by bumptech.

the class ExternallyClearedDiskCacheTest method loadFromCache_afterDiskCacheDeletedAndCleared_doesNotFail.

@Test
public void loadFromCache_afterDiskCacheDeletedAndCleared_doesNotFail() {
    final DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024);
    cache.get(mock(Key.class));
    deleteRecursively(cacheDir);
    cache.clear();
    Glide.init(context, new GlideBuilder().setDiskCache(new Factory() {

        @Override
        public DiskCache build() {
            return cache;
        }
    }));
    Drawable drawable = concurrency.get(Glide.with(context).load(ResourceIds.raw.canonical).submit());
    assertThat(drawable).isNotNull();
}
Also used : Drawable(android.graphics.drawable.Drawable) Factory(com.bumptech.glide.load.engine.cache.DiskCache.Factory) DiskCache(com.bumptech.glide.load.engine.cache.DiskCache) Key(com.bumptech.glide.load.Key) Test(org.junit.Test)

Aggregations

DiskCache (com.bumptech.glide.load.engine.cache.DiskCache)7 Key (com.bumptech.glide.load.Key)4 Test (org.junit.Test)4 LruResourceCache (com.bumptech.glide.load.engine.cache.LruResourceCache)3 Drawable (android.graphics.drawable.Drawable)2 LruBitmapPool (com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool)2 Factory (com.bumptech.glide.load.engine.cache.DiskCache.Factory)2 MemorySizeCalculator (com.bumptech.glide.load.engine.cache.MemorySizeCalculator)2 File (java.io.File)2 AppComponent (com.tool.common.di.component.AppComponent)1 BaseImageLoader (com.tool.common.widget.imageloader.BaseImageLoader)1