Search code examples
androidcropandroid-glide

Android glide: crop - cut off X pixels from image bottom


I need to cut off 20px from the image bottom and cache it so the device doesn't have to crop it over and over again every time the user sees the image again, otherwise it would be bad for battery etc. right?

This is what I have so far:

        Glide
            .with(context)
            .load(imgUrl)
            .into(holder.image)



fun cropOffLogo(originalBitmap: Bitmap) : Bitmap {

    return Bitmap.createBitmap(
        originalBitmap,
        0,
        0,
        originalBitmap.width,
        originalBitmap.height - 20
    )
}

how could I use cropOffLogo with glide?

EDIT:

I tried using https://github.com/bumptech/glide/wiki/Transformations#custom-transformations

private static class CutOffLogo extends BitmapTransformation {

    public CutOffLogo(Context context) {
        super(context);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
                               int outWidth, int outHeight) {

        Bitmap myTransformedBitmap = Bitmap.createBitmap(
                toTransform,
                10,
                10,
                toTransform.getWidth(),
                toTransform.getHeight() - 20);

        return myTransformedBitmap;
    }
}

And get those errors:

Modifier 'private' not allowed here

Modifier 'static' not allowed here

'BitmapTransformation()' in 'com.bumptech.glide.load.resource.bitmap.BitmapTransformation' cannot be applied to '(android.content.Context)' 

Solution

  • Transformation

    To cut some pixels from the image you can create new (custom) transformation. In Kotlin:

    class CutOffLogo : BitmapTransformation() {
        override fun transform(
            pool: BitmapPool,
            toTransform: Bitmap,
            outWidth: Int,
            outHeight: Int
        ): Bitmap =
            Bitmap.createBitmap(
                toTransform,
                0,
                0,
                toTransform.width,
                toTransform.height - 20   // numer of pixels
            )
    
        override fun updateDiskCacheKey(messageDigest: MessageDigest) {}
    }
    

    or in Java:

    public class CutOffLogo extends BitmapTransformation {
    
        @Override
        protected Bitmap transform(
                @NotNull BitmapPool pool,
                @NotNull Bitmap toTransform,
                int outWidth,
                int outHeight
        ) {
    
            return Bitmap.createBitmap(
                    toTransform,
                    0,
                    0,
                    toTransform.getWidth(),
                    toTransform.getHeight() - 20   // numer of pixels
            );
        }
    
        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
    
        }
    }
    

    Call it

    In Kotlin

    .transform(CutOffLogo())
    

    or in Java

    .transform(new CutOffLogo())