4

我有一个自定义视图,可以在其中绘制弧线。现在我还需要画一条连接中心和圆周的线,并且应该正好在弧的中间。

现在我知道在 android canvas 中画线的代码相当简单

但它不涉及角度,弧总是使用角度来绘制自己。

所以我可以在与圆弧相同的方向上画线吗?

我在这里清楚还是需要解释更多?

请帮忙。

4

2 回答 2

11

计算起点和终点。

private class MView extends View {
    private Paint mPaint;
    private RectF mRect;
    private int mCenterX = 150;
    private int mCenterY = 150;

    public MView(Context context) {
        super(context);
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Style.STROKE);
        mRect = new RectF(0, 0, mCenterX * 2, mCenterY * 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float startAngle = 30;
        float sweepAngle = 60;

        canvas.drawArc(mRect, startAngle, sweepAngle, true, mPaint);
        float startX = mCenterX;
        float startY = mCenterY;
        float radius = mCenterX;
        float angle = (float) ((startAngle + sweepAngle / 2) * Math.PI / 180);
        float stopX = (float) (startX + radius * Math.cos(angle));
        float stopY = (float) (startY + radius * Math.sin(angle));

        canvas.drawLine(startX, startY, stopX, stopY, mPaint);
    }

}
于 2012-12-14T15:23:24.190 回答
0

这是我根据上面的答案得出的结论:

    for (int i = 0; i < numOfDividing; i++) {
        angle = i * (360 / numOfDividing);

        Point aPoint = new Point(
                (int) (x0 + (radius) * Math.cos(Math.toRadians(angle))),
                (int) (y0 + (radius) * Math.sin(Math.toRadians(angle)))
        );
        //Timber.d("Point %d : %d, %d", i, aPoint.x, aPoint.y);
        points.add(aPoint);
    }
于 2016-08-26T08:04:43.943 回答