我正在尝试将画布绘制操作裁剪到一个弧形的楔子上。然而,在设置画布的裁剪路径后,我没有得到预期的结果。
为了说明,我是这样做的:
path.reset();
//Move to point #1
path.moveTo(rect.centerX(), rect.centerY());
//Per the documentation, this will draw a connecting line from the current
//position to the starting position of the arc (at 0 degrees), add the arc
//and my current position now lies at #2.
path.arcTo(rect, 0, -30);
//This should then close the path, finishing back at the center point (#3)
path.close();
这是可行的,当我简单地绘制此路径(canvas.drawPath(path, paint)
)时,它将绘制如上所示的楔形。但是,当我将此路径设置为画布的剪切路径并绘制到其中时:
//I've tried it with and without the Region.Op parameter
canvas.clipPath(path, Region.Op.REPLACE);
canvas.drawColor(Color.BLUE);
我得到了以下结果(按钮留下来只是为了显示引用):
因此,它似乎裁剪到Path
的边界矩形,而不是Path
本身。你知道这里发生了什么吗?
编辑就像更新一样,我发现了一种更有效的方法来实现这一点,它允许硬件加速。首先,绘制整个图像(您将裁剪)到一个屏幕外的位图。使用此Bitmap
创建BitmapShader
,将该着色器设置为Paint
,然后使用该Paint
对象绘制楔形路径:
drawMyBitmap(bitmap);
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setShader(shader);
@Override
public void onDraw(Canvas canvas) {
canvas.drawArc(rect, //The rectangle bounding the circle
startAngle, //The angle (CW from 3 o'clock) to start
sweepAngle, //The angle (CW from 3 o'clock) of the arc
true, //Boolean of whether to draw a filled arc (wedge)
paint //The paint with the shader attached
);
}
发布于 2012-12-03 03:42:29
您使用的是HC或更高版本,还是使用硬件加速?
如果是这样的话,clipPath是不受支持且有问题的。
developer.android.com/guide/topics/graphics/hardware-accel.html。
发布于 2013-09-05 23:56:26
OP的问题是关于使用裁剪区域的,已经由@Simon回答了。但是,请记住,有一种更直接的方法来绘制一条完整的弧线:
创建Paint
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.FILL);
mPaint.setAntiAlias(true);
绘制时,只需绘制路径:
canvas.drawPath(path, mPaint);
https://stackoverflow.com/questions/13672802
复制相似问题