Java Code Examples for android.graphics.Bitmap#isMutable()

The following examples show how to use android.graphics.Bitmap#isMutable() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: HokoBlur   File: OriginBlurFilter.java    License: Apache License 2.0 6 votes vote down vote up
public static void doFullBlur(@Mode int mode, Bitmap bitmap, int radius) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    final int[] pixels = new int[w * h];
    bitmap.getPixels(pixels, 0, w, 0, 0, w, h);

    switch (mode) {
        case HokoBlur.MODE_BOX:
            BoxBlurFilter.doBlur(pixels, w, h, radius, HokoBlur.BOTH);
            break;

        case HokoBlur.MODE_GAUSSIAN:
            GaussianBlurFilter.doBlur(pixels, w, h, radius, HokoBlur.BOTH);
            break;

        case HokoBlur.MODE_STACK:
            StackBlurFilter.doBlur(pixels, w, h, radius, HokoBlur.BOTH);
            break;
    }
    if (bitmap.isMutable()) {
        bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
    } else {
        replaceBitmap(bitmap, pixels, 0, 0, w, h);
    }
}
 
Example 2
Source Project: CrossBow   File: BitmapScrapHeap.java    License: Apache License 2.0 6 votes vote down vote up
public Bitmap getBitmapToFill(BitmapFactory.Options options) {
    synchronized (unusedBitmaps) {
        Iterator<WeakReference<Bitmap>> iterator = unusedBitmaps.iterator();

        while (iterator.hasNext()) {
            Bitmap bitmap = iterator.next().get();
            if(bitmap != null) {
                if(bitmap.isMutable() && canUseForInBitmap(bitmap, options)) {
                    iterator.remove();
                    return bitmap;
                }
            }
            else {
                iterator.remove();
            }
        }
    }

    return null;
}
 
Example 3
public void recycleBitmap(Object o, Bitmap bitmapToRecycle) {
    String name = getObjectName(o);
    synchronized (mLoadedPreviews) {
        if (mLoadedPreviews.containsKey(name)) {
            Bitmap b = mLoadedPreviews.get(name).get();
            if (b == bitmapToRecycle) {
                mLoadedPreviews.remove(name);
                if (bitmapToRecycle.isMutable()) {
                    synchronized (mUnusedBitmaps) {
                        mUnusedBitmaps.add(new SoftReference<Bitmap>(b));
                    }
                }
            } else {
                throw new RuntimeException("Bitmap passed in doesn't match up");
            }
        }
    }
}
 
Example 4
Source Project: giffun   File: LruBitmapPool.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean put(Bitmap bitmap) {
    if (bitmap == null) {
        throw new NullPointerException("Bitmap must not be null");
    }
    if (!bitmap.isMutable() || strategy.getSize(bitmap) > maxSize || !allowedConfigs.contains(bitmap.getConfig())) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "Reject bitmap from pool"
                    + ", bitmap: " + strategy.logBitmap(bitmap)
                    + ", is mutable: " + bitmap.isMutable()
                    + ", is allowed config: " + allowedConfigs.contains(bitmap.getConfig()));
        }
        return false;
    }

    final int size = strategy.getSize(bitmap);
    strategy.put(bitmap);
    tracker.add(bitmap);

    puts++;
    currentSize += size;

    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "Put bitmap in pool=" + strategy.logBitmap(bitmap));
    }
    dump();

    evict();
    return true;
}
 
Example 5
Source Project: graphics-samples   File: ImageCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param options - BitmapFactory.Options with out* options populated
 * @return Bitmap that case be used for inBitmap
 */
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
    //BEGIN_INCLUDE(get_bitmap_from_reusable_set)
    Bitmap bitmap = null;

    if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
        synchronized (mReusableBitmaps) {
            final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
            Bitmap item;

            while (iterator.hasNext()) {
                item = iterator.next().get();

                if (null != item && item.isMutable()) {
                    // Check to see it the item can be used for inBitmap
                    if (canUseForInBitmap(item, options)) {
                        bitmap = item;

                        // Remove from reusable set so it can't be used again
                        iterator.remove();
                        break;
                    }
                } else {
                    // Remove from the set if the reference has been cleared.
                    iterator.remove();
                }
            }
        }
    }

    return bitmap;
    //END_INCLUDE(get_bitmap_from_reusable_set)
}
 
