在圆弧的起点和终点画圆
Draw circle on start and end point of an Arc
嗨,我在圆弧的两端(起点和终点)画点有困难
虽然我可以在canvas上画弧线。这是我绘制圆弧的示例代码。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = (float) getWidth();
float height = (float) getHeight();
float radius;
if (width > height) {
radius = height / 4;
} else {
radius = width / 4;
}
float center_x, center_y;
final RectF oval = new RectF();
center_x = width / 2;
center_y = height / 2;
oval.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
float percent = 25;
float arcRadius = 360;
float angle = arcRadius * (percent/100);
canvas.drawArc(oval, 270, 360, false, trackpaint);
canvas.drawArc(oval, 270, angle, false, arcPaint);
}
唯一缺少的是在圆弧的起点和终点放置圆圈。我试过这个 link 但确实有效 Calculate Arc Center Point, Knowing It's Start and End Degrees。任何帮助都感激不尽。谢谢
起点坐标为:
double startX = Math.cos(Math.toRadians(270)) * radius + center_x;
double startY = Math.sin(Math.toRadians(270)) * radius + center_y;
终点坐标为:
double endX = Math.cos(Math.toRadians(270 + angle)) * radius + center_x;
double endY = Math.sin(Math.toRadians(270 + angle)) * radius + center_y;
然后可以用起点和终点画圆:
canvas.drawCircle(startX, startY, 10, paint);
canvas.drawCircle(endX, endY, 10, paint);
- 从 ARC 获取路径
- 使用 PathMeasure class 检索路径长度和路径 TAN,使用 ARC 的起始或结束 X 和 Y 坐标
- 使用此 X 和 Y 坐标绘制圆。
ARC 开头的圆示例:
final Path mPath = new Path();
mPath.addArc(oval, startAngle, sweepAngle);
PathMeasure pm = new PathMeasure(mPath, false);
float[] xyCoordinate = { arcStarting.x , arcStarting.y };
float pathLength = pm.getLength();
pm.getPosTan(0, xyCoordinate, null);//"0 for starting point"
PointF point = new PointF(xyCoordinate[0], xyCoordinate[1]);
canvas.drawCircle(point.x, point.y, 10, YourPaintHere)
嗨,我在圆弧的两端(起点和终点)画点有困难
虽然我可以在canvas上画弧线。这是我绘制圆弧的示例代码。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = (float) getWidth();
float height = (float) getHeight();
float radius;
if (width > height) {
radius = height / 4;
} else {
radius = width / 4;
}
float center_x, center_y;
final RectF oval = new RectF();
center_x = width / 2;
center_y = height / 2;
oval.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
float percent = 25;
float arcRadius = 360;
float angle = arcRadius * (percent/100);
canvas.drawArc(oval, 270, 360, false, trackpaint);
canvas.drawArc(oval, 270, angle, false, arcPaint);
}
唯一缺少的是在圆弧的起点和终点放置圆圈。我试过这个 link 但确实有效 Calculate Arc Center Point, Knowing It's Start and End Degrees。任何帮助都感激不尽。谢谢
起点坐标为:
double startX = Math.cos(Math.toRadians(270)) * radius + center_x;
double startY = Math.sin(Math.toRadians(270)) * radius + center_y;
终点坐标为:
double endX = Math.cos(Math.toRadians(270 + angle)) * radius + center_x;
double endY = Math.sin(Math.toRadians(270 + angle)) * radius + center_y;
然后可以用起点和终点画圆:
canvas.drawCircle(startX, startY, 10, paint);
canvas.drawCircle(endX, endY, 10, paint);
- 从 ARC 获取路径
- 使用 PathMeasure class 检索路径长度和路径 TAN,使用 ARC 的起始或结束 X 和 Y 坐标
- 使用此 X 和 Y 坐标绘制圆。
ARC 开头的圆示例:
final Path mPath = new Path();
mPath.addArc(oval, startAngle, sweepAngle);
PathMeasure pm = new PathMeasure(mPath, false);
float[] xyCoordinate = { arcStarting.x , arcStarting.y };
float pathLength = pm.getLength();
pm.getPosTan(0, xyCoordinate, null);//"0 for starting point"
PointF point = new PointF(xyCoordinate[0], xyCoordinate[1]);
canvas.drawCircle(point.x, point.y, 10, YourPaintHere)