Skip to content

Android - Smooth Bézier Spline Through Prescribed Points

Notifications You must be signed in to change notification settings

xujiaao/BezierSpline

Repository files navigation

BezierSpline

Download

Smooth Bézier Spline Through Prescribed Points for Android Platform.

Helps to simulate curves such as Sine Curve, Wave etc... Here is a Sample Application.

Screenshot - Wave Screenshot - Knots

Usage

Copy BezierSpline.java to your project.

private final Path mPath = new Path();
private final Paint mPaint = new Paint();
private final BezierSpline mBezierSpline = new BezierSpline(20);

@Override
protected void onDraw(Canvas canvas) {
    final float width = getWidth();
    final float density = getResources().getDisplayMetrics().density;
    for (int knot = 0, knots = mBezierSpline.knots(); knot < knots; knot++) {
        final float x = knot * (width / (knots - 1F));
        final float y = (float) (Math.toDegrees(Math.sin(Math.toRadians(x / density))) * density);
        mBezierSpline.set(knot, x, y);
    }

    mBezierSpline.applyToPath(mPath);
    canvas.drawPath(mPath, mPaint);
}