画布中Android Path addArc在两点之间 [英] Android Path addArc in canvas between two points

查看:696
本文介绍了画布中Android Path addArc在两点之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在android中绘制圆弧. 在IOS中,使用这种方法真的很容易

I'm trying to draw an arc in android. In IOS, it's really easy to do it with this method

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

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

In android, I have 3 points (the center of my circle, and the two points I want to draw an arc between) :

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之间绘制圆弧? 我已经尝试过在(如何在画布上有两点?):

But how can I use the Path.addArc method to draw an arc between p1 and p2 ? I have tried as said in (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应该是什么?

But actually it doesn't draw the expected arc. I don't understand what is the first parameter of the addArc method ! What the RectF is supposed to be ?

谢谢

推荐答案

path.addArc将圆弧添加到路径的末尾. Android顺时针绘制圆弧,因此当看一个圆 O 时,如果我的圆弧需要作为四分之一圆,则说如果它是从12点到3点的时钟,则需要RectF代表其中的正方形我可以画出弧线另外两个参数是startAngle和sweepAngle. 在我的示例中,startAngle是270(12点是270、3点是0、6点是90、9点是180). slipAngle是弧的长度,在我的示例中为90.如果我想绘制一个半圆,则为180.

path.addArc adds an arc to the end of your path. Android draws the arc clockwise, so when looking at a circle O if my arc needs to be as a quarter circle, say if it was a clock from 12 o'clock to 3 o'clock I need RectF to represent the square that inside it I can draw my arc. Two additional parameters are startAngle and sweepAngle. startAngle in my example is 270 (12 o'clock is 270, 3 o'clock is 0, 6 o'clock is 90 and 9 o'clock is 180). sweepAngle is how long is your arc, in my example it's 90. if I want to draw a half circle then it's 180.

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

If you wish to draw counter clockwise you need to set sweepAngle to minus degrees. For example an arc from 3 o'clock to 12 o'clock of 90 degrees, I'll need the startAngle to be 90 and the sweepAngle to be -90 .

在此链接中,有一篇很好的文章教会了我所有这一切: https://www. cumulations.com/blogs/5/UnderstandingSweepangleindrawArcmethodofandroid

A great article that taught me all this is in this link: https://www.cumulations.com/blogs/5/UnderstandingSweepangleindrawArcmethodofandroid

这篇关于画布中Android Path addArc在两点之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