/** * @deprecated Use {@link #obtain(Resources, Resource)} instead, it can be unsafe to extract * {@link Bitmap}s from their wrapped {@link Resource}. */ @Deprecated public static LazyBitmapDrawableResource obtain(Resources resources, BitmapPool bitmapPool, Bitmap bitmap) { return (LazyBitmapDrawableResource) obtain(resources, BitmapResource.obtain(bitmap, bitmapPool)); }
@NonNull Resource<Bitmap> buildFrameResource(@NonNull Bitmap bitmap, @NonNull BitmapPool bitmapPool) { return new BitmapResource(bitmap, bitmapPool); } }
@Override public Resource<?> answer(InvocationOnMock invocationOnMock) throws Throwable { BitmapResource resource = (BitmapResource) invocationOnMock.getArguments()[1]; bitmaps.add(resource.get()); return null; } }
@Test public void testSizeIsBasedOnDimensPreKitKat() { Util.setSdkVersionInt(18); assertEquals(harness.bitmap.getWidth() * harness.bitmap.getHeight() * 4, harness.resource.getSize()); }
@Override public Resource<Bitmap> decode(@NonNull GifDecoder source, int width, int height, @NonNull Options options) { Bitmap bitmap = source.getNextFrame(); return BitmapResource.obtain(bitmap, bitmapPool); } }
/** * Returns a new {@link BitmapResource} wrapping the given {@link Bitmap} if the Bitmap is * non-null or null if the given Bitmap is null. * * @param bitmap A Bitmap. * @param bitmapPool A non-null {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}. */ @Nullable public static BitmapResource obtain(@Nullable Bitmap bitmap, @NonNull BitmapPool bitmapPool) { if (bitmap == null) { return null; } else { return new BitmapResource(bitmap, bitmapPool); } }
@Test public void testCanGetBitmap() { assertEquals(harness.bitmap, harness.resource.get()); }
@Override public Resource<Bitmap> answer(InvocationOnMock invocationOnMock) throws Throwable { return BitmapResource.obtain( Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888), bitmapPool); } });
@Override public boolean encode(@NonNull Resource<BitmapDrawable> data, @NonNull File file, @NonNull Options options) { return encoder.encode(new BitmapResource(data.get().getBitmap(), bitmapPool), file, options); }
@Nullable static Resource<Bitmap> convert(BitmapPool bitmapPool, Drawable drawable, int width, int height) { // Handle DrawableContainer or StateListDrawables that may contain one or more BitmapDrawables. drawable = drawable.getCurrent(); Bitmap result = null; boolean isRecycleable = false; if (drawable instanceof BitmapDrawable) { result = ((BitmapDrawable) drawable).getBitmap(); } else if (!(drawable instanceof Animatable)) { result = drawToBitmap(bitmapPool, drawable, width, height); // We created and drew to the Bitmap, so it's safe for us to recycle or re-use. isRecycleable = true; } BitmapPool toUse = isRecycleable ? bitmapPool : NO_RECYCLE_BITMAP_POOL; return BitmapResource.obtain(result, toUse); }
@Test(expected = NullPointerException.class) public void testThrowsIfBitmapPoolIsNull() { new BitmapResource(Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565), null); }
/** * @deprecated Use {@link #obtain(Resources, Resource)} instead, it can be unsafe to extract * {@link Bitmap}s from their wrapped {@link Resource}. */ @Deprecated public static LazyBitmapDrawableResource obtain(Context context, Bitmap bitmap) { return (LazyBitmapDrawableResource) obtain( context.getResources(), BitmapResource.obtain(bitmap, Glide.get(context).getBitmapPool())); }
@Test(expected = NullPointerException.class) public void testThrowsIfBitmapAndBitmapPoolAreNull() { new BitmapResource(null, null); }
@Nullable @Override public Resource<byte[]> transcode(@NonNull Resource<Drawable> toTranscode, @NonNull Options options) { Drawable drawable = toTranscode.get(); if (drawable instanceof BitmapDrawable) { return bitmapBytesTranscoder.transcode( BitmapResource.obtain(((BitmapDrawable) drawable).getBitmap(), bitmapPool), options); } else if (drawable instanceof GifDrawable) { return gifDrawableBytesTranscoder.transcode(toGifDrawableResource(toTranscode), options); } return null; }
@Test(expected = NullPointerException.class) public void testThrowsIfBitmapIsNull() { new BitmapResource(null, mock(BitmapPool.class)); }
@NonNull @Override public final Resource<Bitmap> transform( @NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) { if (!Util.isValidDimensions(outWidth, outHeight)) { throw new IllegalArgumentException( "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight + " less than or equal to zero and not Target.SIZE_ORIGINAL"); } BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); Bitmap toTransform = resource.get(); int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth; int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight; Bitmap transformed = transform(bitmapPool, toTransform, targetWidth, targetHeight); final Resource<Bitmap> result; if (toTransform.equals(transformed)) { result = resource; } else { result = BitmapResource.obtain(transformed, bitmapPool); } return result; }
@NonNull @Override public Resource<GifDrawable> transform( @NonNull Context context, @NonNull Resource<GifDrawable> resource, int outWidth, int outHeight) { GifDrawable drawable = resource.get(); // The drawable needs to be initialized with the correct width and height in order for a view // displaying it to end up with the right dimensions. Since our transformations may arbitrarily // modify the dimensions of our GIF, here we create a stand in for a frame and pass it to the // transformation to see what the final transformed dimensions will be so that our drawable can // report the correct intrinsic width and height. BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); Bitmap firstFrame = drawable.getFirstFrame(); Resource<Bitmap> bitmapResource = new BitmapResource(firstFrame, bitmapPool); Resource<Bitmap> transformed = wrapped.transform(context, bitmapResource, outWidth, outHeight); if (!bitmapResource.equals(transformed)) { bitmapResource.recycle(); } Bitmap transformedFrame = transformed.get(); drawable.setFrameTransformation(wrapped, transformedFrame); return resource; }
@NonNull @Override public final Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) { if (!Util.isValidDimensions(outWidth, outHeight)) { throw new IllegalArgumentException( "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight + " less than or equal to zero and not Target.SIZE_ORIGINAL"); } BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); Bitmap toTransform = resource.get(); int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth; int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight; Bitmap transformed = transform(context.getApplicationContext(), bitmapPool, toTransform, targetWidth, targetHeight); final Resource<Bitmap> result; if (toTransform.equals(transformed)) { result = resource; } else { result = BitmapResource.obtain(transformed, bitmapPool); } return result; }
public Resource<Bitmap> buildFrameResource(Bitmap bitmap, BitmapPool bitmapPool) { return new BitmapResource(bitmap, bitmapPool); } }
return BitmapResource.obtain(result, bitmapPool);