Example usage for android.support.v4.util LruCache LruCache

List of usage examples for android.support.v4.util LruCache LruCache

Introduction

In this page you can find the example usage for android.support.v4.util LruCache LruCache.

Prototype

public LruCache(int maxSize) 

Source Link

Usage

From source file:net.zionsoft.obadiah.model.Bible.java

@Inject
public Bible(Context context) {
    super();// ww w . j a v  a  2 s.c  om
    App.get(context).getInjectionComponent().inject(this);

    mContext = context;

    final long maxMemory = Runtime.getRuntime().maxMemory();
    mBookNameCache = new LruCache<String, List<String>>((int) (maxMemory / 16L)) {
        @Override
        protected int sizeOf(String key, List<String> texts) {
            // strings are UTF-16 encoded (with a length of one or two 16-bit code units)
            int length = 0;
            for (String text : texts)
                length += text.length() * 4;
            return length;
        }
    };
    mVerseCache = new LruCache<String, List<Verse>>((int) (maxMemory / 8L)) {
        @Override
        protected int sizeOf(String key, List<Verse> verses) {
            // each Verse contains 3 integers and 2 strings
            // strings are UTF-16 encoded (with a length of one or two 16-bit code units)
            int length = 0;
            for (Verse verse : verses)
                length += 12 + (verse.bookName.length() + verse.verseText.length()) * 4;
            return length;
        }
    };
}

From source file:com.wuman.twolevellrucache.TwoLevelLruCache.java

/**
 * Constructor for TwoLevelLruCache. Use this constructor if the second
 * level disk cache is to be enabled./*from w w  w. jav a 2  s .c om*/
 * 
 * @param directory
 *            a writable directory for the L2 disk cache.
 * @param appVersion
 * @param maxSizeMem
 *            the maximum sum of the sizes of the entries in the L1 mem
 *            cache.
 * @param maxSizeDisk
 *            the maximum number of bytes the L2 disk cache should use to
 *            store.
 * @param converter
 *            a {@code Converter} that is able to convert a byte stream to
 *            and from type {@code V}.
 * @throws IOException
 */
public TwoLevelLruCache(File directory, int appVersion, int maxSizeMem, long maxSizeDisk,
        Converter<V> converter) throws IOException {
    super();

    if (maxSizeMem >= maxSizeDisk) {
        throw new IllegalArgumentException("It makes more sense to have a larger second-level disk cache.");
    }

    if (converter == null) {
        throw new IllegalArgumentException("A converter must be submitted.");
    }

    mConverter = converter;

    mMemCache = new LruCache<String, V>(maxSizeMem) {

        @Override
        protected void entryRemoved(boolean evicted, String key, V oldValue, V newValue) {
            wrapEntryRemoved(evicted, key, oldValue, newValue);
        }

        @Override
        protected V create(String key) {
            return wrapCreate(key);
        }

        @Override
        protected int sizeOf(String key, V value) {
            return wrapSizeOf(key, value);
        }

    };
    mDiskCache = DiskLruCache.open(directory, appVersion, 1, maxSizeDisk);
}

From source file:gr.unfold.android.tsibato.images.ImageCache.java

/** Initialise the cache, providing all parameters. */
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;//from  w w  w . ja  v a2  s .  co  m

    if (mCacheParams.memoryCacheEnabled) {
        if (AppConfig.DEBUG) {
            Log.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
        }

        if (Utils.hasHoneycomb()) {
            mReusableBitmaps = new HashSet<SoftReference<Bitmap>>();
        }

        mMemoryCache = new LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) {
            @Override
            protected void entryRemoved(boolean evicted, String key, BitmapDrawable oldValue,
                    BitmapDrawable newValue) {
                if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
                    // The removed entry is a recycling drawable, so notify it that it has been removed from the memory cache
                    ((RecyclingBitmapDrawable) oldValue).setIsCached(false);
                } else {
                    // The removed entry is a standard BitmapDrawable
                    if (Utils.hasHoneycomb()) {
                        // We're running on Honeycomb or later, so add the bitmap to a SoftRefrence set for possible use with inBitmap later
                        mReusableBitmaps.add(new SoftReference<Bitmap>(oldValue.getBitmap()));
                    }
                }
            }

            @Override
            protected int sizeOf(String key, BitmapDrawable value) {
                final int bitmapSize = getBitmapSize(value) / 1024;
                return bitmapSize == 0 ? 1 : bitmapSize;
            }
        };
    }

    if (cacheParams.initDiskCacheOnCreate) {
        initDiskCache();
    }
}

From source file:fr.steren.cloudcup.ImageAdapter.java

public void setImageUrls(List<String> imageUrls) {
    this.imageUrls = imageUrls;
    imageCache = new LruCache<String, Bitmap>(100);
    downloadingImageUrls = new HashSet<String>();
}

From source file:com.floyd.diamond.IMImageCache.java

/**
 * Initialize the cache, providing all parameters.
 *
 * @param context     The context to use
 * @param cacheParams The cache parameters to initialize the cache
 *//*from ww w. j a v  a2  s  .co m*/
