我正在通过编写游戏来学习
Android,并且对图形API有疑问.
我想将图像绘制为路径的形状,然后在路径上添加边框.我能够使用Path剪切图像,但无法找到在其上添加边框的方法.我虽然很简单,因为API支持Canvas.draw *方法上的Paint对象.
更新
在原始问题中,我的路径包含两个矩形,@ christopher-souvey正确回答.但是,在处理添加一个clipPath()方法时,我遇到了另一个问题.
我通过在Path中添加一个圆来更新以前的代码.这是我的新代码:
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);
我能错过什么让它起作用吗?