如何在android中绘制曲线?

如何在android中绘制曲线?,第1张

概述我是 android新手,我正在开发绘图线上的示例项目.我想绘制连接两个点(x1,y1和x2,y2)的弯曲或高架线.我尝试了canvas.drawArc()方法,但drawArc方法中的RectF值只是圆的x,y中心点.它给了我两点之间的弧度.但我想要一条完全连接我的两点的曲线.有人能帮助我吗?提前致谢. 在onDraw方法中声明此方法: private void drawOvalAndArrow 我是 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,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中绘制曲线?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1123211.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存