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

The following examples show how to use android.graphics.Bitmap#isMutable() . 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 File: OriginBlurFilter.java    From HokoBlur with 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 File: WidgetPreviewLoader.java    From TurboLauncher with Apache License 2.0 6 votes vote down vote up
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 3
Source File: BitmapScrapHeap.java    From CrossBow with 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 4
Source File: ImageCache.java    From graphics-samples with 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 5
Source File: BitmapUtils.java    From tilt-game-android with 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 6
Source File: ImageCache.java    From FanXin-based-HuanXin with GNU General Public License v2.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
Source File: ToastedMarkerOptionsChooser.java    From clusterkraf with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: LruBitmapPool.java    From GlideBitmapPool with 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 9
Source File: BitmapUtil.java    From FileManager with 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 10
Source File: ImageCache.java    From BubbleCloudView with 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 File: BaseBitmapTextureAtlasSourceDecorator.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
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 12
Source File: BitmapLruPool.java    From jus with 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 13
Source File: ImageCache.java    From android-DisplayingBitmaps with 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 14
Source File: ImageCache.java    From vocefiscal-android with 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 15
Source File: LruBitmapPool.java    From giffun with 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 16
Source File: WidgetPreviewLoader.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
@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
Source File: WidgetPreviewLoader.java    From Trebuchet with GNU General Public License v3.0 4 votes vote down vote up
@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
Source File: CacheableBitmapDrawable.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public synchronized boolean isBitmapMutable() {
    Bitmap bitmap = getBitmap();
    return null != bitmap && bitmap.isMutable();
}
 
Example 19
Source File: WidgetPreviewLoader.java    From TurboLauncher with Apache License 2.0 4 votes vote down vote up
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
Source File: BitmapPool.java    From FanXin-based-HuanXin with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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());
}