caching images with LruCache

这一生的挚爱 提交于 2020-03-19 19:39:28

问题


I am trying to use LruCache in android to cache some images, but its not caching

here is the code

   int cacheSize1 = 4 * 1024 * 1024; // 4MiB
    bitmapCache = new LruCache(cacheSize1) {
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();

    }};

here its other methods

    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        bitmapCache.put(key, bitmap);
    }
}
    public Bitmap getBitmapFromMemCache(String key) {
    Bitmap b = (Bitmap)bitmapCache.get(key);
    return b;
}

here I am using them this is my code

for (int i = 0; i < HomeActivity.globalObj.categoriesList.size(); i++) {
            ImageView iv = new ImageView(getApplicationContext());
            Bitmap bb = getBitmapFromMemCache(HomeActivity.globalObj.categoriesList.get(i).name);
            if (bb != null) {
                iv.setImageBitmap(bb);
                imageViewList.add(iv);
            }
            else{
            Bitmap b = getImageBitmap(HomeActivity.globalObj.categoriesList.get(i).large_image);
            addBitmapToMemoryCache(HomeActivity.globalObj.categoriesList.get(i).name, b);
            iv.setImageBitmap(b);
            imageViewList.add(iv);

            }

        }

回答1:


Refer this project, it's a Google I/O sample project to explain how to use LRU Cache.



来源:https://stackoverflow.com/questions/11716214/caching-images-with-lrucache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!