Created
September 25, 2017 18:27
-
-
Save squeeish/9030cf946c78c8d304cb21458ffb277d to your computer and use it in GitHub Desktop.
AlphaTransformation for Glide 3.7.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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