Example 6
Source Project: vocefiscal-android   File: ImageCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param options - BitmapFactory.Options with out* options populated
 * @return Bitmap that case be used for inBitmap
 */
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
    //BEGIN_INCLUDE(get_bitmap_from_reusable_set)
    Bitmap bitmap = null;

    if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
        synchronized (mReusableBitmaps) {
            final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
            Bitmap item;

            while (iterator.hasNext()) {
                item = iterator.next().get();

                if (null != item && item.isMutable()) {
                    // Check to see it the item can be used for inBitmap
                    if (canUseForInBitmap(item, options)) {
                        bitmap = item;

                        // Remove from reusable set so it can't be used again
                        iterator.remove();
                        break;
                    }
                } else {
                    // Remove from the set if the reference has been cleared.
                    iterator.remove();
                }
            }
        }
    }

    return bitmap;
    //END_INCLUDE(get_bitmap_from_reusable_set)
}
 
Example 7
/**
 * @param options - BitmapFactory.Options with out* options populated
 * @return Bitmap that case be used for inBitmap
 */
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
    //BEGIN_INCLUDE(get_bitmap_from_reusable_set)
    Bitmap bitmap = null;

    if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
        synchronized (mReusableBitmaps) {
            final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
            Bitmap item;

            while (iterator.hasNext()) {
                item = iterator.next().get();

                if (null != item && item.isMutable()) {
                    // Check to see it the item can be used for inBitmap
                    if (canUseForInBitmap(item, options)) {
                        bitmap = item;

                        // Remove from reusable set so it can't be used again
                        iterator.remove();
                        break;
                    }
                } else {
                    // Remove from the set if the reference has been cleared.
                    iterator.remove();
                }
            }
        }
    }

    return bitmap;
    //END_INCLUDE(get_bitmap_from_reusable_set)
}
 
Example 8
Source Project: jus   File: BitmapLruPool.java    License: Apache License 2.0 5 votes vote down vote up
protected static boolean canBePooled(Bitmap bitmap) {
    if (bitmap.isMutable() && !bitmap.isRecycled()) {
        return true;
    }
    //note: do not recycle if cannot be pooled it still can be used somewhere
    return false;
}
 
Example 9
private static Bitmap ensureLoadedBitmapIsMutable(final Bitmap pBitmap) {
	if(pBitmap.isMutable()) {
		return pBitmap;
	} else {
		final Bitmap mutableBitmap = pBitmap.copy(pBitmap.getConfig(), true);
		pBitmap.recycle();
		return mutableBitmap;
	}
}
 
Example 10
Source Project: BubbleCloudView   File: ImageCache.java    License: MIT License 5 votes vote down vote up
/**
 * @param options - BitmapFactory.Options with out* options populated
 * @return Bitmap that case be used for inBitmap
 */
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
    //BEGIN_INCLUDE(get_bitmap_from_reusable_set)
    Bitmap bitmap = null;

    if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
        synchronized (mReusableBitmaps) {
            final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
            Bitmap item;

            while (iterator.hasNext()) {
                item = iterator.next().get();

                if (null != item && item.isMutable()) {
                    // Check to see it the item can be used for inBitmap
                    if (canUseForInBitmap(item, options)) {
                        bitmap = item;

                        // Remove from reusable set so it can't be used again
                        iterator.remove();
                        break;
                    }
                } else {
                    // Remove from the set if the reference has been cleared.
                    iterator.remove();
                }
            }
        }
    }

    return bitmap;
    //END_INCLUDE(get_bitmap_from_reusable_set)
}
 
Example 11
Source Project: FileManager   File: BitmapUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static Bitmap adjustOpacity(Bitmap bitmap, int opacity) {
    Bitmap mutableBitmap = bitmap.isMutable()
            ? bitmap
            : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int colour = (opacity & 0xFF) << 24;
    canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}
 
Example 12
Source Project: GlideBitmapPool   File: LruBitmapPool.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void put(Bitmap bitmap) {
    if (bitmap == null) {
        throw new NullPointerException("Bitmap must not be null");
    }
    if (bitmap.isRecycled()) {
        throw new IllegalStateException("Cannot pool recycled bitmap");
    }

    if (!bitmap.isMutable() || strategy.getSize(bitmap) > maxSize
            || !allowedConfigs.contains(bitmap.getConfig())) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "Reject bitmap from pool"
                    + ", bitmap: " + strategy.logBitmap(bitmap)
                    + ", is mutable: " + bitmap.isMutable()
                    + ", is allowed config: " + allowedConfigs.contains(bitmap.getConfig()));
        }
        bitmap.recycle();
        return;
    }

    final int size = strategy.getSize(bitmap);
    strategy.put(bitmap);
    tracker.add(bitmap);

    puts++;
    currentSize += size;

    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "Put bitmap in pool=" + strategy.logBitmap(bitmap));
    }
    dump();

    evict();
}
 
