如何在Android中绘制曲线?

苏伦达

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

z

在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上绘画的有用参考:

如何在Android中使用画布绘制Arcs?

风景基本绘画

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Android中绘制功能曲线?

来自分类Dev

如何在Android中绘制贝塞尔曲线

来自分类Dev

如何在SVG中绘制S曲线?

来自分类Dev

如何在加工中绘制高斯曲线

来自分类Dev

如何在XAML中绘制曲线?

来自分类Dev

如何在A帧中绘制曲线?

来自分类Dev

如何在Android中的Canvas上绘制尖锐的曲线和斜线

来自分类Dev

如何在python中绘制没有曲线的单个点?

来自分类Dev

如何在cocos2dx中绘制任意曲线

来自分类Dev

如何在R中绘制拷贝数变化曲线?

来自分类Dev

如何在R中绘制泊松密度曲线?

来自分类Dev

如何在MATLAB中仅绘制拟合曲线?

来自分类Dev

如何在R中绘制曲线的渐近线?

来自分类Dev

如何在UIView中绘制贝塞尔曲线

来自分类Dev

如何在Swift中绘制余弦或正弦曲线?

来自分类Dev

如何在R的能量图中绘制曲线?

来自分类Dev

如何使用Android图形绘制无缝曲线?

来自分类Dev

如何在pgfplots中绘制平滑曲线?以及如何删除y轴刻度线?

来自分类Dev

如何在Scikit-Learn中绘制PR曲线超过10倍的交叉验证

来自分类Dev

如何在css中的矩形顶部绘制曲线?仅在顶部边缘

来自分类Dev

如何在pROC中以置信区间绘制多个roc曲线?

来自分类Dev

如何在一张图像中绘制曲线和直线?

来自分类Dev

如何在R中突破y数据范围绘制“多”曲线?

来自分类Dev

如何在 WPF 中仅绘制 Bezier 曲线的一部分?

来自分类Dev

如何在Android中绘制圆的直径

来自分类Dev

如何在Android中绘制饼图

来自分类Dev

如何在Android中绘制圆线

来自分类Dev

在文本中绘制曲线

来自分类Dev

如何在直方图上绘制匹配的贝尔曲线?