Last active
June 17, 2016 06:24
-
-
Save chittaranjan-khuntia/7cccb69cd0c44b24baf6a416fb0ff79b to your computer and use it in GitHub Desktop.
Crop an image to a shape using masking in Android
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
public void makeMaskImage(ImageView mImageView, int mContent){ | |
Bitmap original = BitmapFactory.decodeResource(getResources(), mContent); | |
Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.mask); | |
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888); | |
Canvas mCanvas = new Canvas(result); | |
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); | |
mCanvas.drawBitmap(original, 0, 0, null); | |
mCanvas.drawBitmap(mask, 0, 0, paint); | |
paint.setXfermode(null); | |
mImageView.setImageBitmap(result); | |
mImageView.setScaleType(ScaleType.CENTER); | |
mImageView.setBackgroundResource(R.drawable.frame); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment