裁剪图像/可绘制成三角形 [英] Crop an Image/drawable to a triangle

查看:181
本文介绍了裁剪图像/可绘制成三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android应用中将图像裁剪为三角形,以便仅获得图像的三角形部分。如何才能做到这一点?我已经成功完成了正方形和圆形形状的裁剪,但是无法对三角形进行相同的裁剪。

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.

谢谢。

David

推荐答案

您可以使用遮罩图像。在这种情况下,该图像应为三角形(或您需要的任何其他形状),如下所示:

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);

这篇关于裁剪图像/可绘制成三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