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

The following examples show how to use android.graphics.Bitmap#reconfigure() . 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: giffun   File: SizeStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap get(int width, int height, Bitmap.Config config) {
    final int size = Util.getBitmapByteSize(width, height, config);
    Key key = keyPool.get(size);

    Integer possibleSize = sortedSizes.ceilingKey(size);
    if (possibleSize != null && possibleSize != size && possibleSize <= size * MAX_SIZE_MULTIPLE) {
        keyPool.offer(key);
        key = keyPool.get(possibleSize);
    }

    // Do a get even if we know we don't have a bitmap so that the key moves to the front in the lru pool
    final Bitmap result = groupedMap.get(key);
    if (result != null) {
        result.reconfigure(width, height, config);
        decrementBitmapOfSize(possibleSize);
    }

    return result;
}
 
Example 2
Source Project: sketch   File: SizeConfigStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap get(int width, int height, Bitmap.Config config) {
    int size = SketchUtils.computeByteCount(width, height, config);
    Key targetKey = keyPool.get(size, config);
    Key bestKey = findBestKey(targetKey, size, config);

    Bitmap result = groupedMap.get(bestKey);
    if (result != null) {
        // Decrement must be called before reconfigure.
        decrementBitmapOfSize(SketchUtils.getByteCount(result), result.getConfig());
        try {
            result.reconfigure(width, height,
                    result.getConfig() != null ? result.getConfig() : Bitmap.Config.ARGB_8888);
        } catch (IllegalArgumentException e) {
            // Bitmap.cpp Bitmap_reconfigure method may throw "IllegalArgumentException: Bitmap not large enough to support new configuration" exception
            e.printStackTrace();
            put(result);
        }
    }
    return result;
}
 
Example 3
Source Project: giffun   File: SizeConfigStrategy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap get(int width, int height, Bitmap.Config config) {
    int size = Util.getBitmapByteSize(width, height, config);
    Key targetKey = keyPool.get(size, config);
    Key bestKey = findBestKey(targetKey, size, config);

    Bitmap result = groupedMap.get(bestKey);
    if (result != null) {
        // Decrement must be called before reconfigure.
        decrementBitmapOfSize(Util.getBitmapByteSize(result), result.getConfig());
        result.reconfigure(width, height,
                result.getConfig() != null ? result.getConfig() : Bitmap.Config.ARGB_8888);
    }
    return result;
}
 
Example 4
Source Project: APNG4Android   File: FrameSeqDecoder.java    License: Apache License 2.0 5 votes vote down vote up
protected Bitmap obtainBitmap(int width, int height) {
    Bitmap ret = null;
    Iterator<Bitmap> iterator = cacheBitmaps.iterator();
    while (iterator.hasNext()) {
        int reuseSize = width * height * 4;
        ret = iterator.next();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (ret != null && ret.getAllocationByteCount() >= reuseSize) {
                iterator.remove();
                if (ret.getWidth() != width || ret.getHeight() != height) {
                    ret.reconfigure(width, height, Bitmap.Config.ARGB_8888);
                }
                ret.eraseColor(0);
                return ret;
            }
        } else {
            if (ret != null && ret.getByteCount() >= reuseSize) {
                if (ret.getWidth() == width && ret.getHeight() == height) {
                    iterator.remove();
                    ret.eraseColor(0);
                }
                return ret;
            }
        }
    }

    try {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        ret = Bitmap.createBitmap(width, height, config);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return ret;
}
 
Example 5
private Bitmap generateShortcutPreview(BaseActivity launcher, ShortcutConfigActivityInfo info,
        int maxWidth, int maxHeight, Bitmap preview) {
    int iconSize = launcher.getDeviceProfile().iconSizePx;
    int padding = launcher.getResources()
            .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);

    int size = iconSize + 2 * padding;
    if (maxHeight < size || maxWidth < size) {
        throw new RuntimeException("Max size is too small for preview");
    }
    final Canvas c = new Canvas();
    if (preview == null || preview.getWidth() < size || preview.getHeight() < size) {
        preview = Bitmap.createBitmap(size, size, Config.ARGB_8888);
        c.setBitmap(preview);
    } else {
        if (preview.getWidth() > size || preview.getHeight() > size) {
            preview.reconfigure(size, size, preview.getConfig());
        }

        // Reusing bitmap. Clear it.
        c.setBitmap(preview);
        c.drawColor(0, PorterDuff.Mode.CLEAR);
    }
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    RectF boxRect = drawBoxWithShadow(c, p, size, size);

    Bitmap icon = LauncherIcons.createScaledBitmapWithoutShadow(
            mutateOnMainThread(info.getFullResIcon(mIconCache)), mContext, Build.VERSION_CODES.O);
    Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());

    boxRect.set(0, 0, iconSize, iconSize);
    boxRect.offset(padding, padding);
    c.drawBitmap(icon, src, boxRect, p);
    c.setBitmap(null);
    return preview;
}
 
Example 6
@Override
public Bitmap get(int width, int height, Bitmap.Config config) {
    int size = Util.getBitmapByteSize(width, height, config);
    Key bestKey = findBestKey(size, config);

    Bitmap result = groupedMap.get(bestKey);
    if (result != null) {
        // Decrement must be called before reconfigure.
        decrementBitmapOfSize(bestKey.size, result);
        result.reconfigure(width, height,
                result.getConfig() != null ? result.getConfig() : Bitmap.Config.ARGB_8888);
    }
    return result;
}
 
Example 7
Source Project: fresco   File: ArtBitmapFactory.java    License: MIT License 5 votes vote down vote up
/**
 * Creates a bitmap of the specified width and height.
 *
 * @param width the width of the bitmap
 * @param height the height of the bitmap
 * @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded
 *     Bitmap
 * @return a reference to the bitmap
 * @exception java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
@Override
public CloseableReference<Bitmap> createBitmapInternal(
    int width, int height, Bitmap.Config bitmapConfig) {
  int sizeInBytes = BitmapUtil.getSizeInByteForBitmap(width, height, bitmapConfig);
  Bitmap bitmap = mBitmapPool.get(sizeInBytes);
  Preconditions.checkArgument(
      bitmap.getAllocationByteCount()
          >= width * height * BitmapUtil.getPixelSizeForBitmapConfig(bitmapConfig));
  bitmap.reconfigure(width, height, bitmapConfig);
  return mCloseableReferenceFactory.create(bitmap, mBitmapPool);
}
 
Example 8
/**
 * Reconfigures bitmap after checking its allocation size.
 *
 * <p> This method is here to overcome our testing framework limit. Robolectric does not provide
 * KitKat specific APIs: {@link Bitmap#reconfigure} and {@link Bitmap#getAllocationByteCount}
 * are part of that.
 */
@TargetApi(19)
public static void reconfigureBitmap(Bitmap bitmap, int width, int height) {
  Preconditions.checkArgument(
      bitmap.getAllocationByteCount() >= width * height * BYTES_PER_PIXEL);
  bitmap.reconfigure(width, height, BITMAP_CONFIG);
}