Android的 - 裁剪从multipoints图像(Android - Crop an imag

2019-06-26 10:27发布

我需要裁剪位图,但不是具有矩形裁剪图像(我成功设法做到),我需要它是由坐标定义的任何形式

我下面从这个线程的答案: 切割多ploygon出位图,并把它放在透明 ,并试图实现它,但不幸的是它不裁剪图像。

我没有在说明书中,但似乎有一个错误的地方。 图像绘制矩形方式。 我缺少的东西吗?

Bitmap originalBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
// Image cropped
Bitmap croppedBitmap=Bitmap.createBitmap(originalBitmap, 10, 10, 200, 200);
Canvas canvas=new Canvas(croppedBitmap);

// Create a path
Path path=new Path();
path.setFillType(FillType.INVERSE_EVEN_ODD);
path.moveTo(0, 0);
path.moveTo(0, 100);
path.moveTo(100, 0);
path.moveTo(0, 0);

// Paint with Xfermode
Paint paint=new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

// Draw the path
canvas.drawPath(path, paint);

imageView.setImageBitmap(croppedBitmap);

Answer 1:

我是非常接近的解决方案。 这里是:

compositeImageView = (ImageView) findViewById(R.id.imageView);

Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.batman_ad);
Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.logo);

Bitmap resultingImage=Bitmap.createBitmap(320, 480, bitmap1.getConfig());

Canvas canvas = new Canvas(resultingImage);

Paint paint = new Paint();
paint.setAntiAlias(true);
Path path=new Path();
path.lineTo(150, 0);
path.lineTo(230, 120);
path.lineTo(70, 120);
path.lineTo(150, 0);

canvas.drawPath(path, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap2, 0, 0, paint);

compositeImageView.setImageBitmap(resultingImage);


文章来源: Android - Crop an image from multipoints