I'm new to the Glide library, following the Transformations guide found here: https://github.com/bumptech/glide/wiki/Transformations
I'm trying to create a custom transformation, but when I place a breakline in the Transformation class's transform
method, I can see that it is never called.
Below is my code:
private static class CustomTransformation extends BitmapTransformation {
private Context aContext;
public CustomTransformation(Context context) {
super(context);
aContext = context;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return bitmapChanger(toTransform, 1080, (int) aContext.getResources().getDimension(R.dimen.big_image));
}
@Override
public String getId() {
return "some_id";
}
}
private static Bitmap bitmapChanger(Bitmap bitmap, int desiredWidth, int desiredHeight) {
float originalWidth = bitmap.getWidth();
float originalHeight = bitmap.getHeight();
float scaleX = desiredWidth / originalWidth;
float scaleY = desiredHeight / originalHeight;
//Use the larger of the two scales to maintain aspect ratio
float scale = Math.max(scaleX, scaleY);
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
//If the scaleY is greater, we need to center the image
if(scaleX < scaleY) {
float tx = (scale * originalWidth - desiredWidth) / 2f;
matrix.postTranslate(-tx, 0f);
}
return Bitmap.createBitmap(bitmap, 0, 0, (int) originalWidth, (int) originalHeight, matrix, true);
}
I've tried initiating Glide in two ways:
Glide.with(this).load(url).asBitmap().transform(new CustomTransformation(this)).into(imageView);
and
Glide.with(this).load(url).bitmapTransform(new CustomTransformation(this)).into(imageView);
But neither work. Any ideas? Again, I'm not looking for advice on the Matrix itself, I just don't understand why transform(...)
isn't being called at all. Thanks!