Fresco库,自定义图片缓存的key

来源:互联网 发布:linux malloc实现原理 编辑:程序博客网 时间:2024/06/06 08:23

Fresco库,自定义图片缓存的key

重写CacheKeyFactory类,重写里面的3个方法,将return方法里的key替换成你生成你需要的key。不过自定的key也是需要遵循Fresco库里的Urls规则,网络图片还是需要加上”http://”前缀啦。

public static class MyCacheKeyFactory implements CacheKeyFactory {    @Override    public CacheKey getBitmapCacheKey(ImageRequest request) {        Uri uri = request.getSourceUri();        String key = mCacheStr;        return new BitmapMemoryCacheKey(                key,                request.getResizeOptions(),                request.getAutoRotateEnabled(),                request.getImageDecodeOptions(),                null,                null);    }    @Override    public CacheKey getPostprocessedBitmapCacheKey(ImageRequest request) {        Uri uri = request.getSourceUri();        String key = mCacheStr;        final Postprocessor postprocessor = request.getPostprocessor();        final CacheKey postprocessorCacheKey;        final String postprocessorName;        if (postprocessor != null) {            postprocessorCacheKey = postprocessor.getPostprocessorCacheKey();            postprocessorName = postprocessor.getClass().getName();        } else {            postprocessorCacheKey = null;            postprocessorName = null;        }        return new BitmapMemoryCacheKey(                key,                request.getResizeOptions(),                request.getAutoRotateEnabled(),                request.getImageDecodeOptions(),                postprocessorCacheKey,                postprocessorName);    }    @Override    public CacheKey getEncodedCacheKey(ImageRequest request) {        Uri uri = request.getSourceUri();        String key = mCacheStr;        return new SimpleCacheKey(key);    }}

初始化fresco时将CacheKeyFactory设置为重写的对象

    //初始化,一般要放在application    ImagePipelineConfig config = ImagePipelineConfig.newBuilder(MainActivity.this)            .setCacheKeyFactory(myCacheKeyFactory)            .build();    Fresco.initialize(MainActivity.this, config);

提供Demo 下载

该例子是针对 compile ‘com.facebook.fresco:fresco:0.10.0’
版本做的处理。

提醒一下0.10.0版本开始如果要加载gif图片,需要另外倒入 compile ‘com.facebook.fresco:animated-gif:0.10.0’
包,因为该版本开始,将gif功能拆分成另外一个模块包了

0 0
原创粉丝点击