Java Code Examples for android.graphics.Bitmap.Config

The following are Jave code examples for showing how to use Config 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: GitHub   File: AnimatedImageFactoryImpl.java   View Source Code Vote up 6 votes
/**
 * Decodes a GIF into a CloseableImage.
 * @param encodedImage encoded image (native byte array holding the encoded bytes and meta data)
 * @param options the options for the decode
 * @param bitmapConfig the Bitmap.Config used to generate the output bitmaps
 * @return a {@link CloseableImage} for the GIF image
 */
public CloseableImage decodeGif(
    final EncodedImage encodedImage,
    final ImageDecodeOptions options,
    final Bitmap.Config bitmapConfig) {
  if (sGifAnimatedImageDecoder == null) {
    throw new UnsupportedOperationException("To encode animated gif please add the dependency " +
        "to the animated-gif module");
  }
  final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
  Preconditions.checkNotNull(bytesRef);
  try {
    final PooledByteBuffer input = bytesRef.get();
    AnimatedImage gifImage = sGifAnimatedImageDecoder.decode(input.getNativePtr(), input.size());

    return getCloseableImage(options, gifImage, bitmapConfig);
  } finally {
    CloseableReference.closeSafely(bytesRef);
  }
}
 
Example 2
Project: GitHub   File: SizeStrategy.java   View Source Code Vote up 6 votes
@Override
@Nullable
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 3
Project: tiny   File: BitmapKit.java   View Source Code Vote up 6 votes
public static long getBitmapSizeInBytes(Bitmap bitmap) {
    if (bitmap == null || bitmap.isRecycled())
        return 0L;
    Bitmap.Config config = bitmap.getConfig();
    switch (config) {
        case ARGB_8888:
            return ARGB_8888_BYTES_PER_PIXEL * getSizeInBytes(bitmap);
        case ALPHA_8:
            return ALPHA_8_BYTES_PER_PIXEL * getSizeInBytes(bitmap);
        case ARGB_4444:
            return ARGB_4444_BYTES_PER_PIXEL * getSizeInBytes(bitmap);
        case RGB_565:
            return RGB_565_BYTES_PER_PIXEL * getSizeInBytes(bitmap);
    }
    throw new TinyException.UnsupportedParamException("this bitmap config is not supported!");
}
 
Example 4
Project: GitHub   File: ACache.java   View Source Code Vote up 6 votes
private static Bitmap drawable2Bitmap(Drawable drawable) {
    if (drawable == null) {
        return null;
    }
    // 取 drawable 的长宽
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    // 取 drawable 的颜色格式
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE
            ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;
    // 建立对应 bitmap
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    // 建立对应 bitmap 的画布
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    // 把 drawable 内容画到画布中
    drawable.draw(canvas);
    return bitmap;
}
 
