Search in sources :

Example 1 with OkHttpNetworkFetcher

use of com.facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher in project DevRing by LJYcoder.

the class FrescoManager method init.

@Override
public void init(Context context, final ImageConfig imageConfig) {
    mContext = context;
    mImageConfig = imageConfig;
    // 配置管理者
    ImagePipelineConfig.Builder imagePipelineConfigBuilder = ImagePipelineConfig.newBuilder(context);
    // 配置内存缓存(已解码部分的缓存)
    if (imageConfig.getMemoryCacheSize() > 0) {
        imagePipelineConfigBuilder.setBitmapMemoryCacheParamsSupplier(new Supplier<MemoryCacheParams>() {

            public MemoryCacheParams get() {
                MemoryCacheParams bitmapCacheParams = new // 
                MemoryCacheParams(// 可用最大内存数,以字节为单位
                imageConfig.getMemoryCacheSize(), // 内存中允许的最多图片数量
                Integer.MAX_VALUE, // 内存中准备清理但是尚未删除的总图片所可用的最大内存数,以字节为单位
                imageConfig.getMemoryCacheSize(), // 内存中准备清除的图片最大数量
                Integer.MAX_VALUE, // 内存中单图片的最大大小
                Integer.MAX_VALUE);
                return bitmapCacheParams;
            }
        });
    }
    // 配置磁盘缓存
    if (imageConfig.getDiskCacheFile() != null) {
        DiskCacheConfig.Builder diakBuilder = DiskCacheConfig.newBuilder(context);
        // 磁盘缓存目录路径
        diakBuilder.setBaseDirectoryPath(imageConfig.getDiskCacheFile().getParentFile());
        // 磁盘缓存目录名
        diakBuilder.setBaseDirectoryName(imageConfig.getDiskCacheFile().getName());
        // 磁盘缓存大小
        if (imageConfig.getDiskCacheSize() > 0)
            diakBuilder.setMaxCacheSize(imageConfig.getDiskCacheSize());
        imagePipelineConfigBuilder.setMainDiskCacheConfig(diakBuilder.build());
    } else if (imageConfig.isDiskCacheExternal()) {
        DiskCacheConfig.Builder diakBuilder = DiskCacheConfig.newBuilder(context);
        // 磁盘缓存目录路径
        diakBuilder.setBaseDirectoryPath(context.getExternalCacheDir());
        // 磁盘缓存目录名
        diakBuilder.setBaseDirectoryName("fresco_image_cache");
        // 磁盘缓存大小
        if (imageConfig.getDiskCacheSize() > 0)
            diakBuilder.setMaxCacheSize(imageConfig.getDiskCacheSize());
        imagePipelineConfigBuilder.setMainDiskCacheConfig(diakBuilder.build());
    }
    if (imageConfig.isUseOkhttp())
        imagePipelineConfigBuilder.setNetworkFetcher(new OkHttpNetworkFetcher(DevRing.ringComponent().okHttpClient()));
    // 当内存紧张时采取的措施
    MemoryTrimmableRegistry memoryTrimmableRegistry = NoOpMemoryTrimmableRegistry.getInstance();
    memoryTrimmableRegistry.registerMemoryTrimmable(new MemoryTrimmable() {

        @Override
        public void trim(MemoryTrimType trimType) {
            final double suggestedTrimRatio = trimType.getSuggestedTrimRatio();
            RingLog.e(String.format("onCreate suggestedTrimRatio : %f", suggestedTrimRatio));
            if (MemoryTrimType.OnCloseToDalvikHeapLimit.getSuggestedTrimRatio() == suggestedTrimRatio || MemoryTrimType.OnSystemLowMemoryWhileAppInBackground.getSuggestedTrimRatio() == suggestedTrimRatio || MemoryTrimType.OnSystemLowMemoryWhileAppInForeground.getSuggestedTrimRatio() == suggestedTrimRatio) {
                ImagePipelineFactory.getInstance().getImagePipeline().clearMemoryCaches();
            }
        }
    });
    imagePipelineConfigBuilder.setMemoryTrimmableRegistry(memoryTrimmableRegistry);
    /**
     * 在图片解码时根据ResizeOptions所设的宽高的像素进行解码,这样解码出来可以得到一个更小的Bitmap。
     * 必须和ImageRequest的ResizeOptions一起使用,ResizeOptions和DownsampleEnabled参数都不影响原图片的大小,影响的是EncodeImage的大小,
     * 进而影响Decode出来的Bitmap的大小,ResizeOptions须和此参数结合使用,
     * 是因为单独使用ResizeOptions的话只支持JPEG图,所以需支持png、jpg、webp需要先设置此参数。
     */
    imagePipelineConfigBuilder.setDownsampleEnabled(true);
    // 配置渐进式显示(使用默认效果),仅支持文件类型为JPEG的网络图片
    imagePipelineConfigBuilder.setProgressiveJpegConfig(new SimpleProgressiveJpegConfig());
    // 设置调试时,显示图片加载的Log
    FLog.setMinimumLoggingLevel(FLog.VERBOSE);
    Set<RequestListener> requestListeners = new HashSet<>();
    requestListeners.add(new RequestLoggingListener());
    imagePipelineConfigBuilder.setRequestListeners(requestListeners);
    // 进行初始化
    Fresco.initialize(context, imagePipelineConfigBuilder.build());
}
Also used : MemoryTrimType(com.facebook.common.memory.MemoryTrimType) RequestListener(com.facebook.imagepipeline.listener.RequestListener) NoOpMemoryTrimmableRegistry(com.facebook.common.memory.NoOpMemoryTrimmableRegistry) MemoryTrimmableRegistry(com.facebook.common.memory.MemoryTrimmableRegistry) ImagePipelineConfig(com.facebook.imagepipeline.core.ImagePipelineConfig) PipelineDraweeControllerBuilder(com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder) ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder) RequestLoggingListener(com.facebook.imagepipeline.listener.RequestLoggingListener) MemoryTrimmable(com.facebook.common.memory.MemoryTrimmable) DiskCacheConfig(com.facebook.cache.disk.DiskCacheConfig) SimpleProgressiveJpegConfig(com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig) MemoryCacheParams(com.facebook.imagepipeline.cache.MemoryCacheParams) OkHttpNetworkFetcher(com.facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher) HashSet(java.util.HashSet)

Aggregations

DiskCacheConfig (com.facebook.cache.disk.DiskCacheConfig)1 MemoryTrimType (com.facebook.common.memory.MemoryTrimType)1 MemoryTrimmable (com.facebook.common.memory.MemoryTrimmable)1 MemoryTrimmableRegistry (com.facebook.common.memory.MemoryTrimmableRegistry)1 NoOpMemoryTrimmableRegistry (com.facebook.common.memory.NoOpMemoryTrimmableRegistry)1 PipelineDraweeControllerBuilder (com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder)1 OkHttpNetworkFetcher (com.facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher)1 MemoryCacheParams (com.facebook.imagepipeline.cache.MemoryCacheParams)1 ImagePipelineConfig (com.facebook.imagepipeline.core.ImagePipelineConfig)1 SimpleProgressiveJpegConfig (com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig)1 RequestListener (com.facebook.imagepipeline.listener.RequestListener)1 RequestLoggingListener (com.facebook.imagepipeline.listener.RequestLoggingListener)1 ImageRequestBuilder (com.facebook.imagepipeline.request.ImageRequestBuilder)1 HashSet (java.util.HashSet)1