Search in sources :

Example 1 with SimpleProgressiveJpegConfig

use of com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig 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)

Example 2 with SimpleProgressiveJpegConfig

use of com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig in project fresco by facebook.

the class DecodeProducerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    mProgressiveJpegConfig = new SimpleProgressiveJpegConfig(new SimpleProgressiveJpegConfig.DynamicValueConfig() {

        public List<Integer> getScansToDecode() {
            return Arrays.asList(PREVIEW_SCAN, GOOD_ENOUGH_SCAN);
        }

        public int getGoodEnoughScanNumber() {
            return GOOD_ENOUGH_SCAN;
        }
    });
    PowerMockito.mockStatic(ProgressiveJpegParser.class);
    PowerMockito.whenNew(ProgressiveJpegParser.class).withAnyArguments().thenReturn(mProgressiveJpegParser);
    PowerMockito.mockStatic(JobScheduler.class);
    PowerMockito.whenNew(JobScheduler.class).withAnyArguments().thenReturn(mJobScheduler);
    when(mConfig.getExperiments()).thenReturn(mPipelineExperiments);
    mDecodeProducer = new DecodeProducer(mByteArrayPool, mExecutor, mImageDecoder, mProgressiveJpegConfig, false, /* Set downsampleEnabled to false */
    false, /* Set resizeAndRotateForNetwork to false */
    false, /* We don't cancel when the request is cancelled */
    mInputProducer, MAX_BITMAP_SIZE, new CloseableReferenceFactory(new NoOpCloseableReferenceLeakTracker()), null, Suppliers.BOOLEAN_FALSE);
    PooledByteBuffer pooledByteBuffer = mockPooledByteBuffer(IMAGE_SIZE);
    mByteBufferRef = CloseableReference.of(pooledByteBuffer);
    mEncodedImage = new EncodedImage(mByteBufferRef);
    mEncodedImage.setImageFormat(DefaultImageFormats.JPEG);
    mEncodedImage.setWidth(IMAGE_WIDTH);
    mEncodedImage.setHeight(IMAGE_HEIGHT);
    mEncodedImage.setRotationAngle(IMAGE_ROTATION_ANGLE);
    mEncodedImage.setExifOrientation(IMAGE_EXIF_ORIENTATION);
}
Also used : SimpleProgressiveJpegConfig(com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig) NoOpCloseableReferenceLeakTracker(com.facebook.imagepipeline.debug.NoOpCloseableReferenceLeakTracker) CloseableReferenceFactory(com.facebook.imagepipeline.core.CloseableReferenceFactory) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Aggregations

SimpleProgressiveJpegConfig (com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig)2 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 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)1 PipelineDraweeControllerBuilder (com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder)1 OkHttpNetworkFetcher (com.facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher)1 MemoryCacheParams (com.facebook.imagepipeline.cache.MemoryCacheParams)1 CloseableReferenceFactory (com.facebook.imagepipeline.core.CloseableReferenceFactory)1 ImagePipelineConfig (com.facebook.imagepipeline.core.ImagePipelineConfig)1 NoOpCloseableReferenceLeakTracker (com.facebook.imagepipeline.debug.NoOpCloseableReferenceLeakTracker)1 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)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