List of usage examples for android.graphics.drawable BitmapDrawable getBitmap
public final Bitmap getBitmap()
From source file:de.s2hmobile.bitmaps.ImageCache.java
/** * Adds a bitmap to both memory and disk cache. * /*from w w w .j a v a2s.c o m*/ * @param data * Unique identifier for the bitmap to store * @param value * The bitmap drawable to store * @throws IOException */ void addToCache(final String key, final BitmapDrawable value) throws IOException { if (TextUtils.isEmpty(key) || value == null) { return; } // add drawable 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(key, value); } // write bitmap to disk cache synchronized (mDiskCacheLock) { if (mDiskLruCache != null) { writeToDisk(mDiskLruCache, key, value.getBitmap()); } } }
From source file:at.fjp.cards.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *///w ww.j av a2s . 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). 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:org.getlantern.firetweet.view.ShapedImageView.java
@Override protected void onDraw(@NonNull Canvas canvas) { mDestination.set(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom()); if (getStyle() == SHAPE_CIRCLE) { canvas.drawOval(mDestination, mBackgroundPaint); } else {/*ww w .j a va2 s . c o m*/ final float radius = getCalculatedCornerRadius(); canvas.drawRoundRect(mDestination, radius, radius, mBackgroundPaint); } if (OUTLINE_DRAW) { super.onDraw(canvas); } else { final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(), contentRight = getWidth() - getPaddingRight(), contentBottom = getHeight() - getPaddingBottom(); final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop; final int size = Math.min(contentWidth, contentHeight); if (mShadowBitmap != null) { canvas.drawBitmap(mShadowBitmap, contentLeft + (contentWidth - size) / 2 - mShadowRadius, contentTop + (contentHeight - size) / 2 - mShadowRadius, null); } Drawable drawable = getDrawable(); BitmapDrawable bitmapDrawable = null; // support state list drawable by getting the current state if (drawable instanceof StateListDrawable) { if (drawable.getCurrent() != null) { bitmapDrawable = (BitmapDrawable) drawable.getCurrent(); } } else if (drawable instanceof BitmapDrawable) { bitmapDrawable = (BitmapDrawable) drawable; } else if (drawable instanceof ColorDrawable) { mSolidColorPaint.setColor(((ColorDrawable) drawable).getColor()); } else { mSolidColorPaint.setColor(0); } Bitmap bitmap = null; if (bitmapDrawable != null) { bitmap = bitmapDrawable.getBitmap(); } if (bitmap != null) { mSource.set(0, 0, bitmap.getWidth(), bitmap.getHeight()); } drawBitmapWithCircleOnCanvas(bitmap, canvas, mSource, mDestination); } // Then draw the border. if (mBorderEnabled) { drawBorder(canvas, mDestination); } }
From source file:com.pdftron.pdf.controls.UserCropDialogFragment.java
private void setPage(int pageNumber) { if (pageNumber != mCurrentPage) { // if (mPageProperties[mCurrentPage] != null) { // if (mCropImageView.hasBitmap()) { // updatePageCropFromImageView(mPageProperties[mCurrentPage], mCropImageView.getCropRectPercentageMargins()); // Bitmap oldBitmap = mCropImageView.getImageBitmapAndClear(); // } // } mCurrentPage = pageNumber;//from w w w. j av a2 s . c o m } mBlankPageSpinnerHost.hide(true, false); mBlankPageSpinnerHost.show(); mPageNumberTextView.setText(String.format( getActivity().getResources().getString(R.string.user_crop_manual_crop_page_label), pageNumber)); PDFDoc doc = mPdfViewCtrl.getDoc(); try { doc.lockRead(); Page page = doc.getPage(pageNumber); PageCropProperties currentProperties = getCropPropertiesForPage(pageNumber); Log.i("USER_CROP", "Just set page to + " + pageNumber + ". Calculating page size..."); Log.i("USER_CROP", String.format("UserCropView size is (%1d, %2d)", mCropImageView.getMeasuredWidth(), mCropImageView.getMeasuredHeight())); Point newPageSize = calculateBlankPageSize(page.getPageWidth(), page.getPageHeight()); resizePageProperties((int) newPageSize.x, (int) newPageSize.y); BitmapDrawable drawable = mImageCache.get("" + pageNumber); if (drawable != null) { readyNextPage(pageNumber, drawable.getBitmap()); } else { if (mCurrentJob == null || mCurrentJob.getPageNumber() != pageNumber) { if (mCurrentJob != null && !mCurrentJob.isDone()) { Log.i("USER_CROP", "Cancelling job for page " + mCurrentJob.mPageNumber); mCurrentJob.cancelRasterizing(); } mCurrentJob = new DrawImageTask(getActivity(), pageNumber, newPageSize, doc); Log.i("USER_CROP", "Starting render job for page " + mCurrentJob.mPageNumber); mCurrentJob.execute(); } else { Log.i("USER_CROP", "Page " + mCurrentJob.mPageNumber + " is already being rendered"); } } } catch (com.pdftron.common.PDFNetException e) { } finally { try { doc.unlockRead(); } catch (com.pdftron.common.PDFNetException e) { } } }
From source file:ar.com.lapotoca.resiliencia.gallery.util.ImageCache.java
/** * Initialize the cache, providing all parameters. * * @param cacheParams The cache parameters to initialize the cache *//* w w w. j a v a 2 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) { 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.intuit.qboecoui.feeds.util.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 2s.co m*/ private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // Set up memory cache if (mCacheParams.memoryCacheEnabled) { if (BuildConfig.DEBUG) { CustomLog.logDebug(TAG, "[Feed] ImageCache: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()) { if (mEvictAndRecycle) { //QBM Mobile:: Take more control of Bitmap memory management. Bitmap recycleBitmap = oldValue.getBitmap(); if (recycleBitmap != null) { recycleBitmap.recycle(); recycleBitmap = null; } } else { // 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.android.launcher3.Utilities.java
/** * @param scale the scale to apply before drawing {@param icon} on the canvas *//* w ww .j av a2s .c o m*/ public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) { synchronized (sCanvas) { final int iconBitmapSize = getIconBitmapSize(); int width = iconBitmapSize; int height = iconBitmapSize; if (icon instanceof PaintDrawable) { PaintDrawable painter = (PaintDrawable) icon; painter.setIntrinsicWidth(width); painter.setIntrinsicHeight(height); } else if (icon instanceof BitmapDrawable) { // Ensure the bitmap has a density. BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; Bitmap bitmap = bitmapDrawable.getBitmap(); if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) { bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); } } int sourceWidth = icon.getIntrinsicWidth(); int sourceHeight = icon.getIntrinsicHeight(); if (sourceWidth > 0 && sourceHeight > 0) { // Scale the icon proportionally to the icon dimensions final float ratio = (float) sourceWidth / sourceHeight; if (sourceWidth > sourceHeight) { height = (int) (width / ratio); } else if (sourceHeight > sourceWidth) { width = (int) (height * ratio); } } // no intrinsic size --> use default size int textureWidth = iconBitmapSize; int textureHeight = iconBitmapSize; final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888); final Canvas canvas = sCanvas; canvas.setBitmap(bitmap); final int left = (textureWidth - width) / 2; final int top = (textureHeight - height) / 2; @SuppressWarnings("all") // suppress dead code warning final boolean debug = false; if (debug) { // draw a big box for the icon for debugging canvas.drawColor(sColors[sColorIndex]); if (++sColorIndex >= sColors.length) sColorIndex = 0; Paint debugPaint = new Paint(); debugPaint.setColor(0xffcccc00); canvas.drawRect(left, top, left + width, top + height, debugPaint); } sOldBounds.set(icon.getBounds()); icon.setBounds(left, top, left + width, top + height); canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2); icon.draw(canvas); canvas.restore(); icon.setBounds(sOldBounds); canvas.setBitmap(null); return bitmap; } }
From source file:com.android.pc.ioc.image.ImageCache.java
/** * Initialize the cache, providing all parameters. * /*from w ww . ja va2s . 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) { Ioc.getIoc().getLogger().d("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) { initDiskCache(); } }
From source file:com.eboxlive.ebox.image.ImageCache.java
/** * Initialize the cache, providing all parameters. * //from w w w .java2 s.c om * @param cacheParams * The cache parameters to initialize the cache */ private void init(ImageCacheParams cacheParams) { mCacheParams = cacheParams; // Set up memory cache if (mCacheParams.memoryCacheEnabled) { 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) { initDiskCache(); } }
From source file:org.thoughtcrime.securesms.ProfileFragment.java
@Override public void onLowMemory() { super.onLowMemory(); if (profilePicture != null) { if (profilePicture.getForeground() != null) { if (profilePicture.getForeground() instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) profilePicture.getForeground(); Bitmap bitmap = bitmapDrawable.getBitmap(); bitmap.recycle();/* w ww. jav a2 s .c om*/ } } profilePicture.setImageBitmap(null); profilePicture.setForeground(null); profilePicture.setBackground(null); System.gc(); } }