android - 如何在android中绘制曲线?

标签 android canvas line

我是 Android 的新手,我正在开发一个关于绘图线的示例项目。我想绘制一条连接两点 (x1,y1 和 x2,y2) 的曲线或高架线。我尝试了 canvas.drawArc() 方法,但是 drawArc 方法中的 RectF 值只是圆的 x,y 中心点。它给了我两点之间的弧线。但我想要一条曲线正好连接我的两点。有人可以帮我吗?提前致谢。

最佳答案

在 onDraw 方法中声明此方法:

private void drawOvalAndArrow(Canvas canvas){


    Paint circlePaint = new Paint();
    circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    circlePaint.setAntiAlias(true);
    circlePaint.setStrokeWidth(2);
    circlePaint.setColor(Color.CYAN);

    float centerWidth = canvas.getWidth()/2; //get center x of display
    float centerHeight = canvas.getHeight()/2; //get center y of display
    float circleRadius = 20; //set radius 
    float circleDistance = 200; //set distance between both circles

    //draw circles
    canvas.drawCircle(centerWidth, centerHeight, circleRadius, circlePaint);
    canvas.drawCircle(centerWidth+circleDistance, centerHeight, circleRadius, circlePaint);


    //to draw an arrow, just lines needed, so style is only STROKE
    circlePaint.setStyle(Paint.Style.STROKE);       
    circlePaint.setColor(Color.RED);

    //create a path to draw on
    Path arrowPath = new Path();

    //create an invisible oval. the oval is for "behind the scenes" ,to set the path´
    //area. Imagine this is an egg behind your circles. the circles are in the middle of this egg
    final RectF arrowOval = new RectF();
    arrowOval.set(centerWidth, 
            centerHeight-80, 
            centerWidth + circleDistance, 
            centerHeight+80);

    //add the oval to path
    arrowPath.addArc(arrowOval,-180,180);

    //draw path on canvas
    canvas.drawPath(arrowPath, circlePaint);


    //draw arrowhead on path start
     arrowPath.moveTo(centerWidth,centerHeight ); //move to the center of first circle
     arrowPath.lineTo(centerWidth-circleRadius, centerHeight-circleRadius);//draw the first arrowhead line to the left
     arrowPath.moveTo(centerWidth,centerHeight );//move back to the center
     arrowPath.lineTo(centerWidth+circleRadius, centerHeight-circleRadius);//draw the next arrowhead line to the right

     //same as above on path end
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)-circleRadius, centerHeight-circleRadius);
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)+circleRadius, centerHeight-circleRadius);

     //draw the path
     canvas.drawPath(arrowPath,circlePaint);

}

此外,这会找到屏幕的两侧(横向模式),并在屏幕上绘制一条完美的曲线

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    PointF mPoint1 = new PointF(w/1.2F, h/1.2F);
    PointF mPoint2 = new PointF(w/24, h/1.2F);
    Path myPath1 = new Path();
    Paint paint  = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.WHITE);

    myPath1 = drawCurve(canvas, paint, mPoint1, mPoint2);
    canvas.drawPath(myPath1, paint);

}

private Path drawCurve(Canvas canvas, Paint paint, PointF mPointa, PointF mPointb) {

    Path myPath = new Path();
    myPath.moveTo(63*w/64, h/10);
    myPath.quadTo(mPointa.x, mPointa.y, mPointb.x, mPointb.y);
    return myPath;  
}

在android中绘画的有用引用:

How to draw Arcs in Android using canvas?

Basic Painting with Views

关于android - 如何在android中绘制曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27704200/

相关文章:

java - Android OpenGLES 骨骼动画问题

javascript - JQuery Canvas drawImage() 不工作

java - 长时间挂起执行外部方法的初始化线程

android - 无法访问录音机

javascript - canvas drawImage 第一次不绘制图像

javascript - 无法删除事件监听器

line - 检查点是否在 line2D 内

html - SVG 线宽问题

c# - 在 Canvas 上画线 "slowly"

Android:是否可以使用 AlarmManager 完成所有更新任务