android – 排球和位图缓存

我正在尝试显示包含大量(远程)图像的列表视图.我正试图用凌空来完成任务.

凌空有点奏效,但还不够好.在ImageLoader.get中,volley有以下代码

final String cacheKey = getCacheKey(requestUrl,maxWidth,maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
        // Return the cached bitmap.
        ImageContainer container = new ImageContainer(cachedBitmap,requestUrl,null,null);
        imageListener.onResponse(container,true);
        return container;
    }

但是,getCacheKey会生成如下所示的键:

/**
 * Creates a cache key for use with the L1 cache.
 * @param url The URL of the request.
 * @param maxWidth The max-width of the output.
 * @param maxHeight The max-height of the output.
 */
private static String getCacheKey(String url,int maxWidth,int maxHeight) {
    return new StringBuilder(url.length() + 12).append("#W").append(maxWidth)
            .append("#H").append(maxHeight).append(url).toString();
}

即它向键附加一些“元数据”,如宽度和高度.

此密钥永远不会产生命中,如果图像不在L1缓存中,则会在线获取.当在线获取图像时,它将保存在磁盘缓存中,但Volley会将URL(仅限URL)保存为密钥.

这是预期的行为吗?我错过了什么吗?

解决方法

您没有获得任何匹配的原因是因为Volley中用于磁盘缓存的认行为取决于您请求的元素的HTTP标头(在您的情况下是图像).

Volley的工作方式是:

> ImageLoader检查L1缓存中的图像(由您提供给其构造函数中的ImageLoader的内存缓存).如果可用则返回图像.
>请求由RequestQueue处理.它检查映像的L2(磁盘缓存).
>如果在磁盘缓存中找到,请检查映像到期时间.如果没有过期,请返回.
>下载图像并将其返回.
>将图像保存在缓存中.

如果您希望认设置起作用,图像必须具有Cache-Control标头,如max-age = ???问号表示从下载时起的足够秒数.

如果你想改变认行为,我不确定,但我认为你必须稍微编辑一下代码.

查看Volley源中的Cachedispatcher类.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...