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

The following are Jave code examples for showing how to use isMutable() of the android.graphics.Bitmap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: QiangHongBao   File: BorderImageView.java   View Source Code Vote up 6 votes
@Override
protected Bitmap getBitmapFromDrawable(Drawable drawable) {
	Bitmap bitmap = super.getBitmapFromDrawable(drawable);
	if (mBorderSize <= 0) {
		return bitmap;
	}

	if(!bitmap.isMutable()){
		bitmap = bitmap.copy(bitmap.getConfig(), true);
	}

	Canvas canvas = new Canvas(bitmap);

	// draw border
	mBorderPaint.setColor(mBorderColor);
	mBorderPaint.setStrokeWidth(mBorderSize + 2);
	mPath.reset();
	mPathFactory.planRoundPath(mRadius, getRectF(), mPath, mBorderSize / 2 - 1);
	canvas.drawPath(mPath, mBorderPaint);

	return bitmap;
}
 
Example 2
Project: MontageCam   File: PhotoProcessing.java   View Source Code Vote up 6 votes
private static Bitmap getBitmapFromNative(Bitmap bitmap) {
    int width = nativeGetBitmapWidth();
    int height = nativeGetBitmapHeight();

    if (bitmap == null || width != bitmap.getWidth()
            || height != bitmap.getHeight() || !bitmap.isMutable()) { // in
        Config config = Config.ARGB_8888;
        if (bitmap != null) {
            config = bitmap.getConfig();
            bitmap.recycle();
        }
        bitmap = Bitmap.createBitmap(width, height, config);
    }

    int[] pixels = new int[width];
    for (int y = 0; y < height; y++) {
        nativeGetBitmapRow(y, pixels);
        bitmap.setPixels(pixels, 0, width, 0, y, width, 1);
    }

    return bitmap;
}
 
Example 3
Project: starcor.xul   File: BitmapTools.java   View Source Code Vote up 6 votes
public static int countGCBitmap() {
	synchronized (_weakGCBitmapQueue) {
		for (int i = 0, gcBitmapQueueSize = _weakGCBitmapQueue.size(); i < gcBitmapQueueSize; i++) {
			Pair<Long, SoftReference<Bitmap>> item = _weakGCBitmapQueue.get(i);
			Bitmap bmp = item.second.get();

			if (bmp == null || !bmp.isMutable() || bmp.isRecycled()) {
				if (XulManager.DEBUG) {
					if (bmp == null) {
					} else if (!bmp.isMutable()) {
						Log.e(TAG_RECYCLER, "invalid bitmap immutable(gc reuse phase)!!!");
					} else {
						Log.e(TAG_RECYCLER, "invalid bitmap recycled(gc reuse phase)!!!");
					}
				}
				_weakGCBitmapQueue.remove(i);
				--i;
				--gcBitmapQueueSize;
				continue;
			}
		}
	}
	return _weakGCBitmapQueue.size();
}
 
Example 4
Project: GitHub   File: LruBitmapPool.java   View Source Code Vote up 5 votes
@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 5
Project: GitHub   File: LruBitmapPool.java   View Source Code Vote up 5 votes
@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 6
Project: SuperSelector   File: ImageCache.java   View Source Code Vote up 5 votes
/**
 * @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
Project: AdaptiveIconView   File: ImageUtils.java   View Source Code Vote up 5 votes
/**
 * Removes the shadow (and any other transparent parts)
 * from a bitmap.
 *
 * @param bitmap the original bitmap
 * @return the bitmap with the shadow removed
 */
public static Bitmap removeShadow(Bitmap bitmap) {
    if (!bitmap.isMutable())
        bitmap = bitmap.copy(bitmap.getConfig(), true);

    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    for (int i = 0; i < pixels.length; i++) {
        if (Color.alpha(pixels[i]) < 255)
            pixels[i] = Color.TRANSPARENT;
    }

    bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    return bitmap;
}
 
Example 8
Project: ImageFrame   File: ImageCache.java   View Source Code Vote up 5 votes
Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
  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;
}
 