Example 13
@SuppressLint("NewApi")
private Bitmap getClusterBitmap(Resources res, int resourceId, int clusterSize) {
	BitmapFactory.Options options = new BitmapFactory.Options();

	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		options.inMutable = true;
	}
	Bitmap bitmap = BitmapFactory.decodeResource(res, resourceId, options);
	if (bitmap.isMutable() == false) {
		bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
	}

	Canvas canvas = new Canvas(bitmap);

	Paint paint = null;
	float originY;
	if (clusterSize < 100) {
		paint = clusterPaintLarge;
		originY = bitmap.getHeight() * 0.64f;
	} else if (clusterSize < 1000) {
		paint = clusterPaintMedium;
		originY = bitmap.getHeight() * 0.6f;
	} else {
		paint = clusterPaintSmall;
		originY = bitmap.getHeight() * 0.56f;
	}

	canvas.drawText(String.valueOf(clusterSize), bitmap.getWidth() * 0.5f, originY, paint);

	return bitmap;
}
 
Example 14
/**
 * @param options
 *            - BitmapFactory.Options with out* options populated
 * @return Bitmap that case be used for inBitmap
 */
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
	// BEGIN_INCLUDE(get_bitmap_from_reusable_set)
	Bitmap bitmap = null;

	if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
		synchronized (mReusableBitmaps) {
			final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps
					.iterator();
			Bitmap item;

			while (iterator.hasNext()) {
				item = iterator.next().get();

				if (null != item && item.isMutable()) {
					// Check to see it the item can be used for inBitmap
					if (canUseForInBitmap(item, options)) {
						bitmap = item;

						// Remove from reusable set so it can't be used
						// again
						iterator.remove();
						break;
					}
				} else {
					// Remove from the set if the reference has been
					// cleared.
					iterator.remove();
				}
			}
		}
	}

	return bitmap;
	// END_INCLUDE(get_bitmap_from_reusable_set)
}
 
Example 15
Source Project: tilt-game-android   File: BitmapUtils.java    License: MIT License 5 votes vote down vote up
/**
 * @param pBitmap
 *
 * @return a {@link Bitmap} that is {@link Bitmap#isMutable()}. If <code>pBitmap</code> is {@link Bitmap#isMutable()} then <code>pBitmap</code> is returned, otherwise a new {@link Bitmap} instance is returned that is {@link Bitmap#isMutable()} and <code>pBitmap</code> is {@link Bitmap#recycle()}d.
 */
public static Bitmap ensureBitmapIsMutable(final Bitmap pBitmap) {
	if (pBitmap.isMutable()) {
		return pBitmap;
	} else {
		final Bitmap mutableBitmap = pBitmap.copy(pBitmap.getConfig(), true);
		pBitmap.recycle();
		return mutableBitmap;
	}
}
 
Example 16
@Override
protected Bitmap doInBackground(Void... params) {
    Bitmap unusedBitmap = null;

    // If already cancelled before this gets to run in the background, then return early
    if (isCancelled()) {
        return null;
    }
    synchronized (mUnusedBitmaps) {
        // Check if we can re-use a bitmap
        for (Bitmap candidate : mUnusedBitmaps) {
            if (candidate != null && candidate.isMutable() &&
                    candidate.getWidth() == mPreviewWidth &&
                    candidate.getHeight() == mPreviewHeight) {
                unusedBitmap = candidate;
                mUnusedBitmaps.remove(unusedBitmap);
                break;
            }
        }
    }

    // creating a bitmap is expensive. Do not do this inside synchronized block.
    if (unusedBitmap == null) {
        unusedBitmap = Bitmap.createBitmap(mPreviewWidth, mPreviewHeight, Config.ARGB_8888);
    }
    // If cancelled now, don't bother reading the preview from the DB
    if (isCancelled()) {
        return unusedBitmap;
    }
    Bitmap preview = readFromDb(mKey, unusedBitmap, this);
    // Only consider generating the preview if we have not cancelled the task already
    if (!isCancelled() && preview == null) {
        // Fetch the version info before we generate the preview, so that, in-case the
        // app was updated while we are generating the preview, we use the old version info,
        // which would gets re-written next time.
        boolean persistable = mInfo.activityInfo == null
                || mInfo.activityInfo.isPersistable();
        mVersions = persistable ? getPackageVersion(mKey.componentName.getPackageName())
                : null;

        // it's not in the db... we need to generate it
        preview = generatePreview(mActivity, mInfo, unusedBitmap, mPreviewWidth, mPreviewHeight);
    }
    return preview;
}
 
