缓存图像与LruCache

缓存图像与LruCache

本文介绍了缓存图像与LruCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用LruCache在android系统缓存一些图像,但它不是缓存

这里是code

  INT cacheSize1 = 4 * 1024 * 1024; // 4MiB
    bitmapCache =新LruCache(cacheSize1){
        保护INT整型尺寸(字符串键,位值){
            返回value.getRowBytes()* value.getHeight();    }};

在这里的其他方法

 公共无效addBitmapToMemoryCache(字符串键,位图位图){
    如果(getBitmapFromMemCache(键)== NULL){
        bitmapCache.put(键,位图);
    }
}
    公共位图getBitmapFromMemCache(字符串键){
    位图B =(位图)bitmapCache.get(密钥);
    返回b;
}

在这里我使用它们,这是我的code

 的for(int i = 0; I< HomeActivity.globalObj.categoriesList.size();我++){
            ImageView的IV =新ImageView的(getApplicationContext());
            位图BB = getBitmapFromMemCache(HomeActivity.globalObj.categoriesList.get(I)。名称);
            如果(BB!= NULL){
                iv.setImageBitmap(BB);
                imageViewList.add(四);
            }
            其他{
            位图B = getImageBitmap(HomeActivity.globalObj.categoriesList.get㈠.large_image);
            addBitmapToMemoryCache(HomeActivity.globalObj.categoriesList.get(我).name和B);
            iv.setImageBitmap(二);
            imageViewList.add(四);            }        }


解决方案

转寄此,这是一个谷歌I / O示例项目讲解了​​如何使用 LRU缓存

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);

            }

        }
解决方案

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

这篇关于缓存图像与LruCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:11