如何使用动态图像URI缓存图像?

乔戈皮娃

如何设置Android Universal Image Loader以加载动态图像URI?

例如:

两个URI必须代表相同的远程映像image.jpg

参考:

有时,您可能不希望将图像URL用作缓存键,因为URL的一部分是动态的(即:出于访问控制目的)

SDWebImage-使用缓存密钥过滤器

我在iOS应用程序中使用SDWebImage,并且确实需要类似的功能才能在其Android版本中使用UIL。

我们的13

我认为您可以使用此内存缓存装饰器:

public class CustomMemoryCache implements MemoryCacheAware<String, Bitmap> {

    private final MemoryCacheAware<String, Bitmap> cache;

    public CustomMemoryCache(MemoryCacheAware<String, Bitmap> cache) {
        this.cache = cache;
    }

    @Override
    public boolean put(String key, Bitmap value) {
        return cache.put(cleanKey(key), value);
    }

    @Override
    public Bitmap get(String key) {
        return cache.get(cleanKey(key));
    }

    @Override
    public void remove(String key) {
        cache.remove(cleanKey(key));
    }

    @Override
    public Collection<String> keys() {
        return cache.keys();
    }

    @Override
    public void clear() {
        cache.clear();
    }

    private String cleanKey(String key) {
        return key.substring(0, key.lastIndexOf("?")) + 
                 key.substring(key.lastIndexOf("_")); 
            // original cache key is like "[imageUri]_[width]x[height]"
    }
}

然后包装任何就绪的内存缓存实现,并将其设置为配置。例如:

LruMemoryCache memoryCache = new LruMemoryCache(memoryCacheSize);
CustomMemoryCache memoryCacheDecorator = new CustomMemoryCache(memoryCache);

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
    ...
    .memoryCache(memoryCacheDecorator)
    ...
    .build();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章