10

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!

JMRboosties
  • 15,500
  • 20
  • 76
  • 116
  • perhaps their test cases would be useful: https://github.com/bumptech/glide/blob/bf53fa653849217ddd25d6b3c54d8e05312369ee/library/src/test/java/com/bumptech/glide/load/resource/bitmap/BitmapTransformationTest.java – Dave S Jun 10 '15 at 23:03
  • I don't see anything obviously wrong here, want to file an issue on GitHub so we can investigate? https://github.com/bumptech/glide/issues/new – Sam Judd Jun 10 '15 at 23:59
  • @samajudd just added the issue, thanks for replying! – JMRboosties Jun 11 '15 at 00:11
  • Crosslink: https://github.com/bumptech/glide/issues/492 – TWiStErRob Jun 11 '15 at 16:07

1 Answers1

20

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:

// TODO remove after transformation is done
.diskCacheStrategy(SOURCE) // override default RESULT cache and apply transform always
.skipMemoryCache(true) // do not reuse the transformed result while running

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.

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • Thank you. As for your additional note, what I'm trying to do is make the image centered at the top (where faces normally are). Like if your imageview bounds were only 1 square in width/height, and your image looked like this: [1][2][3] [4][5][6] [7][8][9] I'd want square 2 to be what the imageview shows. – JMRboosties Jun 11 '15 at 16:29
  • Well, comments put it all in 1 paragraph, but i think you get the idea. Anyways your question solves my immediate problem of transform not being called, thanks! – JMRboosties Jun 11 '15 at 16:30