How to draw a Bezier curve in Android

I have a requirement to create a Bezier curve in my project. To do this, I draw a representation with paint, but the problem is that I do not get the exact form for my needs, as indicated in the figure below. So please kindly help me with your decisions and changes or changes in my code. Thanks in advance.

The code I use to create a Bezier curve:

public class DrawView extends View { public DrawView (Context context) { super (context); } protected void onDraw (Canvas canvas) { super.onDraw (canvas); Paint pLine = new Paint () {{ setStyle (Paint.Style.STROKE); setAntiAlias (true); setStrokeWidth (1.5f); setColor (Color.RED); // Line color }}; Paint pLineBorder = new Paint () {{ setStyle (Paint.Style.STROKE); setAntiAlias (true); setStrokeWidth (3.0f); setStrokeCap (Cap.ROUND); setColor (Color.RED); // Darker version of the color }}; Path p = new Path (); Point mid = new Point (); // ... Point start =new Point (30,90); Point end =new Point (canvas.getWidth ()-30,140); mid.set ((start.x + end.x) / 2, (start.y + end.y) / 2); // Draw line connecting the two points: p.reset (); p.moveTo (start.x, start.y); p.quadTo ((start.x + mid.x) / 2, start.y, mid.x, mid.y); p.quadTo ((mid.x + end.x) / 2, end.y, end.x, end.y); canvas.drawPath (p, pLineBorder); canvas.drawPath (p, pLine); } } 

Mainactivity

 public class MainActivity extends Activity { private DrawView drawView; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); drawView = new DrawView (this); setContentView (drawView); } } 

My actual need:

enter image description here

The result that I get:

enter image description here

+8
source share
2 answers

After a long struggle, I found the root cause of my problem right from scratch. Thanks for the tool that helped me generate the coordinates, as well as the blog , which showed me the exact sample for my need. Finally, my code is as follows:

 public class DrawView extends View { Paint paint; Path path; public DrawView(Context context) { super(context); init(); } public DrawView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public DrawView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init(){ paint = new Paint(); paint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); path = new Path(); paint.setColor(Color.RED); paint.setStrokeWidth(3); path.moveTo(34, 259); path.cubicTo(68, 151, 286, 350, 336, 252); canvas.drawPath(path, paint); } 
+11
source

You did not close your path and did not indicate the color that you drew.

0
source

Source: https://habr.com/ru/post/986643/


All Articles