Crop an Image/drawable to a triangle

2019-09-01 02:27发布

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 and Circle shapes but I'm unable to do the same for the triangle. Could someone help me with this.

Thanks.

David

1条回答
一夜七次
2楼-- · 2019-09-01 03:16

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);
查看更多
登录 后发表回答