3

我需要裁剪一个位图,但不是有一个矩形裁剪图像(我成功地做到了),我需要它是由坐标定义的 任何形式。

我正在关注这个线程的答案:从Bitmap 中剪切一个多点多边形并将其放置在透明度上,并尝试实现它,但不幸的是它没有剪切图像。

我按照描述做了,但似乎某处有错误。图像以矩形方式绘制。我错过了什么吗?

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

1 回答 1

2

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

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);
于 2012-08-07T08:02:21.807 回答