在当前剪辑上绘制边框(Paint)(由不同的Region.Op创建)

时间:2011-06-17 14:04:41

标签: android graphics

我想将图像绘制为Path的形状,然后在Path上添加边框。我能够使用Path剪切图像,但无法找到在其上添加边框的方法。我虽然这很简单,因为API支持Canvas.draw *方法上的Paint对象。

我问了另一个问题:Draw bitmap on current clip in canvas with border (Paint)我接受了答案。然而,在那之后我发现我需要做一些更复杂的处理。因为我使用两个选项进行裁剪而不是一个。

下面是我用两个不同Region.Op参数剪辑和图像的代码

Bitmap srcImage = BitmapFactory.decodeStream(getAssets().open("panda.jpg"));
Bitmap bitmapResult = Bitmap.createBitmap(srcImage.getWidth(), srcImage.getHeight(), Bitmap.Config.ARGB_8888);
Path path = new Path();

// This is my border
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);

Canvas canvas = new Canvas(bitmapResult);

// Overlay two rectangles
path.addRect(10, 10, 70, 70, Path.Direction.CCW); 
path.addRect(40, 40, 120, 120, Path.Direction.CCW);
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.INTERSECT);

// Draw the circle 
path.reset();
path.addCircle(40, 80, 20, Path.Direction.CCW); 
canvas.drawPath(path , paint);
canvas.clipPath(path, Region.Op.DIFFERENCE);

// The image is drawn within the area of two rectangles and a circle
// Although I suppose that puting Paint object into drawBitmap() method will add a red border on result image but it doesn't work
canvas.drawBitmap(srcImage, 0, 0, paint);

((ImageView)this.findViewById(R.id.imageView1)).setImageBitmap(bitmapResult);

以下是我的代码的结果:http://i.stack.imgur.com/8j2Kg.png

这就是我的期望:http://i.stack.imgur.com/iKhIr.png

我是否想念任何事情才能让它发挥作用?

0 个答案:

没有答案