使用路径

时间:2016-04-12 17:36:26

标签: android view path android-canvas

我想画这样的东西:

enter image description here

所以在每张照片上,我想把一个带黑色区域的圆形扇形(切割弧形)。如何使用例如

实现这一目标
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);

1 个答案:

答案 0 :(得分:1)

你差不多了。您只需要在绘制之前剪切画布并使用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.drawArcdocumentation

我从您的问题中假设您需要为您的扇区指定修复高度,因此您需要根据startAnglesweepAngle方法计算drawArc 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); 方法的参数那个高度(假设是方形图像)。以下是示例代码(API级别15兼容):

Path

使用弧绘制扇区的另一种方法是创建Path.addArc对象,使用Canvas.drawPathdocumentation)添加弧,然后使用Sub MoveNames() Dim SrchRng As Range, cel As Range Set SrchRng = Range("D:D") For Each cel In SrchRng If InStr(1, cel.Value, "(2)") > 0 Then cel.Offset(0, 1).Range("A1:C1").Select Selection.Copy ActiveCell.Offset(-1, 40).Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False ActiveCell.Offset(0, -4) = "0" ActiveCell.Offset(0, -5) = "0" ActiveCell.Offset(0, -6) = "0" ActiveCell.Offset(0, -7) = "0" ActiveCell.Offset(0, -10) = "0" ActiveCell.Offset(0, -12) = "0" End If Next cel End Sub ({{3}绘制它。