Skip to content Skip to sidebar Skip to footer

Crop An Image/drawable To A Triangle

I'm trying to crop an image to triangle in my Android App so that I only get the triangle portion of the image. How can this be done? I've successfully done the cropping for Square

Solution 1:

You can Use a "Mask" Image.. this image should be a triangle in your case (or any other shape you need) like this :

 ImageView mImageView= (ImageView)findViewById(R.id.imageview_id);
 Bitmap original = BitmapFactory.decodeResource(getResources(),R.drawable.contentimage);
 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.background_frame);

Post a Comment for "Crop An Image/drawable To A Triangle"