Android - 从多点裁剪图像

时间:2012-07-20 12:43:52

标签: android android-layout android-intent android-emulator android-widget

我需要裁剪一个Bitmap,但我没有使用矩形裁剪图像(我成功管理),而是需要以任何形式定义坐标。

我正在关注这个帖子的答案:Cutting a multipoint ploygon out of Bitmap and placing it on transparency,并尝试实现它,但不幸的是它没有剪辑图像。

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

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

1 个答案:

答案 0 :(得分: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);