Java How to draw Bezier Curve in Android

https://‮ttual.www‬uri.com
Java How to draw Bezier Curve in Android

To draw a Bezier curve in Android, you can use the Path class and its cubicTo() method. The cubicTo() method adds a cubic Bezier curve to the path, given the starting and ending points and the two control points.

Here's an example of how to draw a Bezier curve in Android using the Path class:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

public class BezierCurveView extends View {

  private Path mPath;
  private Paint mPaint;

  public BezierCurveView(Context context) {
    super(context);
    init();
  }

  private void init() {
    mPath = new Path();
    mPaint = new Paint();
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(5);
  }

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

    // Define the starting point of the curve
    mPath.moveTo(100, 100);

    // Add a cubic Bezier curve to the path
    mPath.cubicTo(200, 0, 300, 300, 400, 100);

    // Draw the curve on the canvas
    canvas.drawPath(mPath, mPaint);
  }
}

In the above example, the BezierCurveView class extends the View class and overrides the onDraw() method to draw a Bezier curve on the canvas. The init() method initializes the Path and Paint objects that are used to draw the curve.

Created Time:2017-11-03 22:21:03  Author:lautturi