Skip to content

Instantly share code, notes, and snippets.

@nightwolf738
Created March 20, 2019 12:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nightwolf738/4c341d7f806fec3352d10bfa941c65a3 to your computer and use it in GitHub Desktop.
Save nightwolf738/4c341d7f806fec3352d10bfa941c65a3 to your computer and use it in GitHub Desktop.
Masking bitmap with given destinationImage. Useful for masking ImageViews
fun Bitmap.maskWith(destinationImage: Bitmap, mode: PorterDuff.Mode): Bitmap {
val result = this.copy(Bitmap.Config.ARGB_8888, true)
val canvas = Canvas(result)
val paint = Paint()
canvas.drawBitmap(destinationImage, 0f, 0f, paint)
paint.xfermode = PorterDuffXfermode(mode)
canvas.drawBitmap(this, 0f, 0f, paint)
return result
}
val mask = BitmapFactory.decodeResource(resources, R.drawable.mask)
val image = BitmapFactory.decodeResource(resources, R.drawable.image)
val maskedBitmap = image.maskWith(mask, PorterDuff.Mode.DST_ATOP)
imageView.setImageBitmap(maskedBitmap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment