diff --git a/library/robolectric/src/com/bumptech/glide/loader/bitmap/ImageVideoBitmapLoadFactoryTest.java b/library/robolectric/src/com/bumptech/glide/loader/bitmap/ImageVideoBitmapLoadFactoryTest.java deleted file mode 100644 index e75cb5922e50c9571cc3591351ade72e95548421..0000000000000000000000000000000000000000 --- a/library/robolectric/src/com/bumptech/glide/loader/bitmap/ImageVideoBitmapLoadFactoryTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.bumptech.glide.loader.bitmap; - -import com.bumptech.glide.resize.load.Transformation; -import org.junit.Before; -import org.junit.Test; - -import static junit.framework.TestCase.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class ImageVideoBitmapLoadFactoryTest { - // Not magic, just a non 0 number. - private static final int IMAGE_SIDE = 200; - private static final String TRANSFORMATION_ID = "id"; - private Transformation transformation; - - @Before - public void setUp() throws Exception { - transformation = mock(Transformation.class); - when(transformation.getId()).thenReturn(TRANSFORMATION_ID); - } - - @Test - public void testIgnoresNullImageLoadFactory() { - ImageVideoBitmapLoadFactory factory = new ImageVideoBitmapLoadFactory(null, - mock(ResourceBitmapLoadFactory.class), - transformation); - - assertNotNull(factory.getLoadTask(new Object(), IMAGE_SIDE, IMAGE_SIDE)); - } - - @Test - public void testIgnoresNullVideoLoadFactory() { - ImageVideoBitmapLoadFactory factory = new ImageVideoBitmapLoadFactory(mock(ResourceBitmapLoadFactory.class), - null, transformation); - - assertNotNull(factory.getLoadTask(new Object(), IMAGE_SIDE, IMAGE_SIDE)); - } - - @Test(expected = IllegalArgumentException.class) - public void testThrowsIfNullVideoAndNullImageLoaders() { - new ImageVideoBitmapLoadFactory(null, null, transformation); - } - - @Test(expected = IllegalArgumentException.class) - public void testThrowsWithNullTransformationLoader() { - new ImageVideoBitmapLoadFactory(mock(ResourceBitmapLoadFactory.class), - mock(ResourceBitmapLoadFactory.class), null); - } -} diff --git a/library/robolectric/src/com/bumptech/glide/resize/DefaultResourceRunnerFactoryTest.java b/library/robolectric/src/com/bumptech/glide/resize/DefaultResourceRunnerFactoryTest.java index 8d6f69785e5895b76fb687b197fdb1bc0e81a2b1..3138b1ffb94173619b3fcaa97d9b25f7f478de4b 100644 --- a/library/robolectric/src/com/bumptech/glide/resize/DefaultResourceRunnerFactoryTest.java +++ b/library/robolectric/src/com/bumptech/glide/resize/DefaultResourceRunnerFactoryTest.java @@ -4,6 +4,7 @@ import android.os.Handler; import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; import com.bumptech.glide.resize.cache.DiskCache; import com.bumptech.glide.resize.cache.ResourceCache; +import com.bumptech.glide.resize.load.Transformation; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,6 +41,7 @@ public class DefaultResourceRunnerFactoryTest { ExecutorService service = mock(ExecutorService.class); ResourceCallback cb = mock(ResourceCallback.class); ResourceReferenceCounter resourceReferenceCounter = mock(ResourceReferenceCounter.class); + Transformation transformation = mock(Transformation.class); int width = 100; int height = 100; @@ -53,7 +55,8 @@ public class DefaultResourceRunnerFactoryTest { Metadata metadata = mock(Metadata.class); public ResourceRunner build() { - return factory.build(ID, width, height, cacheDecoder, fetcher, decoder, encoder, metadata, listener, cb); + return factory.build(ID, width, height, cacheDecoder, fetcher, decoder, transformation, encoder, metadata, + listener, cb); } } } diff --git a/library/robolectric/src/com/bumptech/glide/resize/EngineTest.java b/library/robolectric/src/com/bumptech/glide/resize/EngineTest.java index 04d1267934fecbe4894bd38db0f38b2b56421a32..2f9a78371a7750816e0bb9926275d4817eb5d922 100644 --- a/library/robolectric/src/com/bumptech/glide/resize/EngineTest.java +++ b/library/robolectric/src/com/bumptech/glide/resize/EngineTest.java @@ -2,6 +2,7 @@ package com.bumptech.glide.resize; import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; import com.bumptech.glide.resize.cache.ResourceCache; +import com.bumptech.glide.resize.load.Transformation; import org.junit.Before; import org.junit.Test; @@ -176,6 +177,7 @@ public class EngineTest { Resource resource = mock(Resource.class); Map runners = new HashMap(); ResourceReferenceCounter resourceReferenceCounter = mock(ResourceReferenceCounter.class); + Transformation transformation = mock(Transformation.class); int width = 100; int height = 100; @@ -192,12 +194,13 @@ public class EngineTest { engine = new Engine(factory, cache, runners, resourceReferenceCounter); - when(factory.build(eq(ID), eq(width), eq(height), eq(cacheDecoder), eq(fetcher), eq(decoder), eq(encoder), - eq(metadata), eq(engine), eq(cb))).thenReturn(runner); + when(factory.build(eq(ID), eq(width), eq(height), eq(cacheDecoder), eq(fetcher), eq(decoder), + eq(transformation), eq(encoder), eq(metadata), eq(engine), eq(cb))).thenReturn(runner); } public Engine.LoadStatus doLoad() { - return engine.load(ID, width, height, cacheDecoder, fetcher, decoder, encoder, metadata, cb); + return engine.load(ID, width, height, cacheDecoder, fetcher, decoder, transformation, encoder, metadata, + cb); } } } diff --git a/library/robolectric/src/com/bumptech/glide/resize/SourceResourceRunnerTest.java b/library/robolectric/src/com/bumptech/glide/resize/SourceResourceRunnerTest.java index 6613d771fff46ec39e1cfc8c8c7bff4efc633a6f..55449222ad5f4bd68699290bd6c838ece8ce67c3 100644 --- a/library/robolectric/src/com/bumptech/glide/resize/SourceResourceRunnerTest.java +++ b/library/robolectric/src/com/bumptech/glide/resize/SourceResourceRunnerTest.java @@ -2,10 +2,9 @@ package com.bumptech.glide.resize; import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; import com.bumptech.glide.resize.cache.DiskCache; +import com.bumptech.glide.resize.load.Transformation; import org.junit.Before; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -18,7 +17,6 @@ import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isNull; -import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -61,6 +59,7 @@ public class SourceResourceRunnerTest { verify(harness.cb).onResourceReady(eq(harness.result)); } + @Test public void testResourceIsWrittenToCacheIfFetchedAndDecoded() throws Exception { InputStream is = new ByteArrayInputStream(new byte[0]); @@ -68,20 +67,54 @@ public class SourceResourceRunnerTest { when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.result); final OutputStream expected = new ByteArrayOutputStream(); - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - DiskCache.Writer writer = (DiskCache.Writer) invocation.getArguments()[1]; - writer.write(expected); - return null; - } - }).when(harness.diskCache).put(eq(ID), any(DiskCache.Writer.class)); harness.runner.run(); + harness.runner.write(expected); verify(harness.encoder).encode(eq(harness.result), eq(expected)); } + @Test + public void testResourceIsTransformedBeforeBeingWrittenToCache() throws Exception { + InputStream is = new ByteArrayInputStream(new byte[0]); + when(harness.fetcher.loadResource(eq(harness.metadata))).thenReturn(is); + when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.result); + Resource transformed = mock(Resource.class); + when(harness.transformation.transform(eq(harness.result), eq(harness.width), eq(harness.height))) + .thenReturn(transformed); + + OutputStream expected = new ByteArrayOutputStream(); + harness.runner.run(); + harness.runner.write(expected); + + verify(harness.encoder).encode(eq(transformed), eq(expected)); + } + + @Test + public void testDecodedResourceIsRecycledIfTransformedResourceIsDifferent() throws Exception { + InputStream is = new ByteArrayInputStream(new byte[0]); + when(harness.fetcher.loadResource(eq(harness.metadata))).thenReturn(is); + when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.result); + Resource transformed = mock(Resource.class); + when(harness.transformation.transform(eq(harness.result), eq(harness.width), eq(harness.height))) + .thenReturn(transformed); + + harness.runner.run(); + + verify(harness.result).recycle(); + } + + @Test + public void testDecodedResourceIsNotRecycledIfResourceIsNotTransformed() throws Exception { + InputStream is = new ByteArrayInputStream(new byte[0]); + when(harness.fetcher.loadResource(eq(harness.metadata))).thenReturn(is); + when(harness.decoder.decode(eq(is), eq(harness.width), eq(harness.height))).thenReturn(harness.result); + + harness.runner.run(); + + verify(harness.result, never()).recycle(); + } + @Test public void testCallbackIsCalledIfFetchFails() throws Exception { Exception expected = new Exception("Test"); @@ -135,9 +168,14 @@ public class SourceResourceRunnerTest { Metadata metadata = mock(Metadata.class); ResourceCallback cb = mock(ResourceCallback.class); Resource result = mock(Resource.class); + Transformation transformation = mock(Transformation.class); int width = 150; int height = 200; SourceResourceRunner runner = new SourceResourceRunner(ID, width, height, - fetcher, decoder, encoder, diskCache, metadata, cb); + fetcher, decoder, transformation, encoder, diskCache, metadata, cb); + + public SourceResourceHarness() { + when(transformation.transform(eq(result), eq(width), eq(height))).thenReturn(result); + } } } diff --git a/library/robolectric/src/com/bumptech/glide/resize/load/ImageVideoBitmapLoadTaskTest.java b/library/robolectric/src/com/bumptech/glide/resize/load/ImageVideoBitmapLoadTaskTest.java deleted file mode 100644 index c6993110d8f8e2ac23bf7f73190eb41d91e5e390..0000000000000000000000000000000000000000 --- a/library/robolectric/src/com/bumptech/glide/resize/load/ImageVideoBitmapLoadTaskTest.java +++ /dev/null @@ -1,193 +0,0 @@ -package com.bumptech.glide.resize.load; - -import android.graphics.Bitmap; -import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.robolectric.RobolectricTestRunner; - -import java.util.ArrayList; -import java.util.List; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(RobolectricTestRunner.class) -public class ImageVideoBitmapLoadTaskTest { - private static final int IMAGE_SIDE = 235; - - @Test - public void testLoadsOnlyWithImageLoaderIfImageLoaderSucceeds() throws Exception { - Bitmap expected = Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8); - BitmapLoad imageLoad = mock(BitmapLoad.class); - when(imageLoad.load(any(BitmapPool.class))).thenReturn(expected); - - Bitmap notExpected = Bitmap.createBitmap(11, 11, Bitmap.Config.ARGB_8888); - BitmapLoad videoLoad = mock(BitmapLoad.class); - when(videoLoad.load(any(BitmapPool.class))).thenReturn(notExpected); - - Transformation transformation = mock(Transformation.class); - when(transformation.transform(any(Bitmap.class), any(BitmapPool.class), anyInt(), anyInt())) - .thenAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return invocation.getArguments()[0]; - } - }); - - ImageVideoBitmapLoad task = new ImageVideoBitmapLoad(imageLoad, videoLoad, - IMAGE_SIDE, IMAGE_SIDE, transformation); - - Bitmap result = task.load(mock(BitmapPool.class)); - - assertEquals(expected, result); - verify(videoLoad, never()).load(any(BitmapPool.class)); - } - - @Test - public void testLoadsWithImageLoaderIfVideoLoaderFails() throws Exception { - Bitmap expected = Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8); - ImageVideoBitmapLoad task = createBaseBitmapLoadTask(expected, null); - - Bitmap result = task.load(mock(BitmapPool.class)); - - assertEquals(expected, result); - } - - @Test - public void testLoadsWithVideoLoaderIfImageLoadFails() throws Exception { - Bitmap expected = Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8); - ImageVideoBitmapLoad task = createBaseBitmapLoadTask(null, expected); - - Bitmap result = task.load(mock(BitmapPool.class)); - - assertEquals(expected, result); - } - - @Test - public void testReturnsNullIfImageAndVideoLoadsFail() throws Exception { - ImageVideoBitmapLoad task = createBaseBitmapLoadTask(null, null); - - Bitmap result = task.load(mock(BitmapPool.class)); - - assertEquals(null, result); - } - - @Test - public void testTransformsImageResult() throws Exception { - Bitmap fromImage = Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8); - Bitmap transformed = Bitmap.createBitmap(9, 9, Bitmap.Config.ARGB_8888); - - Transformation transformation = mock(Transformation.class); - when(transformation.transform(eq(fromImage), any(BitmapPool.class), anyInt(), anyInt())) - .thenReturn(transformed); - - ImageVideoBitmapLoad task = createBaseBitmapLoadTask(fromImage, null, transformation); - - Bitmap result = task.load(mock(BitmapPool.class)); - - assertEquals(transformed, result); - } - - @Test - public void testTransformsVideoResult() throws Exception { - Bitmap fromVideo = Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8); - Bitmap transformed = Bitmap.createBitmap(9, 9, Bitmap.Config.ARGB_8888); - - Transformation transformation = mock(Transformation.class); - when(transformation.transform(eq(fromVideo), any(BitmapPool.class), anyInt(), anyInt())) - .thenReturn(transformed); - - ImageVideoBitmapLoad task = createBaseBitmapLoadTask(null, fromVideo, transformation); - - Bitmap result = task.load(mock(BitmapPool.class)); - - assertEquals(transformed, result); - } - - @Test - public void testTransformationIsGivenImageSize() throws Exception { - Transformation transformation = mock(Transformation.class); - - int width = 123; - int height = 456; - ImageVideoBitmapLoad task = createBaseBitmapLoadTask(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8), - Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8), transformation, width, height); - - task.load(mock(BitmapPool.class)); - - verify(transformation).transform(any(Bitmap.class), any(BitmapPool.class), eq(width), eq(height)); - } - - @Test - public void testGeneratesIdWithAllArguments() { - List allIds = new ArrayList(); - String initialId = getGeneratedIdWithMutatedArgumentIdAt(-1); - for (int i = 0; i < 2; i++) { - allIds.add(getGeneratedIdWithMutatedArgumentIdAt(i)); - } - for (String id : allIds) { - assertFalse(initialId.equals(id)); - } - } - - private ImageVideoBitmapLoad createBaseBitmapLoadTask(Bitmap imageDecoderResult, Bitmap videoDecoderResult) - throws Exception { - return createBaseBitmapLoadTask(imageDecoderResult, videoDecoderResult, null); - } - - private ImageVideoBitmapLoad createBaseBitmapLoadTask(Bitmap imageDecoderResult, - Bitmap videoDecoderResult, Transformation transformation) throws Exception { - return createBaseBitmapLoadTask(imageDecoderResult, videoDecoderResult, transformation, IMAGE_SIDE, IMAGE_SIDE); - } - - private ImageVideoBitmapLoad createBaseBitmapLoadTask(Bitmap imageDecoderResult, Bitmap videoDecoderResult, - Transformation transformation, int width, int height) throws Exception { - - BitmapLoad imageLoad = mock(BitmapLoad.class); - when(imageLoad.load(any(BitmapPool.class))).thenReturn(imageDecoderResult); - - BitmapLoad videoLoad = mock(BitmapLoad.class); - when(videoLoad.load(any(BitmapPool.class))).thenReturn(videoDecoderResult); - - if (transformation == null) { - transformation = mock(Transformation.class); - when(transformation.transform(any(Bitmap.class), any(BitmapPool.class), anyInt(), anyInt())) - .thenAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return invocation.getArguments()[0]; - } - }); - } - - return new ImageVideoBitmapLoad(imageLoad, videoLoad, width, height, transformation); - } - - private String getGeneratedIdWithMutatedArgumentIdAt(int argumentIndex) { - String imageLoadId = "ImageLoadId" + (argumentIndex == 0 ? "1" : ""); - BitmapLoad imageLoad = mock(BitmapLoad.class); - when(imageLoad.getId()).thenReturn(imageLoadId); - - String videoLoadId = "VideoLoadId" + (argumentIndex == 1 ? "1" : ""); - BitmapLoad videoLoad = mock(BitmapLoad.class); - when(videoLoad.getId()).thenReturn(videoLoadId); - - String transformationId = "TransformationId" + (argumentIndex == 2 ? "1" : ""); - Transformation transformation = mock(Transformation.class); - when(transformation.getId()).thenReturn(transformationId); - - ImageVideoBitmapLoad task = new ImageVideoBitmapLoad(imageLoad, videoLoad, IMAGE_SIDE, IMAGE_SIDE, - transformation); - return task.getId(); - } -} diff --git a/library/robolectric/src/com/bumptech/glide/resize/request/BitmapRequestTest.java b/library/robolectric/src/com/bumptech/glide/resize/request/BitmapRequestTest.java index 9aed2fdf68d873d86c0fc235bc512a15b8b3e04e..25b67a43e9f769845df34c50b318242f61316d36 100644 --- a/library/robolectric/src/com/bumptech/glide/resize/request/BitmapRequestTest.java +++ b/library/robolectric/src/com/bumptech/glide/resize/request/BitmapRequestTest.java @@ -18,6 +18,7 @@ import com.bumptech.glide.resize.ResourceCallback; import com.bumptech.glide.resize.ResourceDecoder; import com.bumptech.glide.resize.ResourceEncoder; import com.bumptech.glide.resize.load.DecodeFormat; +import com.bumptech.glide.resize.load.Transformation; import com.bumptech.glide.resize.target.Target; import org.junit.Before; import org.junit.Test; @@ -113,16 +114,16 @@ public class BitmapRequestTest { request.onSizeReady(100, 100); verify(harness.engine).load(anyString(), anyInt(), anyInt(), any(ResourceDecoder.class), - any(ResourceFetcher.class), any(ResourceDecoder.class), any(ResourceEncoder.class), eq(expected), - any(ResourceCallback.class)); + any(ResourceFetcher.class), any(ResourceDecoder.class), any(Transformation.class), + any(ResourceEncoder.class), eq(expected), any(ResourceCallback.class)); } @Test public void testEngineLoadCancelledOnCancel() { Engine.LoadStatus loadStatus = mock(Engine.LoadStatus.class); - when(harness.engine.load(anyString(), anyInt(), anyInt(), any(ResourceDecoder.class), any(ResourceFetcher.class), - any(ResourceDecoder.class), any(ResourceEncoder.class), any(Metadata.class), - any(ResourceCallback.class))).thenReturn(loadStatus); + when(harness.engine.load(anyString(), anyInt(), anyInt(), any(ResourceDecoder.class), + any(ResourceFetcher.class), any(ResourceDecoder.class), any(Transformation.class), + any(ResourceEncoder.class), any(Metadata.class), any(ResourceCallback.class))).thenReturn(loadStatus); BitmapRequest request = harness.getBuilder().build(); diff --git a/library/src/com/bumptech/glide/GenericRequestBuilder.java b/library/src/com/bumptech/glide/GenericRequestBuilder.java index 45cf3509d904cc85fc2f690e219e1b7ab2e17e02..eb59b08c0c8e0d1417fb554db89d580e7bdbba29 100644 --- a/library/src/com/bumptech/glide/GenericRequestBuilder.java +++ b/library/src/com/bumptech/glide/GenericRequestBuilder.java @@ -11,6 +11,8 @@ import com.bumptech.glide.resize.Metadata; import com.bumptech.glide.resize.Priority; import com.bumptech.glide.resize.RequestContext; import com.bumptech.glide.resize.ResourceDecoder; +import com.bumptech.glide.resize.bitmap.CenterCrop; +import com.bumptech.glide.resize.bitmap.FitCenter; import com.bumptech.glide.resize.load.BitmapDecoder; import com.bumptech.glide.resize.load.DecodeFormat; import com.bumptech.glide.resize.load.Downsampler; @@ -41,7 +43,7 @@ import java.util.List; */ public class GenericRequestBuilder { protected final Context context; - private final List transformations = new ArrayList(); + private final List> transformations = new ArrayList>(); private final ModelType model; private final RequestContext requestContext; private final Class imageType; @@ -75,13 +77,6 @@ public class GenericRequestBuilder getFinalTransformation() { switch (transformations.size()) { case 0: return Transformation.NONE; case 1: return transformations.get(0); default: - return new MultiTransformation(transformations); + return new MultiTransformation(transformations); } } } diff --git a/library/src/com/bumptech/glide/loader/bitmap/ImageVideoBitmapLoadFactory.java b/library/src/com/bumptech/glide/loader/bitmap/ImageVideoBitmapLoadFactory.java deleted file mode 100644 index b0f2232bec1ca91a4a3c9491a96e6edad04433be..0000000000000000000000000000000000000000 --- a/library/src/com/bumptech/glide/loader/bitmap/ImageVideoBitmapLoadFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.bumptech.glide.loader.bitmap; - -import com.bumptech.glide.loader.bitmap.model.ModelLoader; -import com.bumptech.glide.resize.load.BitmapDecoder; -import com.bumptech.glide.resize.load.BitmapLoad; -import com.bumptech.glide.resize.load.ImageVideoBitmapLoad; -import com.bumptech.glide.resize.load.Transformation; - -/** - * A base {@link BitmapLoadFactory} that composes {@link ModelLoader} and {@link BitmapDecoder} sub-components - * to create an {@link BitmapLoad} capable of loading a model that represents either an image or a video. - * - * @param The type of the model. - * @param The type of the resource that the image {@link ModelLoader} provides and the image {@link BitmapDecoder} can - * decode.` - * @param The type of resource that the video {@link ModelLoader} provides and the video {@link BitmapDecoder} can - * decode. - */ -public class ImageVideoBitmapLoadFactory implements BitmapLoadFactory { - private final ResourceBitmapLoadFactory imageLoadFactory; - private final ResourceBitmapLoadFactory videoLoadFactory; - private final Transformation transformation; - - public ImageVideoBitmapLoadFactory(ResourceBitmapLoadFactory imageLoadFactory, - ResourceBitmapLoadFactory videoLoadFactory, - Transformation transformation) { - this.imageLoadFactory = imageLoadFactory; - this.videoLoadFactory = videoLoadFactory; - if (imageLoadFactory == null && videoLoadFactory == null) { - throw new IllegalArgumentException("You must provide at least a video model loader and a video decoder or" - + " an image model loader and an image decoder"); - } - if (transformation == null) { - throw new IllegalArgumentException("You must provide a non null transformation"); - } - this.transformation = transformation; - } - - @Override - public BitmapLoad getLoadTask(T model, int width, int height) { - BitmapLoad imageLoad = null; - if (imageLoadFactory != null) { - imageLoad = imageLoadFactory.getLoadTask(model, width, height); - } - BitmapLoad videoLoad = null; - if (videoLoadFactory != null) { - videoLoad = videoLoadFactory.getLoadTask(model, width, height); - } - return new ImageVideoBitmapLoad(imageLoad, videoLoad, width, height, transformation); - } -} diff --git a/library/src/com/bumptech/glide/loader/bitmap/ResourceBitmapLoadFactory.java b/library/src/com/bumptech/glide/loader/bitmap/ResourceBitmapLoadFactory.java deleted file mode 100644 index 5de7141fd2a7ec32cff21331bc03415be81362ba..0000000000000000000000000000000000000000 --- a/library/src/com/bumptech/glide/loader/bitmap/ResourceBitmapLoadFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.bumptech.glide.loader.bitmap; - -import com.bumptech.glide.loader.bitmap.model.ModelLoader; -import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; -import com.bumptech.glide.resize.load.BitmapLoad; -import com.bumptech.glide.resize.load.BitmapDecoder; -import com.bumptech.glide.resize.load.DecodeFormat; -import com.bumptech.glide.resize.load.ResourceBitmapLoad; -import com.bumptech.glide.resize.load.Transformation; - -public class ResourceBitmapLoadFactory implements BitmapLoadFactory { - private final ModelLoader modelLoader; - private final BitmapDecoder decoder; - private DecodeFormat decodeFormat; - - public ResourceBitmapLoadFactory(ModelLoader modelLoader, BitmapDecoder decoder, - DecodeFormat decodeFormat) { - if (modelLoader == null) { - throw new IllegalArgumentException("Can't create load factory with null model loader"); - } - if (decoder == null) { - throw new IllegalArgumentException("Can't create load factory with null decoder"); - } - if (decodeFormat == null) { - throw new IllegalArgumentException("Can't create load factory with null decode format"); - } - this.modelLoader = modelLoader; - this.decoder = decoder; - this.decodeFormat = decodeFormat; - } - - @Override - public BitmapLoad getLoadTask(T model, int width, int height) { - final String id = modelLoader.getId(model); - final ResourceFetcher resourceFetcher = modelLoader.getResourceFetcher(model, width, height); - return new ResourceBitmapLoad(id, resourceFetcher, decoder, width, height, Transformation.NONE, - decodeFormat); - } -} diff --git a/library/src/com/bumptech/glide/resize/DefaultResourceRunnerFactory.java b/library/src/com/bumptech/glide/resize/DefaultResourceRunnerFactory.java index 4b9327d7d3b294a79dcf11a0fe11f34407997d06..070db017ac791c147bb41448f59d9e1f1d43d189 100644 --- a/library/src/com/bumptech/glide/resize/DefaultResourceRunnerFactory.java +++ b/library/src/com/bumptech/glide/resize/DefaultResourceRunnerFactory.java @@ -4,6 +4,7 @@ import android.os.Handler; import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; import com.bumptech.glide.resize.cache.DiskCache; import com.bumptech.glide.resize.cache.ResourceCache; +import com.bumptech.glide.resize.load.Transformation; import java.io.InputStream; import java.util.concurrent.ExecutorService; @@ -26,27 +27,16 @@ class DefaultResourceRunnerFactory implements ResourceRunnerFactory { this.referenceCounter = referenceCounter; } - /** - * - * @param id - * @param cacheDecoder - * @param fetcher - * @param decoder - * @param encoder - * @param metadata - * @param The type of the data the resource will be decoded from. - * @param The type of the resource that will be decoded. - * @return - */ @Override public ResourceRunner build(String id, int width, int height, ResourceDecoder cacheDecoder, ResourceFetcher fetcher, ResourceDecoder decoder, - ResourceEncoder encoder, Metadata metadata, EngineJobListener listener, ResourceCallback cb) { + Transformation transformation, ResourceEncoder encoder, Metadata metadata, EngineJobListener listener, + ResourceCallback cb) { EngineJob engineJob = new EngineJob(id, resourceCache, mainHandler, referenceCounter, listener, cb); SourceResourceRunner sourceRunner = new SourceResourceRunner(id, width, height, fetcher, decoder, - encoder, diskCache, metadata, engineJob); + transformation, encoder, diskCache, metadata, engineJob); return new ResourceRunner(id, width, height, diskCache, cacheDecoder, sourceRunner, service, bgHandler, engineJob); diff --git a/library/src/com/bumptech/glide/resize/Engine.java b/library/src/com/bumptech/glide/resize/Engine.java index 6acb717da20c540a69fabb0d67fa0518df7057d5..8faed4e2c2f9278b932a930cf5677d7b16d729a5 100644 --- a/library/src/com/bumptech/glide/resize/Engine.java +++ b/library/src/com/bumptech/glide/resize/Engine.java @@ -3,6 +3,7 @@ package com.bumptech.glide.resize; import android.os.Build; import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; import com.bumptech.glide.resize.cache.ResourceCache; +import com.bumptech.glide.resize.load.Transformation; import java.io.InputStream; import java.util.HashMap; @@ -57,9 +58,9 @@ public class Engine implements EngineJobListener, ResourceCache.ResourceRemovedL * @param The type of data the resource will be decoded from. * @param The type of the resource that will be decoded. */ - public LoadStatus load(String id, int width, int height, ResourceDecoder cacheDecoder, - ResourceFetcher fetcher, ResourceDecoder decoder, ResourceEncoder encoder, Metadata metadata, - ResourceCallback cb) { + public LoadStatus load(String id, int width, int height, ResourceDecoder cacheDecoder, + ResourceFetcher fetcher, ResourceDecoder decoder, Transformation transformation, + ResourceEncoder encoder, Metadata metadata, ResourceCallback cb) { Resource cached = cache.get(id); if (cached != null) { cb.onResourceReady(cached); @@ -73,8 +74,8 @@ public class Engine implements EngineJobListener, ResourceCache.ResourceRemovedL return new LoadStatus(cb, job); } - ResourceRunner runner = factory.build(id, width, height, cacheDecoder, fetcher, decoder, encoder, metadata, - this, cb); + ResourceRunner runner = factory.build(id, width, height, cacheDecoder, fetcher, decoder, transformation, + encoder, metadata, this, cb); runners.put(id, runner); runner.queue(); return new LoadStatus(cb, runner.getJob()); diff --git a/library/src/com/bumptech/glide/resize/ResourceRunnerFactory.java b/library/src/com/bumptech/glide/resize/ResourceRunnerFactory.java index 84d2d4b378c7fdc3e60fbe4d87daf17e78bf95f3..0abd6a18a29b23965f8f230b1193cacb30caf0d9 100644 --- a/library/src/com/bumptech/glide/resize/ResourceRunnerFactory.java +++ b/library/src/com/bumptech/glide/resize/ResourceRunnerFactory.java @@ -1,6 +1,7 @@ package com.bumptech.glide.resize; import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; +import com.bumptech.glide.resize.load.Transformation; import java.io.InputStream; @@ -11,6 +12,7 @@ interface ResourceRunnerFactory { * @param cacheDecoder * @param fetcher * @param decoder + * @param transformation * @param encoder * @param metadata * @param The type of data the resource will be decoded from. @@ -19,5 +21,6 @@ interface ResourceRunnerFactory { */ public ResourceRunner build(String id, int width, int height, ResourceDecoder cacheDecoder, ResourceFetcher fetcher, ResourceDecoder decoder, - ResourceEncoder encoder, Metadata metadata, EngineJobListener listener, ResourceCallback cb); + Transformation transformation, ResourceEncoder encoder, Metadata metadata, + EngineJobListener listener, ResourceCallback cb); } diff --git a/library/src/com/bumptech/glide/resize/SourceResourceRunner.java b/library/src/com/bumptech/glide/resize/SourceResourceRunner.java index 708a5ede4d16eb52caa8092d5dea59678902d90e..7f2a14707b42ac1a6c42bb164427991473ab3d01 100644 --- a/library/src/com/bumptech/glide/resize/SourceResourceRunner.java +++ b/library/src/com/bumptech/glide/resize/SourceResourceRunner.java @@ -2,6 +2,7 @@ package com.bumptech.glide.resize; import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; import com.bumptech.glide.resize.cache.DiskCache; +import com.bumptech.glide.resize.load.Transformation; import java.io.OutputStream; @@ -16,6 +17,7 @@ public class SourceResourceRunner implements Runnable, DiskCache.Writer, P private final int height; private final ResourceFetcher fetcher; private final ResourceDecoder decoder; + private Transformation transformation; private final ResourceEncoder encoder; private DiskCache diskCache; private Metadata metadata; @@ -23,13 +25,15 @@ public class SourceResourceRunner implements Runnable, DiskCache.Writer, P private Resource result; private volatile boolean isCancelled; - public SourceResourceRunner(String id, int width, int height, ResourceFetcher resourceFetcher, ResourceDecoder decoder, - ResourceEncoder encoder, DiskCache diskCache, Metadata metadata, ResourceCallback cb) { + public SourceResourceRunner(String id, int width, int height, ResourceFetcher resourceFetcher, + ResourceDecoder decoder, Transformation transformation, ResourceEncoder encoder, + DiskCache diskCache, Metadata metadata, ResourceCallback cb) { this.id = id; this.width = width; this.height = height; this.fetcher = resourceFetcher; this.decoder = decoder; + this.transformation = transformation; this.encoder = encoder; this.diskCache = diskCache; this.metadata = metadata; @@ -48,10 +52,16 @@ public class SourceResourceRunner implements Runnable, DiskCache.Writer, P } try { - result = null; T toDecode = fetcher.loadResource(metadata); if (toDecode != null) { - result = decoder.decode(toDecode, width, height); + Resource decoded = decoder.decode(toDecode, width, height); + if (decoded != null) { + Resource transformed = transformation.transform(decoded, width, height); + if (decoded != transformed) { + decoded.recycle(); + } + result = transformed; + } } if (result != null) { diskCache.put(id, this); diff --git a/library/src/com/bumptech/glide/resize/bitmap/CenterCrop.java b/library/src/com/bumptech/glide/resize/bitmap/CenterCrop.java new file mode 100644 index 0000000000000000000000000000000000000000..8641ab6c93a42918b4548d4fbd5e8c07b2e6531d --- /dev/null +++ b/library/src/com/bumptech/glide/resize/bitmap/CenterCrop.java @@ -0,0 +1,40 @@ +package com.bumptech.glide.resize.bitmap; + +import android.graphics.Bitmap; +import com.bumptech.glide.resize.Resource; +import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; +import com.bumptech.glide.resize.load.Transformation; +import com.bumptech.glide.resize.load.TransformationUtils; + +/** + * Scale the image so that either the width of the image matches the given width and the height of the image is + * greater than the given height or vice versa, and then crop the larger dimension to match the given dimension. + * + * Does not maintain the image's aspect ratio + */ +public class CenterCrop implements Transformation { + private BitmapPool pool; + + public CenterCrop(BitmapPool pool) { + this.pool = pool; + } + + @Override + public Resource transform(Resource resource, int outWidth, int outHeight) { + if (outWidth <= 0 || outHeight <= 0) { + throw new IllegalArgumentException("Cannot center crop image to width=" + outWidth + " and height=" + + outHeight); + } + final Bitmap toReuse = pool.get(outWidth, outHeight, resource.get().getConfig()); + Bitmap transformed = TransformationUtils.centerCrop(toReuse, resource.get(), outWidth, outHeight); + if (toReuse != transformed && !pool.put(toReuse)) { + toReuse.recycle(); + } + return new BitmapResource(transformed, pool); + } + + @Override + public String getId() { + return "com.bumptech.glide.resize.load.Transformation.CenterCrop"; + } +} diff --git a/library/src/com/bumptech/glide/resize/bitmap/FitCenter.java b/library/src/com/bumptech/glide/resize/bitmap/FitCenter.java new file mode 100644 index 0000000000000000000000000000000000000000..92aa37f0e69433aff07e0ec4cacae6fd92f372fb --- /dev/null +++ b/library/src/com/bumptech/glide/resize/bitmap/FitCenter.java @@ -0,0 +1,36 @@ +package com.bumptech.glide.resize.bitmap; + +import android.graphics.Bitmap; +import com.bumptech.glide.resize.Resource; +import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; +import com.bumptech.glide.resize.load.Transformation; +import com.bumptech.glide.resize.load.TransformationUtils; + +/** + * Scale the image uniformly (maintaining the image's aspect ratio) so that one of the dimensions of the image + * will be equal to the given dimension and the other will be less than the given dimension + */ +public class FitCenter implements Transformation { + private BitmapPool pool; + + public FitCenter(BitmapPool pool) { + this.pool = pool; + } + + @Override + public Resource transform(Resource resource, int outWidth, int outHeight) { + if (outWidth <= 0 || outHeight <= 0) { + throw new IllegalArgumentException("Cannot fit center image to within width=" + outWidth + " or height=" + + outHeight); + } + Bitmap transformed = TransformationUtils.fitCenter(resource.get(), pool, outWidth, outHeight); + return new BitmapResource(transformed, pool); + } + + @Override + public String getId() { + return "com.bumptech.glide.resize.load.Transformation.FitCenter"; + } +} + + diff --git a/library/src/com/bumptech/glide/resize/load/ImageVideoBitmapLoad.java b/library/src/com/bumptech/glide/resize/load/ImageVideoBitmapLoad.java deleted file mode 100644 index 92a4b2c8979976ccf8eb87c48c8890a74069cc87..0000000000000000000000000000000000000000 --- a/library/src/com/bumptech/glide/resize/load/ImageVideoBitmapLoad.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.bumptech.glide.resize.load; - -import android.graphics.Bitmap; -import android.util.Log; -import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; -import com.bumptech.glide.resize.Metadata; -import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; - -/** - * A base {@link BitmapLoad} that composes {@link ResourceFetcher} and {@link BitmapDecoder} to decode a - * bitmap from either an image or a video. - */ -public class ImageVideoBitmapLoad implements BitmapLoad { - private static final String TAG = "IVBL"; - - private final String id; - private final int width; - private final int height; - private final BitmapLoad imageLoad; - private final BitmapLoad videoLoad; - private final Transformation transformation; - private Metadata metadata; - - public ImageVideoBitmapLoad(BitmapLoad imageLoad, BitmapLoad videoLoad, int width, - int height, Transformation transformation) { - this.imageLoad = imageLoad; - this.videoLoad = videoLoad; - this.transformation = transformation; - this.width = width; - this.height = height; - - StringBuilder idBuilder = new StringBuilder(); - if (imageLoad != null) { - idBuilder.append(imageLoad.getId()); - } - if (videoLoad != null) { - idBuilder.append(videoLoad.getId()); - } - this.id = idBuilder.append(transformation.getId()).toString(); - } - - public void cancel() { - if (imageLoad != null) { - imageLoad.cancel(); - } - if (videoLoad != null) { - videoLoad.cancel(); - } - } - - @Override - public Metadata getMetadata() { - return metadata; - } - - @Override - public void setMetadata(Metadata metadata) { - this.metadata = metadata; - if (imageLoad != null) { - imageLoad.setMetadata(metadata); - } - if (videoLoad != null) { - videoLoad.setMetadata(metadata); - } - } - - @Override - public Bitmap load(BitmapPool bitmapPool) throws Exception { - long now = System.currentTimeMillis(); - Bitmap original = null; - if (imageLoad != null) { - original = imageLoad.load(bitmapPool); - } - if (Log.isLoggable(TAG, Log.VERBOSE)) { - Log.v(TAG, "loaded image in " + (System.currentTimeMillis() - now)); - now = System.currentTimeMillis(); - } - if (original == null && videoLoad != null) { - original = videoLoad.load(bitmapPool); - } - if (Log.isLoggable(TAG, Log.VERBOSE)) { - Log.v(TAG, "loaded video in " + (System.currentTimeMillis() - now)); - now = System.currentTimeMillis(); - } - - if (original == null) { - if (Log.isLoggable(TAG, Log.DEBUG)) { - Log.d(TAG, "Unable to decode either image or video"); - } - return null; - } - - Bitmap transformed = transformation.transform(original, bitmapPool, width, height); - if (Log.isLoggable(TAG, Log.VERBOSE)) { - Log.v(TAG, "transformed in " + (System.currentTimeMillis() - now)); - } - if (original != transformed) { - bitmapPool.put(original); - } - return transformed; - } - - public String getId() { - return id; - } -} diff --git a/library/src/com/bumptech/glide/resize/load/MultiTransformation.java b/library/src/com/bumptech/glide/resize/load/MultiTransformation.java index 637137a806e9d88e7f3ff13b9cd5de7f81bb4d2a..094c1e1b37b994306023ba88b3a277d324808405 100644 --- a/library/src/com/bumptech/glide/resize/load/MultiTransformation.java +++ b/library/src/com/bumptech/glide/resize/load/MultiTransformation.java @@ -1,25 +1,24 @@ package com.bumptech.glide.resize.load; -import android.graphics.Bitmap; -import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; +import com.bumptech.glide.resize.Resource; import java.util.List; /** * A transformation that applies an ordered array of one or more transformations to an image. */ -public class MultiTransformation extends Transformation { - private Transformation[] transformations; - private List transformationList; +public class MultiTransformation implements Transformation { + private Transformation[] transformations; + private List> transformationList; - public MultiTransformation(Transformation... transformations) { + public MultiTransformation(Transformation... transformations) { if (transformations.length < 1) { throw new IllegalArgumentException("MultiTransformation must contain at least one Transformation"); } this.transformations = transformations; } - public MultiTransformation(List transformationList) { + public MultiTransformation(List> transformationList) { if (transformationList.size() < 1) { throw new IllegalArgumentException("MultiTransformation must contain at least one Transformation"); } @@ -28,28 +27,28 @@ public class MultiTransformation extends Transformation { } @Override - public Bitmap transform(Bitmap bitmap, BitmapPool pool, int outWidth, int outHeight) { + public Resource transform(Resource resource, int outWidth, int outHeight) { // Set current to null so we don't recycle our original bitmap. Instead rely on the caller of this method to do // so. - Bitmap current = null; + Resource current = null; if (transformations != null) { - for (Transformation transformation : transformations) { - current = transform(current, transformation, pool, outWidth, outHeight); + for (Transformation transformation : transformations) { + current = transform(current, transformation, outWidth, outHeight); } } else { - for (Transformation transformation : transformationList) { - current = transform(current, transformation, pool, outWidth, outHeight); + for (Transformation transformation : transformationList) { + current = transform(current, transformation, outWidth, outHeight); } } return current; } - private Bitmap transform(Bitmap current, Transformation transformation, BitmapPool pool, int outWidth, + private Resource transform(Resource current, Transformation transformation, int outWidth, int outHeight) { - Bitmap transformed = transformation.transform(current, pool, outWidth, outHeight); - if (current != null && current != transformed && !pool.put(current)) { + Resource transformed = transformation.transform(current, outWidth, outHeight); + if (current != null && current != transformed) { current.recycle(); } diff --git a/library/src/com/bumptech/glide/resize/load/ResourceBitmapLoad.java b/library/src/com/bumptech/glide/resize/load/ResourceBitmapLoad.java deleted file mode 100644 index eae8549019d141da662ad02ecafa0293fb98c8ee..0000000000000000000000000000000000000000 --- a/library/src/com/bumptech/glide/resize/load/ResourceBitmapLoad.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.bumptech.glide.resize.load; - -import android.graphics.Bitmap; -import com.bumptech.glide.loader.bitmap.resource.ResourceFetcher; -import com.bumptech.glide.resize.Metadata; -import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; - -public class ResourceBitmapLoad implements BitmapLoad { - private final String id; - private final ResourceFetcher fetcher; - private final BitmapDecoder decoder; - private final int width; - private final int height; - private final Transformation transformation; - private final DecodeFormat decodeFormat; - private Metadata metadata; - - public ResourceBitmapLoad(String modelId, ResourceFetcher fetcher, BitmapDecoder decoder, int width, - int height, Transformation transformation, DecodeFormat decodeFormat) { - this.fetcher = fetcher; - this.decoder = decoder; - this.width = width; - this.height = height; - this.transformation = transformation; - this.decodeFormat = decodeFormat; - - this.id = modelId + decoder.getId() + width + height; - } - - @Override - public String getId() { - return id; - } - - @Override - public void cancel() { - if (fetcher != null) { - fetcher.cancel(); - } - } - - @Override - public Metadata getMetadata() { - return metadata; - } - - @Override - public void setMetadata(Metadata metadata) { - this.metadata = metadata; - } - - @Override - public Bitmap load(BitmapPool bitmapPool) throws Exception { - Bitmap original = null; - if (fetcher == null || decoder == null) { - return original; - } - - T resource = fetcher.loadResource(metadata); - if (resource != null) { - original = decoder.decode(resource, bitmapPool, width, height, decodeFormat); - } - Bitmap transformed = original; - if (original != null) { - transformed = transformation.transform(original, bitmapPool, width, height); - if (transformed != null && transformed != original) { - bitmapPool.put(original); - } - } - return transformed; - } -} diff --git a/library/src/com/bumptech/glide/resize/load/Transformation.java b/library/src/com/bumptech/glide/resize/load/Transformation.java index 9abcbf2fbd838c872d596302d9ba9523421e8499..fd06ee666dfb87ab985c39322a46636b2644228a 100644 --- a/library/src/com/bumptech/glide/resize/load/Transformation.java +++ b/library/src/com/bumptech/glide/resize/load/Transformation.java @@ -1,77 +1,49 @@ package com.bumptech.glide.resize.load; import android.graphics.Bitmap; +import com.bumptech.glide.resize.Resource; +import com.bumptech.glide.resize.bitmap.BitmapResource; import com.bumptech.glide.resize.bitmap_recycle.BitmapPool; /** * A class for performing an arbitrary transformation on a bitmap + * @param The type of the resource being transformed. */ -public abstract class Transformation { - private final String id = getClass().toString(); +public interface Transformation { /** - * Scale the image so that either the width of the image matches the given width and the height of the image is - * greater than the given height or vice versa, and then crop the larger dimension to match the given dimension. - * - * Does not maintain the image's aspect ratio + * A noop Transformation that simply returns the given bitmap */ - public static Transformation CENTER_CROP = new Transformation() { + public static Transformation NONE = new Transformation() { @Override - public Bitmap transform(Bitmap bitmap, BitmapPool pool, int outWidth, int outHeight) { - if (outWidth <= 0 || outHeight <= 0) { - throw new IllegalArgumentException("Cannot center crop image to width=" + outWidth + " and height=" - + outHeight); - } - final Bitmap toReuse = pool.get(outWidth, outHeight, bitmap.getConfig()); - return TransformationUtils.centerCrop(toReuse, bitmap, outWidth, outHeight); + public Resource transform(Resource resource, int outWidth, int outHeight) { + return resource; } - }; - /** - * Scale the image uniformly (maintaining the image's aspect ratio) so that one of the dimensions of the image - * will be equal to the given dimension and the other will be less than the given dimension - */ - public static Transformation FIT_CENTER = new Transformation() { @Override - public Bitmap transform(Bitmap bitmap, BitmapPool pool, int outWidth, int outHeight) { - if (outWidth <= 0 || outHeight <= 0) { - throw new IllegalArgumentException("Cannot fit center image to within width=" + outWidth + " or height=" - + outHeight); - } - return TransformationUtils.fitCenter(bitmap, pool, outWidth, outHeight); + public String getId() { + return "com.bumptech.glide.resize.load.Transformation.NONE"; } }; - /** - * A noop Transformation that simply returns the given bitmap - */ - public static Transformation NONE = new Transformation() { - @Override - public Bitmap transform(Bitmap bitmap, BitmapPool pool, int outWidth, int outHeight) { - return bitmap; - } - }; /** * Transform the given bitmap. It is also acceptable to simply return the given bitmap if no transformation is * required. * - * @param bitmap The bitmap to transform - * @param pool A bitmap pool to obtain reused bitmaps from and release unneeded bitmaps to. It is always safe - * to attempt to retrieve bitmaps. However, any bitmaps released to the pool must not be referenced - * elsewhere or returned. + * @param resource The resource to transform * @param outWidth The width of the view or target the bitmap will be displayed in * @param outHeight The height of the view or target the bitmap will be displayed in * @return The transformed bitmap */ - public abstract Bitmap transform(Bitmap bitmap, BitmapPool pool, int outWidth, int outHeight); + public abstract Resource transform(Resource resource, int outWidth, int outHeight); /** - * A method to get a unique identifier for this particular transformation that can be used as part of a cache key + * A method to get a unique identifier for this particular transformation that can be used as part of a cache key. + * The fully qualified class name for this class is appropriate if written out, but getClass().getName() is not + * because the name may be changed by proguard. * * @return A string that uniquely identifies this transformation from other transformations */ - public String getId() { - return id; - } + public String getId(); } diff --git a/library/src/com/bumptech/glide/resize/request/BitmapRequest.java b/library/src/com/bumptech/glide/resize/request/BitmapRequest.java index 4d7752a6e75b7befa33937d5e8bf7a66d9484994..dbb868b87b5178efb7c1b4958c7869a1350d003a 100644 --- a/library/src/com/bumptech/glide/resize/request/BitmapRequest.java +++ b/library/src/com/bumptech/glide/resize/request/BitmapRequest.java @@ -17,7 +17,9 @@ import com.bumptech.glide.resize.Resource; import com.bumptech.glide.resize.ResourceCallback; import com.bumptech.glide.resize.ResourceDecoder; import com.bumptech.glide.resize.ResourceEncoder; +import com.bumptech.glide.resize.bitmap.BitmapResource; import com.bumptech.glide.resize.load.DecodeFormat; +import com.bumptech.glide.resize.load.Transformation; import com.bumptech.glide.resize.target.Target; import java.io.InputStream; @@ -35,6 +37,7 @@ public class BitmapRequest implements Request, Target.SizeReadyCallback, R private final int errorResourceId; private final Context context; private final Class resourceClass; + private final Transformation transformation; private DecodeFormat decodeFormat; private final int animationId; private final RequestCoordinator requestCoordinator; @@ -72,6 +75,7 @@ public class BitmapRequest implements Request, Target.SizeReadyCallback, R this.decodeFormat = builder.decodeFormat; this.engine = builder.engine; this.requestContext = builder.requestContext; + this.transformation = builder.transformation; } @Override @@ -155,7 +159,8 @@ public class BitmapRequest implements Request, Target.SizeReadyCallback, R final Metadata metadata = new Metadata(priority, decodeFormat); loadedFromMemoryCache = true; - loadStatus = engine.load(id, width, height, cacheDecoder, resourceFetcher, decoder, encoder, metadata, this); + loadStatus = engine.load(id, width, height, cacheDecoder, resourceFetcher, decoder, transformation, + encoder, metadata, this); loadedFromMemoryCache = resource != null; } diff --git a/library/src/com/bumptech/glide/resize/request/BitmapRequestBuilder.java b/library/src/com/bumptech/glide/resize/request/BitmapRequestBuilder.java index a53a6cd19bdef3a7f17bab9c52b55b16802d7a7a..36bc9a18339ba666335ec32b2a07142b10285927 100644 --- a/library/src/com/bumptech/glide/resize/request/BitmapRequestBuilder.java +++ b/library/src/com/bumptech/glide/resize/request/BitmapRequestBuilder.java @@ -5,18 +5,18 @@ import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.view.animation.Animation; import com.bumptech.glide.RequestListener; -import com.bumptech.glide.loader.bitmap.BitmapLoadFactory; import com.bumptech.glide.resize.Engine; import com.bumptech.glide.resize.ImageManager; import com.bumptech.glide.resize.Priority; import com.bumptech.glide.resize.RequestContext; import com.bumptech.glide.resize.load.DecodeFormat; +import com.bumptech.glide.resize.load.Transformation; import com.bumptech.glide.resize.target.Target; /** * A simple builder class for {@link BitmapRequest}. * - * @param The model type the {@link BitmapRequest} will load an {@link Bitmap} from. + * @param The model type the {@link BitmapRequest} will load an {@link Bitmap} from. * @param The resource type the {@link BitmapRequest} will load an {@link Bitmap} from. */ public class BitmapRequestBuilder { @@ -38,6 +38,7 @@ public class BitmapRequestBuilder { DecodeFormat decodeFormat = DecodeFormat.PREFER_RGB_565; Engine engine; RequestContext requestContext; + Transformation transformation; public BitmapRequestBuilder(Class resourceClass) { this.resourceClass = resourceClass; @@ -128,6 +129,11 @@ public class BitmapRequestBuilder { return this; } + public BitmapRequestBuilder setTransformation(Transformation transformation) { + this.transformation = transformation; + return this; + } + public BitmapRequest build() { return new BitmapRequest(this); }