List of usage examples for android.graphics.drawable BitmapDrawable getBitmap
public final Bitmap getBitmap()
From source file:com.chanlytech.unicorn.cache.ImageCache.java
/** * Adds a bitmap to both memory and disk cache. * * @param data/*from w w w . ja va 2s.c o m*/ * Unique identifier for the bitmap to store * @param value * The bitmap drawable to store */ public void addBitmapToCache(String data, BitmapDrawable value) { //BEGIN_INCLUDE(add_bitmap_to_cache) if (data == null || value == null) { return; } // Add to memory cache if (mMemoryCache != null) { if (RecyclingBitmapDrawable.class.isInstance(value)) { // The removed entry is a recycling drawable, so notify it // that it has been added into the memory cache ((RecyclingBitmapDrawable) value).setIsCached(true); } mMemoryCache.put(data, value); } synchronized (mDiskCacheLock) { // Add to disk cache if (mDiskLruCache != null) { final String key = hashKeyForDisk(data); OutputStream out = null; try { // DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); // if (snapshot == null) // { final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); value.getBitmap().compress(mCacheParams.COMPRESS_FORMAT, mCacheParams.COMPRESS_QUALITY, out); editor.commit(); out.close(); } // } // else // { // snapshot.getInputStream(DISK_CACHE_INDEX).close(); // } } catch (Exception e) { UinLog.e(TAG, UinLog.getPrintException(e)); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { } } } } //END_INCLUDE(add_bitmap_to_cache) }
From source file:com.higgses.griffin.cache.ImageCache.java
/** * Adds a bitmap to both memory and disk cache. * * @param data//from w ww . ja v a2 s. c o m * Unique identifier for the bitmap to store * @param value * The bitmap drawable to store */ public void addBitmapToCache(String data, BitmapDrawable value) { //BEGIN_INCLUDE(add_bitmap_to_cache) if (data == null || value == null) { return; } // Add to memory cache if (mMemoryCache != null) { if (RecyclingBitmapDrawable.class.isInstance(value)) { // The removed entry is a recycling drawable, so notify it // that it has been added into the memory cache ((RecyclingBitmapDrawable) value).setIsCached(true); } mMemoryCache.put(data, value); } synchronized (mDiskCacheLock) { // Add to disk cache if (mDiskLruCache != null) { final String key = hashKeyForDisk(data); OutputStream out = null; try { // DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); // if (snapshot == null) // { final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); value.getBitmap().compress(mCacheParams.COMPRESS_FORMAT, mCacheParams.COMPRESS_QUALITY, out); editor.commit(); out.close(); } // } // else // { // snapshot.getInputStream(DISK_CACHE_INDEX).close(); // } } catch (Exception e) { GinLog.e(TAG, GinLog.getPrintException(e)); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { } } } } //END_INCLUDE(add_bitmap_to_cache) }
From source file:com.common.library.bitmap.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///from w w w .j a v a 2 s . co 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). if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // 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.travel.ac.utils.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams/*from www.j av a2s. co m*/ * The cache parameters to initialize the cache */ 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). 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(); } }
From source file:com.nf2m.util.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///from ww w . j a v a 2 s .c om private void init(@NonNull 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 UriObserver 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 UriObserver 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, @NonNull BitmapDrawable oldValue, BitmapDrawable newValue) { if (RecyclingBitmapDrawable.class.isInstance(oldValue)) { // The removed entry is UriObserver recycling drawable, so notify it // that it has been removed from the memory cache ((RecyclingBitmapDrawable) oldValue).setIsCached(false); } else { // The removed entry is UriObserver standard BitmapDrawable if (Utils.hasHoneycomb()) { // We're running on Honeycomb or later, so add the bitmap // to UriObserver 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 UriObserver bitmap cache */ @Override protected int sizeOf(String key, @NonNull 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 UriObserver separate thread due to disk access. if (cacheParams.initDiskCacheOnCreate) { // Set up disk cache initDiskCache(); } }
From source file:com.cloudsynch.quickshare.utils.thumbnail.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams//w w w . ja v a 2s. c o m * The cache parameters to initialize the cache */ private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (LogUtil.DEBUG) { Log.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")"); } // If we're running on Honeycomb or newer, then if (Utils.hasHoneycomb()) { mReusableBitmaps = 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:com.jasper.image.imagemanager.imagehelper.imageEngine.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *//* ww w . 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). if (VersionUtils.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 (VersionUtils.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; } }; } // 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.ifeng.util.imagecache.ImageWorker.java
/** * Load an image specified by the data parameter into an ImageView (override * {@link ImageWorker#processBitmap(Object)} to define the processing * logic). A memory and disk cache will be used if an {@link ImageCache} has * been added using// ww w .j ava 2s. c om * {@link ImageWorker#addImageCache(FragmentManager, ImageCache.ImageCacheParams)} * . If the image is found in the memory cache, it is set immediately, * otherwise an {@link AsyncTask} will be created to asynchronously load the * bitmap. * * @param item * The loadtask of the image to download. * @param imageView * The ImageView to bind the downloaded image to. */ final protected void loadImageInternal(BitmapTaskItem item, ImageView imageView) { if (item == null || item.mDataSource == null || imageView == null) { return; } // ? if (item.mDataSource instanceof String && TextUtils.isEmpty((CharSequence) item.mDataSource)) { return; } // resId?s if (item.mDataSource instanceof Integer && (Integer) item.mDataSource == 0) { return; } BitmapDrawable value = null; if (mImageCache != null) { value = mImageCache.getBitmapFromMemCache(String.valueOf(item.mDataSource)); } if (value != null) { // Bitmap found in memory cache // Bug fix by XuWei 2013-09-09 // Drawable?bug?ViewDrawable??Drawable Drawable copyDrawable = new BitmapDrawable(mResources, value.getBitmap()); imageView.setImageDrawable(copyDrawable); } else if (cancelPotentialWork(item.mDataSource, imageView)) { byte[] chunk = null; if (mLoadingBitmap != null) { chunk = mLoadingBitmap.getNinePatchChunk(); } final BitmapWorkerTask task = new BitmapWorkerTask(imageView); if (chunk == null) { final AsyncBitmapDrawable asyncBitmapDrawable = new AsyncBitmapDrawable(mResources, mLoadingBitmap, task); imageView.setImageDrawable(asyncBitmapDrawable); } else { final AsyncNinePatchDrawable.NinePatchChunk npc = AsyncNinePatchDrawable.NinePatchChunk .deserialize(chunk); final AsyncNinePatchDrawable asyncNinePatchDrawable = new AsyncNinePatchDrawable(mResources, mLoadingBitmap, chunk, npc.mPaddings, null, task); imageView.setImageDrawable(asyncNinePatchDrawable); } // NOTE: This uses a custom version of AsyncTask that has been // pulled from the // framework and slightly modified. Refer to the docs at the top of // the class // for more info on what was changed. task.executeOnExecutor(mExecutor, item); } }
From source file:com.itsherpa.andg.imageloader.ImageCache.java
/** * Initialize the cache, providing all parameters. * /*from w ww . jav a 2s .c o m*/ * @param cacheParams * The cache parameters to initialize the cache */ private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (BuildConfig.DEBUG) { LogUtils.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")"); } // If we're running on Honeycomb or newer, then if (Utils.hasHoneycomb()) { mReusableBitmaps = 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:com.sughimura.samplebitmaps.util.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache */// www .j a va 2 s.com 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). if (com.sughimura.samplebitmaps.util.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 (com.sughimura.samplebitmaps.util.RecyclingBitmapDrawable.class.isInstance(oldValue)) { // The removed entry is a recycling drawable, so notify it // that it has been removed from the memory cache ((com.sughimura.samplebitmaps.util.RecyclingBitmapDrawable) oldValue).setIsCached(false); } else { // The removed entry is a standard BitmapDrawable if (com.sughimura.samplebitmaps.util.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; } }; } //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(); } }