Example 17
@Override
protected Bitmap doInBackground(Void... params) {
    Bitmap unusedBitmap = null;

    // If already cancelled before this gets to run in the background, then return early
    if (isCancelled()) {
        return null;
    }
    synchronized (mUnusedBitmaps) {
        // Check if we can re-use a bitmap
        for (Bitmap candidate : mUnusedBitmaps) {
            if (candidate != null && candidate.isMutable() &&
                    candidate.getWidth() == mPreviewWidth &&
                    candidate.getHeight() == mPreviewHeight) {
                unusedBitmap = candidate;
                mUnusedBitmaps.remove(unusedBitmap);
                break;
            }
        }
    }

    // creating a bitmap is expensive. Do not do this inside synchronized block.
    if (unusedBitmap == null) {
        unusedBitmap = Bitmap.createBitmap(mPreviewWidth, mPreviewHeight, Config.ARGB_8888);
    }
    // If cancelled now, don't bother reading the preview from the DB
    if (isCancelled()) {
        return unusedBitmap;
    }
    Bitmap preview = readFromDb(mKey, unusedBitmap, this);
    // Only consider generating the preview if we have not cancelled the task already
    if (!isCancelled() && preview == null) {
        // Fetch the version info before we generate the preview, so that, in-case the
        // app was updated while we are generating the preview, we use the old version info,
        // which would gets re-written next time.
        mVersions = getPackageVersion(mKey.componentName.getPackageName());

        Launcher launcher = (Launcher) mCaller.getContext();

        // it's not in the db... we need to generate it
        preview = generatePreview(launcher, mInfo, unusedBitmap, mPreviewWidth, mPreviewHeight);
    }
    return preview;
}
 
Example 18
public synchronized boolean isBitmapMutable() {
    Bitmap bitmap = getBitmap();
    return null != bitmap && bitmap.isMutable();
}
 
Example 19
public Bitmap getPreview(final Object o) {
    final String name = getObjectName(o);
    final String packageName = getObjectPackage(o);
    // check if the package is valid
    boolean packageValid = true;
    synchronized(sInvalidPackages) {
        packageValid = !sInvalidPackages.contains(packageName);
    }
    if (!packageValid) {
        return null;
    }
    if (packageValid) {
        synchronized(mLoadedPreviews) {
            // check if it exists in our existing cache
            if (mLoadedPreviews.containsKey(name) && mLoadedPreviews.get(name).get() != null) {
                return mLoadedPreviews.get(name).get();
            }
        }
    }

    Bitmap unusedBitmap = null;
    synchronized(mUnusedBitmaps) {
        // not in cache; we need to load it from the db
        while ((unusedBitmap == null || !unusedBitmap.isMutable() ||
                unusedBitmap.getWidth() != mPreviewBitmapWidth ||
                unusedBitmap.getHeight() != mPreviewBitmapHeight)
                && mUnusedBitmaps.size() > 0) {
            unusedBitmap = mUnusedBitmaps.remove(0).get();
        }
        if (unusedBitmap != null) {
            final Canvas c = mCachedAppWidgetPreviewCanvas.get();
            c.setBitmap(unusedBitmap);
            c.drawColor(0, PorterDuff.Mode.CLEAR);
            c.setBitmap(null);
        }
    }

    if (unusedBitmap == null) {
        unusedBitmap = Bitmap.createBitmap(mPreviewBitmapWidth, mPreviewBitmapHeight,
                Bitmap.Config.ARGB_8888);
    }

    Bitmap preview = null;

    if (packageValid) {
        preview = readFromDb(name, unusedBitmap);
    }

    if (preview != null) {
        synchronized(mLoadedPreviews) {
            mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview));
        }
        return preview;
    } else {
        // it's not in the db... we need to generate it
        final Bitmap generatedPreview = generatePreview(o, unusedBitmap);
        preview = generatedPreview;
        if (preview != unusedBitmap) {
            throw new RuntimeException("generatePreview is not recycling the bitmap " + o);
        }

        synchronized(mLoadedPreviews) {
            mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview));
        }

        // write to db on a thread pool... this can be done lazily and improves the performance
        // of the first time widget previews are loaded
        new AsyncTask<Void, Void, Void>() {
            public Void doInBackground(Void ... args) {
                writeToDb(o, generatedPreview);
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);

        return preview;
    }
}
 
Example 20
/**
 * Determine if this bitmap is reusable (i.e.) if subsequent {@link #get(int)} requests can
 * use this value.
 * The bitmap is reusable if
 *  - it has not already been recycled AND
 *  - it is mutable AND
 *  - it has the desired bitmap-config
 * @param value the value to test for reusability
 * @return true, if the bitmap can be reused
 */
@Override
protected boolean isReusable(Bitmap value) {
  Preconditions.checkNotNull(value);
  return !value.isRecycled() &&
      value.isMutable() &&
      Bitmaps.BITMAP_CONFIG.equals(value.getConfig());
}