Android 在画布中的当前剪辑上绘制带边框的位图(绘制)

Android 在画布中的当前剪辑上绘制带边框的位图(绘制),android,graphics,Android,Graphics,我正在通过编写游戏来学习Android,但图形API有问题 我想把一个图像画成一条路径的形状,然后在路径上添加边框。我能够用路径剪辑图像,但找不到在其上添加边框的方法。我认为这很简单,因为API支持Canvas.draw*方法上的Paint对象 更新 在最初的问题中,我的路径包含两个矩形,@christopher souvey回答正确。但是,在处理添加另一个clipPath()方法时,我遇到了另一个问题 我更新了前面的代码,在路径中添加了一个圆圈。这是我的新代码: Bitmap srcImage

我正在通过编写游戏来学习Android,但图形API有问题

我想把一个图像画成一条路径的形状,然后在路径上添加边框。我能够用路径剪辑图像,但找不到在其上添加边框的方法。我认为这很简单,因为API支持Canvas.draw*方法上的Paint对象

更新

在最初的问题中,我的路径包含两个矩形,@christopher souvey回答正确。但是,在处理添加另一个clipPath()方法时,我遇到了另一个问题

我更新了前面的代码,在路径中添加了一个圆圈。这是我的新代码:

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

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);
以下是我的代码的结果:

这就是我所期望的:


我是否错过了使其工作的任何东西?

尝试在drawBitmap之后使用
canvas.drawPath(path,paint)

您可能需要在
画布中。在剪辑之前保存
,在画布之前还原
(我不确定笔划是否发生在路径线的内部或外部)。

drawBitmap中的paint元素不是边框颜色:,android.graphics.Matrix,android.graphics.paint)我们可以使用drawRect()和drawCircle()创建边框用油漆。为什么它不适用于drawBitmap(),仅供参考,笔划发生在路径线之外