Example 9
Project: DisplayingBitmaps   File: ImageCache.java   View Source Code Vote up 5 votes
/**
 * @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 10
Project: FlickLauncher   File: WallpaperCropActivity.java   View Source Code Vote up 5 votes
private void addReusableBitmap(TileSource src) {
    synchronized (mReusableBitmaps) {
        if (src instanceof BitmapRegionTileSource) {
            Bitmap preview = ((BitmapRegionTileSource) src).getBitmap();
            if (preview != null && preview.isMutable()) {
                mReusableBitmaps.add(preview);
            }
        }
    }
}
 
Example 11
Project: FrameAnimationView   File: ImageCache.java   View Source Code Vote up 5 votes
/**
 * @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 12
Project: GCSApp   File: ImageCache.java   View Source Code Vote up 5 votes
/**
 * @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 13
Project: Android-Practice   File: ImageCache.java   View Source Code Vote up 5 votes
/**
 * @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
Project: starcor.xul   File: XulWorker.java   View Source Code Vote up 5 votes
private static Bitmap toRoundCornerBitmap(Canvas canvas, Paint paintClear, Bitmap srcBitmap, float[] roundRadius) {
	if (srcBitmap.isMutable() && srcBitmap.hasAlpha()) {
		return toRoundCornerMutableBitmap(canvas, paintClear, srcBitmap, roundRadius);
	}
	Bitmap output;
	if (false) {
		output = BitmapTools.createBitmapFromRecycledBitmaps(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
	} else {
		output = BitmapTools.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
	}
	canvas.setBitmap(output);
	Rect rect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
	canvas.drawBitmap(srcBitmap, rect, rect, null);
	return toRoundCornerMutableBitmap(canvas, paintClear, output, roundRadius);
}
 
Example 15
Project: LaunchEnr   File: WidgetPreviewLoader.java   View Source Code Vote up 4 votes
@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 16
Project: appinventor-extensions   File: Canvas.java   View Source Code Vote up 4 votes
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
  int oldBitmapWidth = bitmap.getWidth();
  int oldBitmapHeight = bitmap.getHeight();
  if (w != oldBitmapWidth || h != oldBitmapHeight) {
    Bitmap oldBitmap = bitmap;

    // Create a new bitmap by scaling the old bitmap that contained the
    // drawing layer (points, lines, text, etc.).

    // The documentation for Bitmap.createScaledBitmap doesn't specify whether it creates a
    // mutable or immutable bitmap. Looking at the source code shows that it calls
    // Bitmap.createBitmap(Bitmap, int, int, int, int, Matrix, boolean), which is documented as
    // returning an immutable bitmap. However, it actually returns a mutable bitmap.
    // It's possible that the behavior could change in the future if they "fix" that bug.
    // Try Bitmap.createScaledBitmap, but if it gives us an immutable bitmap, we'll have to
    // create a mutable bitmap and scale the old bitmap using Canvas.drawBitmap.
    try {
      // See comment at the catch below
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(oldBitmap, w, h, false);

      if (scaledBitmap.isMutable()) {
        // scaledBitmap is mutable; we can use it in a canvas.
        bitmap = scaledBitmap;
        // NOTE(lizlooney) - I tried just doing canvas.setBitmap(bitmap), but after that the
        // canvas.drawCircle() method did not work correctly. So, we need to create a whole new
        // canvas.
        canvas = new android.graphics.Canvas(bitmap);

      } else {
        // scaledBitmap is immutable; we can't use it in a canvas.

        bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        // NOTE(lizlooney) - I tried just doing canvas.setBitmap(bitmap), but after that the
        // canvas.drawCircle() method did not work correctly. So, we need to create a whole new
        // canvas.
        canvas = new android.graphics.Canvas(bitmap);

        // Draw the old bitmap into the new canvas, scaling as necessary.
        Rect src = new Rect(0, 0, oldBitmapWidth, oldBitmapHeight);
        RectF dst = new RectF(0, 0, w, h);
        canvas.drawBitmap(oldBitmap, src, dst, null);
      }

    } catch (IllegalArgumentException ioe) {
      // There's some kind of order of events issue that results in w or h being zero.
      // I'm guessing that this is a result of specifying width or height as FILL_PARRENT on an
      // opening screen.   In any case, w<=0 or h<=0 causes the call to createScaledBitmap
      // to throw an illegal argument.  If this happens we simply don't draw the bitmap
      // (which would be of width or height 0)
      // TODO(hal): Investigate this further to see what is causes the w=0 or h=0 and see if
      // there is a more high-level fix.

      Log.e(LOG_TAG, "Bad values to createScaledBimap w = " + w + ", h = " + h);
    }

    // The following has nothing to do with the scaling in this method.
    // It has to do with scaling the background image for GetColor().
    // Specifically, it says we need to regenerate the bitmap representing
    // the background color/image if a call to GetColor() is made.
    scaledBackgroundBitmap = null;
  }
}
 
Example 17
Project: FlickLauncher   File: WidgetPreviewLoader.java   View Source Code Vote up 4 votes
@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.getLauncher(mCaller.getContext());

        // it's not in the db... we need to generate it
        preview = generatePreview(launcher, mInfo, unusedBitmap, mPreviewWidth, mPreviewHeight);
    }
    return preview;
}
 
Example 18
Project: SimpleUILauncher   File: WidgetPreviewLoader.java   View Source Code Vote up 4 votes
@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.getLauncher(mCaller.getContext());

        // it's not in the db... we need to generate it
        preview = generatePreview(launcher, mInfo, unusedBitmap, mPreviewWidth, mPreviewHeight);
    }
    return preview;
}
 
Example 19
Project: GitHub   File: BitmapPool.java   View Source Code Vote up 3 votes
/**
 * 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
 * @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();
}
 
Example 20
Project: CanvasScript   File: CanvasScript.java   View Source Code Vote up 3 votes
/**
 * Create a new instance with a bitmap instance that will be the root of the
 * {@link Canvas} created
 * @param bitmap the base drawing bitmap
 * @return self for chaining
 * @throws IllegalArgumentException if bitmap is not mutable or is recycled
 * @see Canvas
 */
public static CanvasScript create(@NonNull Bitmap bitmap) {
    if (!bitmap.isMutable() || bitmap.isRecycled()) {
        throw new IllegalArgumentException("Bitmap must be mutable and unrecycled");
    }
    return new CanvasScript(bitmap);
}