【问题标题】:Draw bitmap on current clip in canvas with border (Paint)在带有边框的画布中的当前剪辑上绘制位图(油漆)
【发布时间】:2011-06-12 15:07:34
【问题描述】:

我正在通过编写游戏来学习 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);

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

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

我是否错过了什么让它发挥作用?

【问题讨论】:

  • drawBitmap 中的绘制元素不是边框颜色:developer.android.com/reference/android/graphics/…, android.graphics.Matrix, android.graphics.Paint)
  • 我们可以使用drawRect()创建边框,使用Paint创建drawCircle()。为什么drawBitmap()不起作用

标签: android graphics


【解决方案1】:

尝试在drawBitmap之后使用canvas.drawPath(path, paint) 您可能需要在剪辑之前输入canvas.save,在drawPath 之前输入canvas.restore(我不确定中风是发生在路径线内部还是外部)。

【讨论】:

  • 仅供参考,笔画出现在路径线之外
猜你喜欢
  • 2013-04-04
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
相关资源
最近更新 更多