【问题标题】:Draw circle on start and end point of an Arc在弧的起点和终点画圆
【发布时间】:2016-06-03 06:17:43
【问题描述】:

嗨,我在弧线的两端(开始和结束)上画点有困难

虽然我可以在画布上画弧线。这是我绘制圆弧的示例代码。

@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);  
}

唯一缺少的是将圆放在弧的起点和终点。我试过这个链接,但它不起作用Calculate Arc Center Point, Knowing It's Start and End Degrees。任何帮助都感激不尽。谢谢

【问题讨论】:

    标签: android view draw android-custom-view ondraw


    【解决方案1】:

    起点坐标为:

    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);
    

    【讨论】:

    • 如果我只能给你一百个赞,我愿意。要么谢谢,在这里花了几个小时。干杯@7heaven
    • @Sheychan 不确定“没有完美的圆”是什么意思,但是如果您需要找到起点和终点或曲线上的任何点(或使用 Path 类绘制的任何其他点) . PathMeasure 类是您所需要的。
    • @7heaven 我的意思是,这是否适用于矩形边界为矩形的椭圆(例如 (20,10,20,10))。
    • @7heaven 你好!我试过你的方法,你能提供任何关于如何为“线”做同样事情的意见吗?
    • @user2511882 您可以随时将 PathMeasure 用于由 Path 类绘制的任何形状
    【解决方案2】:
    1. 从 ARC 获取路径
    2. 使用 PathMeasure 类检索路径长度和路径 TAN,使用 ARC 的起始或结束 X 和 Y 坐标
    3. 使用此 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)
    

    【讨论】:

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