Example 5
Project: GitHub   File: ArtDecoder.java   View Source Code Vote up 5 votes
/**
 * Creates a bitmap from encoded bytes.
 *
 * @param encodedImage the encoded image with a reference to the encoded bytes
 * @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded
 *     Bitmap
 * @param regionToDecode optional image region to decode or null to decode the whole image
 * @return the bitmap
 * @exception java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
@Override
public CloseableReference<Bitmap> decodeFromEncodedImage(
    EncodedImage encodedImage, Bitmap.Config bitmapConfig, @Nullable Rect regionToDecode) {
  final BitmapFactory.Options options = getDecodeOptionsForStream(encodedImage, bitmapConfig);
  boolean retryOnFail=options.inPreferredConfig != Bitmap.Config.ARGB_8888;
  try {
    return decodeStaticImageFromStream(encodedImage.getInputStream(), options, regionToDecode);
  } catch (RuntimeException re) {
    if (retryOnFail) {
      return decodeFromEncodedImage(encodedImage, Bitmap.Config.ARGB_8888, regionToDecode);
    }
    throw re;
  }
}
 
Example 6
Project: GitHub   File: LruBitmapPool.java   View Source Code Vote up 5 votes
@NonNull
@Override
public Bitmap getDirty(int width, int height, Bitmap.Config config) {
  Bitmap result = getDirtyOrNull(width, height, config);
  if (result == null) {
    result = Bitmap.createBitmap(width, height, config);
  }
  return result;
}
 
Example 7
Project: browser   File: HelpUtils.java   View Source Code Vote up 5 votes
public static Bitmap newBitmap(int width, int height,Bitmap.Config config) {
    try {
        Bitmap bitmap = Bitmap.createBitmap(width, height,config);
        return bitmap;
    } catch (Throwable localThrowable) {
        LogUtil.e(TAG, localThrowable.getMessage());
    }
    return null;
}
 
Example 8
Project: GitHub   File: LruBitmapPoolTest.java   View Source Code Vote up 4 votes
@Override
public String logBitmap(int width, int height, Bitmap.Config config) {
  return null;
}
 
Example 9
Project: GitHub   File: SizeConfigStrategy.java   View Source Code Vote up 4 votes
public void init(int size, Bitmap.Config config) {
  this.size = size;
  this.config = config;
}
 
Example 10
Project: GitHub   File: Utils.java   View Source Code Vote up 4 votes
public static ANResponse<Bitmap> decodeBitmap(Response response, int maxWidth,
                                              int maxHeight, Bitmap.Config decodeConfig,
                                              ImageView.ScaleType scaleType) {
    return decodeBitmap(response, maxWidth, maxHeight, decodeConfig,
            new BitmapFactory.Options(), scaleType);
}
 
Example 11
Project: BookReader-master   File: PdfRendererParams.java   View Source Code Vote up 4 votes
public Bitmap.Config getConfig() {
    return config;
}
 
Example 12
Project: GitHub   File: LruBitmapPoolTest.java   View Source Code Vote up 4 votes
private Bitmap createMutableBitmap(Bitmap.Config config) {
  Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, config);
  Shadows.shadowOf(bitmap).setMutable(true);
  return bitmap;

}
 
Example 13
Project: GitHub   File: LruBitmapPoolTest.java   View Source Code Vote up 4 votes
private Bitmap createMutableBitmap(Bitmap.Config config) {
  Bitmap bitmap = ShadowBitmap.createBitmap(100, 100, config);
  Shadows.shadowOf(bitmap).setMutable(true);
  return bitmap;

}
 
Example 14
Project: GitHub   File: RoundedCornersRegressionTest.java   View Source Code Vote up 4 votes
private Bitmap createRect(int color, int width, int height, Bitmap.Config config) {
  final Bitmap result = Bitmap.createBitmap(width, height, config);
  Canvas canvas = new Canvas(result);
  canvas.drawColor(color);
  return result;
}
 
Example 15
Project: GitHub   File: SizeConfigStrategy.java   View Source Code Vote up 4 votes
@Override
public String logBitmap(int width, int height, Bitmap.Config config) {
  int size = Util.getBitmapByteSize(width, height, config);
  return getBitmapString(size, config);
}
 
Example 16
Project: GitHub   File: LruBitmapPoolTest.java   View Source Code Vote up 4 votes
@Override
public String logBitmap(int width, int height, Bitmap.Config config) {
  return null;
}
 
Example 17
Project: GitHub   File: PlatformBitmapFactory.java   View Source Code Vote up 3 votes
/**
 * Creates a bitmap with the specified width and height.  Its initial density is
 * determined from the given DisplayMetrics.
 *
 * @param colors The colors to write to the bitmap
 * @param width The width of the bitmap
 * @param height The height of the bitmap
 * @param config The bitmap config to create
 * @param callerContext the Tag to track who create the Bitmap
 * @return a reference to the bitmap
 * @throws TooManyBitmapsException if the pool is full
 * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
public CloseableReference<Bitmap> createBitmap(
    int[] colors,
    int width,
    int height,
    Bitmap.Config config,
    @Nullable Object callerContext) {
  CloseableReference<Bitmap> bitmapRef = createBitmapInternal(width, height, config);
  Bitmap bitmap = bitmapRef.get();
  bitmap.setPixels(colors, 0, width, 0, 0, width, height);
  addBitmapReference(bitmapRef.get(), callerContext);
  return bitmapRef;
}
 
Example 18
Project: GitHub   File: PlatformBitmapFactory.java   View Source Code Vote up 3 votes
/**
 * 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 Bitmap.Config used to create the Bitmap
 * @return a reference to the bitmap
 * @throws TooManyBitmapsException if the pool is full
 * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
public CloseableReference<Bitmap> createBitmap(
    int width,
    int height,
    Bitmap.Config bitmapConfig) {
  return createBitmap(width, height, bitmapConfig, null);
}
 
Example 19
Project: GitHub   File: PlatformDecoder.java   View Source Code Vote up 2 votes
/**
 * Creates a bitmap from encoded bytes. Supports JPEG but callers should use {@link
 * #decodeJPEGFromEncodedImage} for partial JPEGs. In addition, a region to decode can be supplied
 * in order to minimize memory usage. NOTE: Not all platform decoders necessarily support
 * supplying specific regions.
 *
 * @param encodedImage the reference to the encoded image with the reference to the encoded bytes
 * @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded
 *     Bitmap
 * @param regionToDecode optional image region to decode or null to decode the whole image
 * @return the bitmap
 * @throws TooManyBitmapsException if the pool is full
 * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
CloseableReference<Bitmap> decodeFromEncodedImage(
    final EncodedImage encodedImage, Bitmap.Config bitmapConfig, @Nullable Rect regionToDecode);
 
Example 20
Project: GitHub   File: GifDecoder.java   View Source Code Vote up 2 votes
/**
 * Returns an {@link Bitmap} with exactly the given dimensions and config.
 *
 * @param width  The width in pixels of the desired {@link android.graphics.Bitmap}.
 * @param height The height in pixels of the desired {@link android.graphics.Bitmap}.
 * @param config The {@link android.graphics.Bitmap.Config} of the desired {@link
 *               android.graphics.Bitmap}.
 */
@NonNull
Bitmap obtain(int width, int height, Bitmap.Config config);