List of usage examples for android.support.v4.util LruCache LruCache
public LruCache(int maxSize)
From source file:info.awesomedevelopment.tvgrid.library.TVGridView.java
@SuppressWarnings("deprecation") private void init(AttributeSet attrs) { ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); mCache = new LruCache<>(am.getMemoryClass() * 1024); mYSize = new ValueAnimator(); mXSize = new ValueAnimator(); mYLocation = new ValueAnimator(); mXLocation = new ValueAnimator(); mXSize.addUpdateListener(xSizeListener); mYSize.addUpdateListener(ySizeListener); mYLocation.addUpdateListener(yLocationListener); mXLocation.addUpdateListener(xLocationListener); mSelectorAnimationSet.playTogether(mXLocation, mYLocation, mXSize, mYSize); mSelectorAnimationSet.setInterpolator(new AccelerateDecelerateInterpolator()); mSelectorAnimationSet.setDuration(ANIMATION_DURATION); TypedValue fillAlpha = new TypedValue(); getResources().getValue(R.dimen.tvg_defFillAlpha, fillAlpha, true); TypedValue fillAlphaSelected = new TypedValue(); getResources().getValue(R.dimen.tvg_defFillAlphaSelected, fillAlphaSelected, true); if (attrs != null) { TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.TVGridView, 0, 0); try {/*from w ww .j a va 2 s. c o m*/ //noinspection ResourceType mStrokePosition = a.getInteger(R.styleable.TVGridView_tvg_strokePosition, OUTSIDE); //noinspection ResourceType mSelectorPosition = a.getInteger(R.styleable.TVGridView_tvg_selectorPosition, OVER); //noinspection ResourceType mSelectorShape = a.getInteger(R.styleable.TVGridView_tvg_selectorShape, RECTANGLE); mAnimateSelectorChanges = a.getBoolean(R.styleable.TVGridView_tvg_animateSelectorChanges, getResources().getInteger(R.integer.tvg_defAnimateSelectorChanges) == 1); mIsFilled = a.getBoolean(R.styleable.TVGridView_tvg_filled, getResources().getInteger(R.integer.tvg_defIsFilled) == 1); mFillAlpha = a.getFloat(R.styleable.TVGridView_tvg_fillAlpha, fillAlpha.getFloat()); mFillAlphaSelected = a.getFloat(R.styleable.TVGridView_tvg_fillAlphaSelected, fillAlphaSelected.getFloat()); mFillColor = a.getColor(R.styleable.TVGridView_tvg_fillColor, getResources().getColor(R.color.tvg_defFillColor)); mFillColorSelected = a.getColor(R.styleable.TVGridView_tvg_fillColorSelected, getResources().getColor(R.color.tvg_defFillColorSelected)); mCornerRadiusX = a.getDimension(R.styleable.TVGridView_tvg_cornerRadius, getResources().getDimension(R.dimen.tvg_defCornerRadius)); mCornerRadiusY = a.getDimension(R.styleable.TVGridView_tvg_cornerRadius, getResources().getDimension(R.dimen.tvg_defCornerRadius)); mStrokeWidth = a.getDimension(R.styleable.TVGridView_tvg_strokeWidth, getResources().getDimension(R.dimen.tvg_defStrokeWidth)); mStrokeColor = a.getColor(R.styleable.TVGridView_tvg_strokeColor, getResources().getColor(R.color.tvg_defStrokeColor)); mStrokeColorSelected = a.getColor(R.styleable.TVGridView_tvg_strokeColorSelected, getResources().getColor(R.color.tvg_defStrokeColorSelected)); mStrokeMarginLeft = a.getDimension(R.styleable.TVGridView_tvg_marginLeft, getResources().getDimension(R.dimen.tvg_defStrokeMarginLeft)); mStrokeMarginTop = a.getDimension(R.styleable.TVGridView_tvg_marginTop, getResources().getDimension(R.dimen.tvg_defStrokeMarginTop)); mStrokeMarginRight = a.getDimension(R.styleable.TVGridView_tvg_marginRight, getResources().getDimension(R.dimen.tvg_defStrokeMarginRight)); mStrokeMarginBottom = a.getDimension(R.styleable.TVGridView_tvg_marginBottom, getResources().getDimension(R.dimen.tvg_defStrokeMarginBottom)); mStrokeSpacingLeft = a.getDimension(R.styleable.TVGridView_tvg_spacingLeft, getResources().getDimension(R.dimen.tvg_defStrokeSpacingLeft)); mStrokeSpacingTop = a.getDimension(R.styleable.TVGridView_tvg_spacingTop, getResources().getDimension(R.dimen.tvg_defStrokeSpacingTop)); mStrokeSpacingRight = a.getDimension(R.styleable.TVGridView_tvg_spacingRight, getResources().getDimension(R.dimen.tvg_defStrokeSpacingRight)); mStrokeSpacingBottom = a.getDimension(R.styleable.TVGridView_tvg_spacingBottom, getResources().getDimension(R.dimen.tvg_defStrokeSpacingBottom)); } finally { a.recycle(); } } else { mStrokePosition = OUTSIDE; mSelectorPosition = OVER; mSelectorShape = RECTANGLE; mAnimateSelectorChanges = getResources().getInteger(R.integer.tvg_defAnimateSelectorChanges) == 1; mIsFilled = getResources().getInteger(R.integer.tvg_defIsFilled) == 1; mFillAlpha = fillAlpha.getFloat(); mFillAlphaSelected = fillAlphaSelected.getFloat(); mFillColor = getResources().getColor(R.color.tvg_defFillColor); mFillColorSelected = getResources().getColor(R.color.tvg_defFillColorSelected); mCornerRadiusX = getResources().getDimension(R.dimen.tvg_defCornerRadius); mCornerRadiusY = getResources().getDimension(R.dimen.tvg_defCornerRadius); mStrokeWidth = getResources().getDimension(R.dimen.tvg_defStrokeWidth); mStrokeColor = getResources().getColor(R.color.tvg_defStrokeColor); mStrokeColorSelected = getResources().getColor(R.color.tvg_defStrokeColorSelected); mStrokeMarginLeft = getResources().getDimension(R.dimen.tvg_defStrokeMarginLeft); mStrokeMarginTop = getResources().getDimension(R.dimen.tvg_defStrokeMarginTop); mStrokeMarginRight = getResources().getDimension(R.dimen.tvg_defStrokeMarginRight); mStrokeMarginBottom = getResources().getDimension(R.dimen.tvg_defStrokeMarginBottom); mStrokeSpacingLeft = getResources().getDimension(R.dimen.tvg_defStrokeSpacingLeft); mStrokeSpacingTop = getResources().getDimension(R.dimen.tvg_defStrokeSpacingTop); mStrokeSpacingRight = getResources().getDimension(R.dimen.tvg_defStrokeSpacingRight); mStrokeSpacingBottom = getResources().getDimension(R.dimen.tvg_defStrokeSpacingBottom); } addOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == SCROLL_STATE_IDLE) { mEdgeChange = false; mHardScrollChange = false; } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); mScrollY = mScrollY + dy; if (mStrokeCellCurrentBounds == null || mStrokeCell == null) return; if (useAnimations()) { mSelectorAnimationSet.cancel(); mStrokeCellCurrentBounds.offsetTo(mStrokeCellCurrentBounds.left - dx, mStrokeCellCurrentBounds.top - dy); performSelectorAnimation(); } else if (mHardScrollChange || mEdgeChange) { mStrokeCellCurrentBounds.offsetTo(mStrokeCellCurrentBounds.left - dx, mStrokeCellCurrentBounds.top - dy); setPrevBounds(); mStrokeCell.setBounds(mStrokeCellPrevBounds); invalidate(); } } }); setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { clearHighlightedView(); return false; } }); }
From source file:com.comic.lazyupload.image.PhotoManager.java
/** * Constructs the work queues and thread pools used to download and decode * images./*w w w . j a v a 2s .co m*/ */ private PhotoManager() { /* * Creates a work queue for the pool of Thread objects used for * downloading, using a linked list queue that blocks when the queue is * empty. */ mDownloadWorkQueue = new LinkedBlockingQueue<Runnable>(); /* * Creates a work queue for the pool of Thread objects used for * decoding, using a linked list queue that blocks when the queue is * empty. */ mDecodeWorkQueue = new LinkedBlockingQueue<Runnable>(); /* * Creates a work queue for the set of of task objects that control * downloading and decoding, using a linked list queue that blocks when * the queue is empty. */ mPhotoTaskWorkQueue = new LinkedBlockingQueue<PhotoTask>(); /* * Creates a new pool of Thread objects for the download work queue */ mDownloadThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, mDownloadWorkQueue); /* * Creates a new pool of Thread objects for the decoding work queue */ mDecodeThreadPool = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, mDecodeWorkQueue); // Instantiates a new cache based on the cache size estimate mPhotoCache = new LruCache<URL, byte[]>(IMAGE_CACHE_SIZE) { /* * This overrides the default sizeOf() implementation to return the * correct size of each cache entry. */ @Override protected int sizeOf(URL paramURL, byte[] paramArrayOfByte) { return paramArrayOfByte.length; } }; /* * Instantiates a new anonymous Handler object and defines its * handleMessage() method. The Handler *must* run on the UI thread, * because it moves photo Bitmaps from the PhotoTask object to the View * object. To force the Handler to run on the UI thread, it's defined as * part of the PhotoManager constructor. The constructor is invoked when * the class is first referenced, and that happens when the View invokes * startDownload. Since the View runs on the UI Thread, so does the * constructor and the Handler. */ mHandler = new Handler(Looper.getMainLooper()) { /* * handleMessage() defines the operations to perform when the * Handler receives a new Message to process. */ @Override public void handleMessage(Message inputMessage) { // Gets the image task from the incoming Message object. PhotoTask photoTask = (PhotoTask) inputMessage.obj; // Sets an PhotoView that's a weak reference to the // input ImageView PhotoView localView = photoTask.getPhotoView(); // If this input view isn't null if (localView != null) { /* * Gets the URL of the *weak reference* to the input * ImageView. The weak reference won't have changed, even if * the input ImageView has. */ URL localURL = localView.getLocation(); /* * Compares the URL of the input ImageView to the URL of the * weak reference. Only updates the bitmap in the ImageView * if this particular Thread is supposed to be serving the * ImageView. */ int downloadingImageId = localView.getCustomDownloadingImage(); int failImageId = localView.getCustomFailImage(); if (photoTask.getImageURL() == localURL) /* * Chooses the action to take, based on the incoming * message */ switch (inputMessage.what) { // If the download has started, sets background color to // dark green case DOWNLOAD_STARTED: Loge.i("DOWNLOAD_STARTED"); localView.setStatusResource(downloadingImageId); break; /* * If the download is complete, but the decode is * waiting, sets the background color to golden yellow */ case DOWNLOAD_COMPLETE: // Sets background color to golden yellow Loge.i("DOWNLOAD_COMPLETE"); localView.setStatusResource(downloadingImageId); break; // If the decode has started, sets background color to // orange case DECODE_STARTED: Loge.i("DECODE_STARTED"); localView.setStatusResource(downloadingImageId); break; /* * The decoding is done, so this sets the ImageView's * bitmap to the bitmap in the incoming message */ case TASK_COMPLETE: Loge.i("TASK_COMPLETE"); localView.setImageBitmap(photoTask.getImage()); recycleTask(photoTask); break; // The download failed, sets the background color to // dark red case DOWNLOAD_FAILED: Loge.i("DOWNLOAD_FAILED"); localView.setStatusResource(downloadingImageId); // Attempts to re-use the Task object recycleTask(photoTask); break; default: // Otherwise, calls the super method super.handleMessage(inputMessage); } } } }; }
From source file:com.android.ex.chips.RecipientAlternatesAdapter.java
public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId, int queryMode, OnCheckedItemChangedListener listener) { super(context, getCursorForConstruction(context, contactId, queryMode), 0); mContentResolver = context.getContentResolver(); mHandler = new Handler(); mPhotoCacheMap = new LruCache<Uri, byte[]>(10); mLayoutInflater = LayoutInflater.from(context); mCurrentId = currentId;/* w w w. ja v a2s .c o m*/ mCheckedItemChangedListener = listener; if (queryMode == QUERY_TYPE_EMAIL) { mQuery = Queries.EMAIL; } else if (queryMode == QUERY_TYPE_PHONE) { mQuery = Queries.PHONE; } else { mQuery = Queries.EMAIL; Log.e(TAG, "Unsupported query type: " + queryMode); } }
From source file:com.hataskrau.utils.image.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///from w w w . j ava 2 s.com private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; //BEGIN_INCLUDE(init_memory_cache) // Set up memory cache if (mCacheParams.memoryCacheEnabled) { // 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). 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 // 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; } }; } //END_INCLUDE(init_memory_cache) // 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:imageUtils.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *//* w ww. j a v a 2s.c om*/ private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (BuildConfig.DEBUG) { Log.d(TAG, "Memory cache created (size = " + mCacheParams.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). 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 } } /** * Measure item size in kilobytes rather than units which is more practical * for a bitmap cache */ }; } // 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:com.amachikhin.vkmachikhin.utils.image_cache.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///w ww.jav a2 s .c o m private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; //BEGIN_INCLUDE(init_memory_cache) // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (BuildConfig.DEBUG) { Log.d(TAG, "Memory cache created (size = " + mCacheParams.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). 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) { // The removed entry is a standard BitmapDrawable // 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; } }; } //END_INCLUDE(init_memory_cache) // 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:com.egoists.coco_nut.android.cache.ImageCache.java
/** * Initialize the cache, providing all parameters. * /* www . java 2s .c o m*/ * @param context The context to use * @param cacheParams The cache parameters to initialize the cache */ private void init(Context context, ImageCacheParams cacheParams) { mCacheParams = cacheParams; final File diskCacheDir = getDiskCacheDir(context, cacheParams.uniqueName); if (cacheParams.diskCacheEnabled) { if (!diskCacheDir.exists()) { diskCacheDir.mkdir(); } if (getUsableSpace(diskCacheDir) > cacheParams.diskCacheSize) { try { mICSDiskCache = ICSDiskLruCache.open(diskCacheDir, 1, 1, cacheParams.diskCacheSize); } catch (final IOException e) { LOGE(TAG, "init - " + e); } } } // 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 getBitmapSize(bitmap); } }; } }
From source file:com.bluetech.gallery5.util.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *//*from w ww . j ava 2s .c o m*/ private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; //BEGIN_INCLUDE(init_memory_cache) // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (BuildConfig.DEBUG) { Log.d(TAG, "Memory cache created (size = " + mCacheParams.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). 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 // 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; } }; } //END_INCLUDE(init_memory_cache) // 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:at.fjp.cards.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///from w w w .j av a 2 s . c om private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; //BEGIN_INCLUDE(init_memory_cache) // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (BuildConfig.DEBUG) { Log.d(TAG, "Memory cache created (size = " + mCacheParams.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). 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 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; } }; } //END_INCLUDE(init_memory_cache) // 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:com.view.bitmapfun.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///from w ww. ja v a2s . com private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (DEBUG) { Log.d(TAG, "Memory cache created (size = " + mCacheParams.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>>()); } 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; } }; } // 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(); } }