【问题标题】:Android Path addArc in canvas between two points两点之间的画布中的Android路径addArc
【发布时间】:2016-05-26 14:54:52
【问题描述】:

我正在尝试在 android 中绘制弧线。 在IOS中,用这种方法真的很容易做到

[path addArcWithCenter: radius: startAngle: endAngle: clockwise:]

在 android 中,我有 3 个点(我的圆心,以及我想在两个点之间画弧线的点):

Point center = new Point(..., ...);
Point p1 = new Point(..., ...);
Point p2 = new Point(..., ...);

int radius = (int) Math.sqrt(Math.pow(p1.x - center.x, 2) + Math.pow(p1.y - center.y, 2));

但是如何使用 Path.addArc 方法在 p1 和 p2 之间绘制弧线? 我已经尝试过 (How to draw Arc between two points on the Canvas?) 中所说的:

RectF oval = new RectF();
oval.set(p2.x - radius, p2.y - radius, p2.x + radius, p2.y + radius);
path.addArc(oval, startAngle, endAngle - startAngle);
// startAngle : angle between horizontal axis and p1 point
// endAngle : angle between horizontal axis and p2 point

但实际上它并没有画出预期的弧线。我不明白 addArc 方法的第一个参数是什么! RectF 应该是什么?

谢谢,

【问题讨论】:

    标签: android path android-canvas ondraw


    【解决方案1】:

    path.addArc 将弧线添加到路径的末尾。 Android 顺时针绘制圆弧,所以当看圆时 O 如果我的圆弧需要为四分之一圆,假设它是从 12 点到 3 点的时钟,我需要 RectF来表示在它里面我可以画出我的弧线的正方形。两个附加参数是 startAngle 和 sweepAngle。 我的示例中的 startAngle 是 270(12 点钟是 270,3 点钟是 0,6 点钟是 90,9 点钟是 180)。 sweepAngle 是你的圆弧有多长,在我的例子中是 90。如果我想画一个半圆,那么它是 180。

    如果您想逆时针绘制,您需要将 sweepAngle 设置为负度数。例如从 3 点到 12 点的 90 度圆弧,我需要 startAngle 为 90,sweepAngle 为 -90。

    一个伟大的文章教会了我这一切在这个链接中:https://www.cumulations.com/blogs/5/UnderstandingSweepangleindrawArcmethodofandroid

    【讨论】:

      【解决方案2】:

      第一个参数是定义圆弧形状和大小的椭圆的边界 - 您的代码在这里绝对正确。 问题可能是在 iOS 中您必须使用弧度,但在 Android 中您必须使用度数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多