Skip to content

Instantly share code, notes, and snippets.

@squeeish
Created September 25, 2017 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squeeish/9030cf946c78c8d304cb21458ffb277d to your computer and use it in GitHub Desktop.
Save squeeish/9030cf946c78c8d304cb21458ffb277d to your computer and use it in GitHub Desktop.
AlphaTransformation for Glide 3.7.0
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
public class AlphaTransformation implements Transformation<Bitmap> {
private BitmapPool mBitmapPool;
private int alpha;
public AlphaTransformation(Context context, int alpha) {
this(Glide.get(context).getBitmapPool());
this.alpha = alpha;
}
public AlphaTransformation(BitmapPool pool) {
mBitmapPool = pool;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int width = source.getWidth();
int height = source.getHeight();
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(width, height, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, config);
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAlpha(alpha);
canvas.drawBitmap(source, 0, 0, paint);
return BitmapResource.obtain(bitmap, mBitmapPool);
}
/*
getId()) method describes an unique identifier for this particular transformation.
Glide uses that key as part of the caching system.
Makes sure to make it unique to avoid unexpected issues.
*/
@Override public String getId() {
return "AlphaTransformation()";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment