Search in sources :

Example 1 with AppComponent

use of com.jess.arms.di.component.AppComponent in project MVPArms by JessYanCoding.

the class GlideConfiguration method registerComponents.

@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    // Glide 默认使用 HttpURLConnection 做网络请求,在这切换成 Okhttp 请求
    AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
    registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(appComponent.okHttpClient()));
    BaseImageLoaderStrategy loadImgStrategy = appComponent.imageLoader().getLoadImgStrategy();
    if (loadImgStrategy instanceof GlideAppliesOptions) {
        ((GlideAppliesOptions) loadImgStrategy).registerComponents(context, glide, registry);
    }
}
Also used : OkHttpUrlLoader(com.jess.arms.http.OkHttpUrlLoader) BaseImageLoaderStrategy(com.jess.arms.http.imageloader.BaseImageLoaderStrategy) AppComponent(com.jess.arms.di.component.AppComponent)

Example 2 with AppComponent

use of com.jess.arms.di.component.AppComponent in project MVPArms by JessYanCoding.

the class GlideConfiguration method applyOptions.

@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);
    }
}
Also used : BaseImageLoaderStrategy(com.jess.arms.http.imageloader.BaseImageLoaderStrategy) AppComponent(com.jess.arms.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) File(java.io.File)

Aggregations

AppComponent (com.jess.arms.di.component.AppComponent)2 BaseImageLoaderStrategy (com.jess.arms.http.imageloader.BaseImageLoaderStrategy)2 LruBitmapPool (com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool)1 LruResourceCache (com.bumptech.glide.load.engine.cache.LruResourceCache)1 MemorySizeCalculator (com.bumptech.glide.load.engine.cache.MemorySizeCalculator)1 OkHttpUrlLoader (com.jess.arms.http.OkHttpUrlLoader)1 File (java.io.File)1