@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { if (toTransform.getWidth() < (outWidth / 2) && toTransform.getHeight() < (outHeight / 2)) { return toTransform; } final float inAspectRatio = (float) toTransform.getWidth() / toTransform.getHeight(); final float outAspectRatio = (float) outWidth / outHeight; if (inAspectRatio < outAspectRatio) { outWidth = (int)(outHeight * inAspectRatio); } final Bitmap toReuse = pool.get(outWidth, outHeight, toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.centerCrop(toReuse, toTransform, outWidth, outHeight); if (toReuse != null && toReuse != transformed && !pool.put(toReuse)) { toReuse.recycle(); } return transformed; }
/** * Transformations that produce a different output color/shape/image etc will end up returning * a {@link Bitmap} based on the original {@link Drawable} but with the transformation applied. */ @Test public void load_withColorDrawable_fixedSize_nonUnitRequiredTransform_returnsBitmapDrawable() throws ExecutionException, InterruptedException { Drawable colorDrawable = new ColorDrawable(Color.RED); Drawable result = Glide.with(context) .load(colorDrawable) .apply(new RequestOptions() .circleCrop()) .submit(100, 100) .get(); Bitmap redSquare = Bitmap.createBitmap(100, 100, Config.ARGB_8888); Canvas canvas = new Canvas(redSquare); canvas.drawColor(Color.RED); BitmapPool bitmapPool = mock(BitmapPool.class); when(bitmapPool.get(100, 100, Bitmap.Config.ARGB_8888)) .thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); Bitmap expected = TransformationUtils.circleCrop(bitmapPool, redSquare, 100, 100); assertThat(result).isInstanceOf(BitmapDrawable.class); Bitmap bitmap = ((BitmapDrawable) result).getBitmap(); assertThat(bitmap.getWidth()).isEqualTo(100); assertThat(bitmap.getHeight()).isEqualTo(100); for (int x = 0; x < bitmap.getWidth(); x++) { for (int y = 0; y < bitmap.getHeight(); y++) { assertThat(bitmap.getPixel(x, y)).isEqualTo(expected.getPixel(x, y)); } } }
private Bitmap centerCrop(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { final Bitmap toReuse = pool.get(outWidth, outHeight, getSafeConfig(toTransform)); final Bitmap transformed = TransformationUtils.centerCrop(toReuse, toTransform, outWidth, outHeight); if (toReuse != null && toReuse != transformed && !pool.put(toReuse)) { toReuse.recycle(); } return transformed; }
private Bitmap fitCenter(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return TransformationUtils.fitCenter(toTransform, pool, outWidth, outHeight); }
@Override protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { return TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight); }
@Override protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { this.size = Math.max(outWidth, outHeight); return TransformationUtils.centerCrop(pool, toTransform, size, size); }