private void init(Context context, ImageCacheParams cacheParams) {
    // final File diskCacheDir = DiskLruCache.getDiskCacheDir(context,
    // cacheParams.uniqueName);

    // Set up disk cache
    //      if (cacheParams.diskCacheEnabled) {
    //         mDiskCache = new WxImageDiskCache(cacheParams.uniqueName);
    //      }

    // Set up memory cache
    if (cacheParams.memoryCacheEnabled) {
        mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more
             * practical for a bitmap cache
             */
            @SuppressLint("NewApi")
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // int size = bitmap.getRowBytes() * bitmap.getHeight();
                // Log.d("setting", "Bitmap size:" + size);
                int size = 0;
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
                    size = bitmap.getRowBytes() * bitmap.getHeight();
                } else {
                    size = bitmap.getByteCount();
                }
                return size;
            }
        };
    }

}

From source file:com.zlk.bigdemo.freeza.image.cache.MagicImageCache.java

/**
 * Initialize the cache, providing all parameters.
 * /*from   w  w  w .j  a  v  a2 s . co m*/
 * @param context
 *            The context to use
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
private void init(Context context, ImageCacheParams cacheParams) {

    // Set up memory cache
    if (cacheParams.memoryCacheEnabled) {
        mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more
             * practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                int size = bitmap.getRowBytes() * bitmap.getHeight();
                // WxLog.d("setting", "Bitmap size:" + size);
                return size;
            }
        };
    }
}

From source file:com.android.volley.cache.BitmapImageCache.java

/**
 * Initialize the cache.//from  ww  w. j  a  v a  2 s  .c  om
 */
private void init(int memCacheSize) {
    // Set up memory cache
    VolleyLog.d(TAG, "Memory cache created (size = " + memCacheSize + "KB)");
    mMemoryCache = new LruCache<String, Bitmap>(memCacheSize) {
        /**
         * Measure item size in kilobytes rather than units which is more practical
         * for a bitmap cache
         */
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            final int bitmapSize = getBitmapSize(bitmap) / 1024;
            return bitmapSize == 0 ? 1 : bitmapSize;
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
            super.entryRemoved(evicted, key, oldValue, newValue);
            VolleyLog.d(TAG, "Memory cache entry removed - " + key);
        }
    };
}

From source file:com.digipom.manteresting.android.service.cache.CacheService.java

@Override
public void onCreate() {
    if (LoggerConfig.canLog(Log.VERBOSE)) {
        Log.v(TAG, "onCreate()");
    }//from  w ww .  j  a  va  2 s  . co m

    final int memoryClass = tryGetLargeMemoryClass(
            ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)));

    imageDownloader = new ImageDownloader();

    // 25% of memory allocated to the bitmap memory cache
    bitmapMemoryCache = new BitmapMemoryCache((memoryClass * 1024 * 1024) / 4);

    // 12% of memory allocated to the image memory cache
    imageMemoryCache = new LruCache<String, ImageWithCategory>((memoryClass * 1024 * 1024) / 8) {
        @Override
        protected int sizeOf(String key, ImageWithCategory value) {
            return value != null && value.image != null ? value.image.length : 0;
        }
    };

    initializePrimaryFileCache();
    initializeSecondaryFileCache();
}

From source file:com.android.volley.cache.plus.BitmapImageCache.java

/**
 * Initialize the cache./*from   w w  w. j a  va 2 s.  c  om*/
 */
private void init(int memCacheSize) {
    // Set up memory cache
    VolleyLog.d(TAG, "Memory cache created (size = " + memCacheSize + "KB)");
    mMemoryCache = new LruCache<String, BitmapDrawable>(memCacheSize) {
        /**
         * Measure item size in kilobytes rather than units which is more practical
         * for a bitmap cache
         */
        @Override
        protected int sizeOf(String key, BitmapDrawable bitmap) {
            final int bitmapSize = getBitmapSize(bitmap) / 1024;
            return bitmapSize == 0 ? 1 : bitmapSize;
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, BitmapDrawable oldValue,
                BitmapDrawable newValue) {
            super.entryRemoved(evicted, key, oldValue, newValue);

            VolleyLog.d(TAG, "Memory cache entry removed - " + key);
            if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
                // The removed entry is a recycling drawable, so notify it 
                // that it has been removed from the memory cache
                ((RecyclingBitmapDrawable) oldValue).setIsCached(false);
            }
        }
    };
}

From source file:com.concentricsky.android.khanacademy.util.ThumbnailManager.java

private LruCache<Thumbnail, Bitmap> prepareCache() {
    // Total available heap size. This changes based on manifest android:largeHeap="true". (Fire HD, transformer both go from 48MB to 256MB)
    Runtime rt = Runtime.getRuntime();
    long maxMemory = rt.maxMemory();
    Log.v(LOG_TAG, "maxMemory:" + Long.toString(maxMemory));

    // Want to use at most about 1/2 of available memory for thumbs.
    // In SAT Math category (116 videos), with a heap size of 48MB, this setting
    // allows 109 thumbs to be cached resulting in total heap usage around 34MB.
    long usableMemory = maxMemory / 2;

    return new LruCache<Thumbnail, Bitmap>((int) usableMemory) {
        @Override//  w  ww  .  j a va  2 s .  c o m
        protected int sizeOf(Thumbnail key, Bitmap value) {
            return value.getByteCount();
        }

        @Override
        protected void entryRemoved(boolean evicted, Thumbnail key, Bitmap oldValue, Bitmap newValue) {
            if (oldValue != newValue) {
                oldValue.recycle();
            }
        }
    };

}