使用 Path 绘制圆形扇区
Draw circular sector using Path
我想画这样的东西:
所以我想在每张照片上放一个带有黑色区域的圆形扇区(切弧)。我如何通过使用
来实现这一目标
canvas.draw(Path path, Paint paint);
我在下面尝试过,但没有如我所愿:
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.addCircle(getWidth() / 2, getHeight() / 2, getHeight() / 2, Path.Direction.CW);
path.addRect(0, getHeight() - 70, getWidth(), getHeight(), Path.Direction.CW);
你几乎成功了。您只需要在绘制之前剪裁 canvas 并使用 Path.FillType.INVERSE_EVEN_ODD
绘制您的扇区:
// Limit the drawable region of the canvas (saving the state before)
canvas.save();
canvas.clipRect(new Rect(0, canvas.getHeight() - 70, canvas.getWidth(), canvas.getHeight()));
Path path = new Path();
path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
path.addCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getHeight() / 2, Path.Direction.CW);
path.addRect(0, canvas.getHeight() - 70, canvas.getWidth(), canvas.getHeight(), Path.Direction.CW);
canvas.drawPath(path, paint);
// Restore the canvas to the saved state to remove clip
canvas.restore();
// Draw more things on the canvas...
或者,您可以使用 Canvas.drawArc
(documentation)
我假设你的问题是你需要为你的扇区分配一个固定高度,所以你需要计算 startAngle
和 sweepAngle
(drawArc
方法的参数)基于该高度(假设为方形图像)。这是示例代码(API 15 级兼容):
int sectorHeigh = 70; // The height of your sector in pixels
// Compute the start angle based on your desired sector height
float startAngle = (float) Math.toDegrees(Math.asin((canvas.getHeight() / 2f - sectorHeigh) / (canvas.getHeight() / 2f)));
// Add the arc (calculating the sweepAngle based on startAngle)
canvas.drawArc(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), startAngle, 2 * (90 - startAngle), false, paint);
另一种使用圆弧绘制扇区的方法是创建一个 Path
对象,使用 Path.addArc
(documentation) and then use Canvas.drawPath
(documentation) 添加一个圆弧来绘制它。
我想画这样的东西:
所以我想在每张照片上放一个带有黑色区域的圆形扇区(切弧)。我如何通过使用
来实现这一目标canvas.draw(Path path, Paint paint);
我在下面尝试过,但没有如我所愿:
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.addCircle(getWidth() / 2, getHeight() / 2, getHeight() / 2, Path.Direction.CW);
path.addRect(0, getHeight() - 70, getWidth(), getHeight(), Path.Direction.CW);
你几乎成功了。您只需要在绘制之前剪裁 canvas 并使用 Path.FillType.INVERSE_EVEN_ODD
绘制您的扇区:
// Limit the drawable region of the canvas (saving the state before)
canvas.save();
canvas.clipRect(new Rect(0, canvas.getHeight() - 70, canvas.getWidth(), canvas.getHeight()));
Path path = new Path();
path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
path.addCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getHeight() / 2, Path.Direction.CW);
path.addRect(0, canvas.getHeight() - 70, canvas.getWidth(), canvas.getHeight(), Path.Direction.CW);
canvas.drawPath(path, paint);
// Restore the canvas to the saved state to remove clip
canvas.restore();
// Draw more things on the canvas...
或者,您可以使用 Canvas.drawArc
(documentation)
我假设你的问题是你需要为你的扇区分配一个固定高度,所以你需要计算 startAngle
和 sweepAngle
(drawArc
方法的参数)基于该高度(假设为方形图像)。这是示例代码(API 15 级兼容):
int sectorHeigh = 70; // The height of your sector in pixels
// Compute the start angle based on your desired sector height
float startAngle = (float) Math.toDegrees(Math.asin((canvas.getHeight() / 2f - sectorHeigh) / (canvas.getHeight() / 2f)));
// Add the arc (calculating the sweepAngle based on startAngle)
canvas.drawArc(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), startAngle, 2 * (90 - startAngle), false, paint);
另一种使用圆弧绘制扇区的方法是创建一个 Path
对象,使用 Path.addArc
(documentation) and then use Canvas.drawPath
(documentation) 添加一个圆弧来绘制它。