Created
December 18, 2017 23:36
-
-
Save vbuberen/134f4b8a521ad90f1ebf371f01848532 to your computer and use it in GitHub Desktop.
Glide v4 mask transformation. Supports vector drawables as mask drawable for transformation.
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.graphics.* | |
import android.graphics.drawable.Drawable | |
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool | |
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation | |
import java.io.UnsupportedEncodingException | |
import java.security.MessageDigest | |
class MaskImageTransformation(private val mask: Drawable) : BitmapTransformation() { | |
override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap { | |
val width = toTransform.width | |
val height = toTransform.height | |
val bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888) | |
bitmap.setHasAlpha(true) | |
val canvas = Canvas(bitmap) | |
mask.setBounds(0, 0, width, height) | |
mask.draw(canvas) | |
canvas.drawBitmap(toTransform, 0f, 0f, paint) | |
return bitmap | |
} | |
@Throws(UnsupportedEncodingException::class) | |
override fun updateDiskCacheKey(messageDigest: MessageDigest) { | |
messageDigest.update(ID_BYTES) | |
} | |
override fun hashCode(): Int { | |
return ID.hashCode() | |
} | |
override fun equals(other: Any?): Boolean { | |
return other is MaskImageTransformation | |
} | |
companion object { | |
private val ID = "MaskImageTransformation" | |
private val ID_BYTES = ID.toByteArray(charset(STRING_CHARSET_NAME)) | |
private val paint = Paint() | |
init { | |
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment