@Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return TransformationUtils.fitCenter(pool, toTransform, outWidth, outHeight); }
/** * If the Bitmap is smaller or equal to the Target it returns the original size, if not then * {@link #fitCenter(BitmapPool, Bitmap, int, int)} is called instead. * * @param pool The BitmapPool obtain a bitmap from. * @param inBitmap The Bitmap to center. * @param width The width in pixels of the target. * @param height The height in pixels of the target. * @return returns input Bitmap if smaller or equal to target, or toFit if the Bitmap's width or * height is larger than the given dimensions */ public static Bitmap centerInside(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int width, int height) { if (inBitmap.getWidth() <= width && inBitmap.getHeight() <= height) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "requested target size larger or equal to input, returning input"); } return inBitmap; } else { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "requested target size too big for input, fit centering instead"); } return fitCenter(pool, inBitmap, width, height); } }
@Test public void testFitCenterReturnsGivenBitmapIfGivenBitmapMatchesExactly() { Bitmap toFit = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, toFit, toFit.getWidth(), toFit.getHeight()); assertTrue(toFit == transformed); }
@Test public void testFitCenterReturnsGivenBitmapIfGivenBitmapWidthMatchesExactly() { Bitmap toFit = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, toFit, toFit.getWidth(), toFit.getHeight() * 2); assertTrue(toFit == transformed); }
@Test public void testFitCenterReturnsGivenBitmapIfGivenBitmapHeightMatchesExactly() { Bitmap toFit = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, toFit, toFit.getWidth() * 2, toFit.getHeight()); assertTrue(toFit == transformed); }
@Test public void testFitCenterUsesFloorInsteadOfRoundingForOutputBitmapSize() { Bitmap toTransform = Bitmap.createBitmap(1230, 1640, Bitmap.Config.RGB_565); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, toTransform, 1075, 1366); assertEquals(1024, transformed.getWidth()); assertEquals(1366, transformed.getHeight()); }
@Test @Config(sdk = 19) public void testFitCenterHandlesBitmapsWithNullConfigs() { Bitmap toFit = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565); toFit.setConfig(null); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, toFit, 50, 50); assertEquals(Bitmap.Config.ARGB_8888, transformed.getConfig()); }
@Test public void testFitCenterSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); reset(bitmapPool); when(bitmapPool.get(eq(toReuse.getWidth()), eq(toReuse.getHeight()), eq(toReuse.getConfig()))) .thenReturn(toReuse); toReuse.setHasAlpha(false); toTransform.setHasAlpha(true); Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight()); assertEquals(toReuse, result); assertTrue(result.hasAlpha()); }
@Test public void testFitCenterSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); toTransform.setHasAlpha(true); Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform, toTransform.getWidth() / 2, toTransform.getHeight() / 2); assertTrue(result.hasAlpha()); }
@Test public void testFitCenterSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); reset(bitmapPool); when(bitmapPool.get(eq(toReuse.getWidth()), eq(toReuse.getHeight()), eq(toReuse.getConfig()))) .thenReturn(toReuse); toReuse.setHasAlpha(true); toTransform.setHasAlpha(false); Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight()); assertEquals(toReuse, result); assertFalse(result.hasAlpha()); }
@Test public void testFitCenterSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); toTransform.setHasAlpha(false); Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform, toTransform.getWidth() / 2, toTransform.getHeight() / 2); assertFalse(result.hasAlpha()); }
@Test public void testFitCenterWithSmallWideBitmap() { final int maxSide = 500; Bitmap smallWide = Bitmap.createBitmap(400, 40, Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, smallWide, maxSide, maxSide); assertHasOriginalAspectRatio(smallWide, transformed); assertBitmapFitsExactlyWithinBounds(maxSide, transformed); }
@Test public void testFitCenterWithTooSmallSquareBitmap() { final int maxSide = 500; Bitmap smallSquare = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, smallSquare, maxSide, maxSide); assertHasOriginalAspectRatio(smallSquare, transformed); assertBitmapFitsExactlyWithinBounds(maxSide, transformed); }
@Test public void testFitCenterWithWideBitmap() { final int maxSide = 500; Bitmap wide = Bitmap.createBitmap(2000, 100, Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, wide, maxSide, maxSide); assertHasOriginalAspectRatio(wide, transformed); assertBitmapFitsExactlyWithinBounds(maxSide, transformed); }
@Test public void testFitCenterWithSquareBitmap() { final int maxSide = 500; Bitmap square = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, square, maxSide, maxSide); assertHasOriginalAspectRatio(square, transformed); assertBitmapFitsExactlyWithinBounds(maxSide, transformed); }
@Test public void testFitCenterWithTallBitmap() { final int maxSide = 500; Bitmap tall = Bitmap.createBitmap(65, 3000, Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, tall, maxSide, maxSide); assertHasOriginalAspectRatio(tall, transformed); assertBitmapFitsExactlyWithinBounds(maxSide, transformed); }
@Test public void testFitCenterWithSmallTallBitmap() { final int maxSide = 500; Bitmap smallTall = Bitmap.createBitmap(10, 400, Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, smallTall, maxSide, maxSide); assertHasOriginalAspectRatio(smallTall, transformed); assertBitmapFitsExactlyWithinBounds(maxSide, transformed); }
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return TransformationUtils.fitCenter(toTransform, pool, outWidth, outHeight); }
@Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return TransformationUtils.fitCenter(pool, toTransform, outWidth, outHeight); }
/** * If the Bitmap is smaller or equal to the Target it returns the original size, if not then * {@link #fitCenter(BitmapPool, Bitmap, int, int)} is called instead. * * @param pool The BitmapPool obtain a bitmap from. * @param inBitmap The Bitmap to center. * @param width The width in pixels of the target. * @param height The height in pixels of the target. * @return returns input Bitmap if smaller or equal to target, or toFit if the Bitmap's width or * height is larger than the given dimensions */ public static Bitmap centerInside(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int width, int height) { if (inBitmap.getWidth() <= width && inBitmap.getHeight() <= height) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "requested target size larger or equal to input, returning input"); } return inBitmap; } else { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "requested target size too big for input, fit centering instead"); } return fitCenter(pool, inBitmap, width, height); } }