List of usage examples for android.support.v4.util LruCache LruCache
public LruCache(int maxSize)
From source file:com.example.xyzreader.cp7.ArticleListFragment.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) @Override//from w ww . j a v a 2 s. co m protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount() / 1024; } else { return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } } }; setListAdapter(new MyAdapter()); setHasOptionsMenu(true); }
From source file:com.example.xyzreader.cp8.ArticleListFragment.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @TargetApi(12)/*from w ww . ja v a 2 s . co m*/ @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount() / 1024; } else { return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } } }; setListAdapter(new MyAdapter()); setHasOptionsMenu(true); }
From source file:com.gokuai.yunkuandroidsdk.imageutils.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///from w ww . j ava2 s . c om private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // Set up memory cache if (mCacheParams.memoryCacheEnabled) { // If we're running on Honeycomb or newer, then if (Utils.hasHoneycomb()) { mReusableBitmaps = Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>()); } mMemoryCache = new LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) { /** * Notify the removed entry that is no longer being cached */ @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())); } } } /** * Measure item size in kilobytes rather than units which is more practical * for a bitmap cache */ @Override protected int sizeOf(String key, BitmapDrawable value) { final int bitmapSize = getBitmapSize(value) / 1024; return bitmapSize == 0 ? 1 : bitmapSize; } }; } // By default the disk cache is not initialized here as it should be initialized // on a separate thread due to disk access. if (cacheParams.initDiskCacheOnCreate) { // Set up disk cache initDiskCache(); } }
From source file:android.support.v17.leanback.widget.ViewsStateBundle.java
protected void applyPolicyChanges() { if (mSavePolicy == SAVE_LIMITED_CHILD) { if (mLimitNumber <= 0) { throw new IllegalArgumentException(); }/*from w w w . j a v a 2 s .c o m*/ if (mChildStates == null || mChildStates.maxSize() != mLimitNumber) { mChildStates = new LruCache<String, SparseArray<Parcelable>>(mLimitNumber); } } else if (mSavePolicy == SAVE_ALL_CHILD || mSavePolicy == SAVE_ON_SCREEN_CHILD) { if (mChildStates == null || mChildStates.maxSize() != UNLIMITED) { mChildStates = new LruCache<String, SparseArray<Parcelable>>(UNLIMITED); } } else { mChildStates = null; } }
From source file:com.friedran.appengine.dashboard.gui.DashboardLoadFragment.java
private LruCache<String, Bitmap> initChartsMemoryCache() { final int maxMemoryInKBs = (int) (Runtime.getRuntime().maxMemory() / 1024); final int cacheSize = maxMemoryInKBs / 5; return new LruCache<String, Bitmap>(cacheSize) { @Override/*w ww . j a v a2 s.c o m*/ protected int sizeOf(String key, Bitmap bitmap) { // Measured in kilobytes return bitmap.getByteCount() / 1024; } }; }
From source file:com.soft.pushsender.DemoActivity.java
private void initCache() { if (mMemoryCache == null) { final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override//from ww w .j a v a 2 s . c o m protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return bitmap.getByteCount() / 1024; } }; } }
From source file:com.ape.transfer.util.FileIconLoader.java
/** * Constructor./* w w w .jav a 2s.com*/ * * @param context content context */ public FileIconLoader(Context context) { mContext = context; final ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)); final float cacheSizeAdjustment = (am.isLowRamDevice()) ? 0.5f : 1.0f; final int holderCacheSize = (int) (cacheSizeAdjustment * HOLDER_CACHE_SIZE); mImageCache = new LruCache<Object, ImageHolder>(holderCacheSize); mThumbnailSize = context.getResources().getDimensionPixelSize(R.dimen.icon_width_height); }
From source file:com.example.com.jglx.android.app.util.video.ImageCache.java
/** * Initialize the cache, providing all parameters. * //w w w. ja v a2 s .c o m * @param cacheParams * The cache parameters to initialize the cache */ private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // BEGIN_INCLUDE(init_memory_cache) // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (Utils.hasHoneycomb()) { mReusableBitmaps = Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>()); } mMemoryCache = new LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) { /** * Notify the removed entry that is no longer being cached */ @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 SoftReference set for possible use with // inBitmap later mReusableBitmaps.add(new SoftReference<Bitmap>(oldValue.getBitmap())); } } } /** * Measure item size in kilobytes rather than units which is * more practical for a bitmap cache */ @Override protected int sizeOf(String key, BitmapDrawable value) { final int bitmapSize = getBitmapSize(value) / 1024; return bitmapSize == 0 ? 1 : bitmapSize; } }; } }
From source file:com.fanchen.aisou.imagefetcher.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param context The context to use// www .jav a 2 s . co m * @param cacheParams The cache parameters to initialize the cache */ private void init(Context context, ImageCacheParams cacheParams) { final File diskCacheDir = DiskLruCache.getDiskCacheDir(context, cacheParams.uniqueName); // Set up disk cache if (cacheParams.diskCacheEnabled) { mDiskCache = DiskLruCache.openCache(context, diskCacheDir, cacheParams.diskCacheSize); mDiskCache.setCompressParams(cacheParams.compressFormat, cacheParams.compressQuality); if (cacheParams.clearDiskCacheOnStart) { mDiskCache.clearCache(); } } // 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) { return AppUtil.getBitmapSize(bitmap); } }; } }
From source file:org.nativescript.widgets.image.Cache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *//*from w w w . j ava 2 s .c om*/ private void init(CacheParams cacheParams) { clearCache(); if (mReusableBitmaps != null) { mReusableBitmaps.clear(); mReusableBitmaps = null; } mParams = cacheParams; // Set up memory cache if (mParams.memoryCacheEnabled) { if (Worker.debuggable > 0) { Log.v(TAG, "Memory cache created (size = " + mParams.memCacheSize + ")"); } // If we're running on Honeycomb or newer, create a set of reusable bitmaps that can be // populated into the inBitmap field of BitmapFactory.Options. Note that the set is // of SoftReferences which will actually not be very effective due to the garbage // collector being aggressive clearing Soft/WeakReferences. A better approach // would be to use a strongly references bitmaps, however this would require some // balancing of memory usage between this set and the bitmap LruCache. It would also // require knowledge of the expected size of the bitmaps. From Honeycomb to JellyBean // the size would need to be precise, from KitKat onward the size would just need to // be the upper bound (due to changes in how inBitmap can re-use bitmaps). if (Utils.hasHoneycomb()) { mReusableBitmaps = Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>()); } mMemoryCacheUsage = new HashMap<String, Integer>(); mMemoryCache = new LruCache<String, Bitmap>(mParams.memCacheSize) { /** * Notify the removed entry that is no longer being cached */ @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { Integer count = mMemoryCacheUsage.get(key); if (Utils.hasHoneycomb() && (count == null || count == 0)) { // We're running on Honeycomb or later, so add the bitmap // to a SoftReference set for possible use with inBitmap later mReusableBitmaps.add(new SoftReference<Bitmap>(oldValue)); } } /** * Measure item size in kilobytes rather than units which is more practical * for a bitmap cache */ @Override protected int sizeOf(String key, Bitmap value) { final int bitmapSize = getBitmapSize(value) / 1024; return bitmapSize == 0 ? 1 : bitmapSize; } }; } }