@SuppressWarnings({"unchecked", "PMD.UnnecessaryLocalBeforeReturn"}) private Resource<Drawable> newDrawableResource( Context context, Resource<Bitmap> transformed) { Resource<? extends Drawable> result = LazyBitmapDrawableResource.obtain(context.getResources(), transformed); return (Resource<Drawable>) result; }
@Nullable public static Resource<BitmapDrawable> obtain( @NonNull Resources resources, @Nullable Resource<Bitmap> bitmapResource) { if (bitmapResource == null) { return null; } return new LazyBitmapDrawableResource(resources, bitmapResource); }
@Test public void initialize_withWrappedInitializableResource_callsInitializeOnWrapped() { InitializableBitmapResource bitmapResource = mock(InitializableBitmapResource.class); resource = (LazyBitmapDrawableResource) LazyBitmapDrawableResource.obtain(resources, bitmapResource); resource.initialize(); verify(bitmapResource).initialize(); }
@Test public void recycle_doesNotRecycleWrappedBitmap() { resource.recycle(); assertThat(bitmap.isRecycled()).isFalse(); }
@Test public void initialize_withNonInitializableResource_doesNothing() { resource.initialize(); }
@Test public void get_returnsDrawableContainingWrappedBitmap() { BitmapDrawable drawable = resource.get(); assertThat(drawable.getBitmap()).isSameAs(bitmap); }
@Test public void getSize_returnsSizeOfWrappedResource() { when(bitmapResource.getSize()).thenReturn(100); assertThat(resource.getSize()).isEqualTo(100); }
@Test public void recycle_callsRecycleOnWrappedResource() { resource.recycle(); verify(bitmapResource).recycle(); }
@Nullable @Override public Resource<BitmapDrawable> transcode(@NonNull Resource<Bitmap> toTranscode, @NonNull Options options) { return LazyBitmapDrawableResource.obtain(resources, toTranscode); } }
public static LazyBitmapDrawableResource obtain(Resources resources, BitmapPool bitmapPool, Bitmap bitmap) { return new LazyBitmapDrawableResource(resources, bitmapPool, bitmap); }
/** * @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)); }
@Override public Resource<BitmapDrawable> decode(@NonNull DataType source, int width, int height, @NonNull Options options) throws IOException { Resource<Bitmap> bitmapResource = decoder.decode(source, width, height, options); return LazyBitmapDrawableResource.obtain(resources, bitmapResource); } }
/** * @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())); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(bitmapResource.get()).thenReturn(bitmap); resources = RuntimeEnvironment.application.getResources(); resource = (LazyBitmapDrawableResource) LazyBitmapDrawableResource.obtain(resources, bitmapResource); }
@Test public void obtain_withNullBitmapResource_returnsNull() { assertThat(LazyBitmapDrawableResource.obtain(resources, null)).isNull(); }
@Override public Resource<BitmapDrawable> transcode(Resource<Bitmap> toTranscode, Options options) { return LazyBitmapDrawableResource.obtain(resources, bitmapPool, toTranscode.get()); } }
public static LazyBitmapDrawableResource obtain(Context context, Bitmap bitmap) { return obtain(context.getResources(), Glide.get(context).getBitmapPool(), bitmap); }
@Override public Resource<BitmapDrawable> decode(DataType source, int width, int height, Options options) throws IOException { Resource<Bitmap> bitmapResource = decoder.decode(source, width, height, options); if (bitmapResource == null) { return null; } return LazyBitmapDrawableResource.obtain(resources, bitmapPool, bitmapResource.get()); } }
@Override public Resource<BitmapDrawable> transform( Context context, Resource<BitmapDrawable> drawableResourceToTransform, int outWidth, int outHeight) { BitmapDrawable drawableToTransform = drawableResourceToTransform.get(); Bitmap bitmapToTransform = drawableToTransform.getBitmap(); BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); BitmapResource bitmapResourceToTransform = BitmapResource.obtain(bitmapToTransform, bitmapPool); Resource<Bitmap> transformedBitmapResource = wrapped.transform(context, bitmapResourceToTransform, outWidth, outHeight); if (transformedBitmapResource.equals(bitmapResourceToTransform)) { return drawableResourceToTransform; } else { return LazyBitmapDrawableResource.obtain(context, transformedBitmapResource.get()); } }