Created
March 20, 2019 12:32
-
-
Save nightwolf738/4c341d7f806fec3352d10bfa941c65a3 to your computer and use it in GitHub Desktop.
Masking bitmap with given destinationImage. Useful for masking ImageViews
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
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