forked from bumptech/glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bumptech#727 from josemontiel/master
Implemented CENTER_INSIDE support
- Loading branch information
Showing
7 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
library/src/main/java/com/bumptech/glide/load/resource/bitmap/CenterInside.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.bumptech.glide.load.resource.bitmap; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
|
||
import java.security.MessageDigest; | ||
|
||
/** | ||
* Returns the image with its original size if its dimensions match or are smaller | ||
* than the target's, couple with {@link android.widget.ImageView.ScaleType#CENTER_INSIDE} | ||
* in order to center it in Target. If not, then it is scaled 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 (maintaining the image's aspect ratio). | ||
*/ | ||
public class CenterInside extends BitmapTransformation { | ||
private static final String ID = "com.bumptech.glide.load.resource.bitmap.CenterInside"; | ||
private static final byte[] ID_BYTES = ID.getBytes(CHARSET); | ||
|
||
public CenterInside(Context context) { | ||
super(context); | ||
} | ||
|
||
public CenterInside(BitmapPool bitmapPool) { | ||
super(bitmapPool); | ||
} | ||
|
||
@Override | ||
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, | ||
int outHeight) { | ||
return TransformationUtils.centerInside(pool, toTransform, outWidth, outHeight); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return o instanceof CenterInside; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return ID.hashCode(); | ||
} | ||
|
||
@Override | ||
public void updateDiskCacheKey(MessageDigest messageDigest) { | ||
messageDigest.update(ID_BYTES); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
library/src/test/java/com/bumptech/glide/load/resource/bitmap/CenterInsideTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.bumptech.glide.load.resource.bitmap; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.graphics.Matrix; | ||
import android.graphics.Paint; | ||
|
||
import com.bumptech.glide.load.Transformation; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter; | ||
import com.bumptech.glide.tests.KeyAssertions; | ||
import com.bumptech.glide.tests.Util; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
import org.robolectric.shadows.ShadowCanvas; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@Config(manifest = Config.NONE, sdk = 18, shadows = { CenterInsideTest.DrawNothingCanvas.class }) | ||
public class CenterInsideTest { | ||
|
||
@Mock Resource<Bitmap> resource; | ||
@Mock Transformation<Bitmap> transformation; | ||
private BitmapPool pool; | ||
private CenterInside centerInside; | ||
private int bitmapWidth; | ||
private int bitmapHeight; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
bitmapWidth = 100; | ||
bitmapHeight = 100; | ||
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888); | ||
when(resource.get()).thenReturn(bitmap); | ||
|
||
pool = new BitmapPoolAdapter(); | ||
|
||
centerInside = new CenterInside(pool); | ||
} | ||
|
||
@Test | ||
public void testReturnsGivenResourceIfMatchesSizeExactly() { | ||
Resource<Bitmap> result = | ||
centerInside.transform(resource, bitmapWidth, bitmapHeight); | ||
|
||
assertEquals(resource, result); | ||
} | ||
|
||
@Test | ||
public void testReturnsGivenResourceIfSmallerThanTarget() { | ||
Resource<Bitmap> result = | ||
centerInside.transform(resource, 150, 150); | ||
|
||
assertEquals(resource, result); | ||
} | ||
|
||
@Test | ||
public void testReturnsNewResourceIfLargerThanTarget() { | ||
Resource<Bitmap> result = | ||
centerInside.transform(resource, 50, 50); | ||
|
||
assertNotEquals(resource, result); | ||
} | ||
|
||
|
||
@Test | ||
public void testDoesNotRecycleGivenResourceIfMatchesSizeExactly() { | ||
centerInside.transform(resource, bitmapWidth, bitmapHeight); | ||
|
||
verify(resource, never()).recycle(); | ||
} | ||
|
||
@Test | ||
public void testDoesNotRecycleGivenResource() { | ||
centerInside.transform(resource, 50, 50); | ||
|
||
verify(resource, never()).recycle(); | ||
} | ||
|
||
@Test | ||
public void testEquals() throws NoSuchAlgorithmException { | ||
KeyAssertions.assertSame(centerInside, new CenterInside(pool)); | ||
|
||
doAnswer(new Util.WriteDigest("other")).when(transformation) | ||
.updateDiskCacheKey(any(MessageDigest.class)); | ||
KeyAssertions.assertDifferent(centerInside, transformation); | ||
} | ||
|
||
@Implements(Canvas.class) | ||
public static final class DrawNothingCanvas extends ShadowCanvas { | ||
|
||
@Implementation | ||
@Override | ||
public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) { | ||
// Do nothing. | ||
} | ||
} | ||
} |