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!
You're most likely experiencing caching issues. The first time you compiled and executed your code the result of the transformation was cached so next time it doesn't have to be applied to the same source image.
Each transformation has a getId() method which is used in determining whether the transformation result has changed. Usually transformations don't change, but are either applied or not. You can change it on every build while developing, but it could be tedius.
To work around this problem you can add the following two calls to your Glide load line:
The first one can be changed to NONE, but then you would have to wait for the url to load from the internet every time, instead of just reading the image from the phone. The second one is useful if you have can navigate to and away from the transformation in question and want to debug it for example. It helps to not need a restart after every load to clear the memory cache.
Don't forget to remove these after you're done with the Transformation's development, because they affect production performance a lot and should be used after much consideration only, if at all.
Note
It looks like you're trying to resize your image to a certain size before loading, you can use
.override(width, height)
in combination with.centerCrop()
/.fitCenter()
/.dontTransform()
for that.