创建一个自定义的View,并覆盖onDraw方法。
在onDraw方法中,使用Path类创建一个曲线对象,并使用lineTo和quadTo方法将曲线绘制出来。
使用close方法将曲线封闭起来,形成一个封闭曲线。
示例代码:
public class MyView extends View {
private Paint mPaint;
private Path mPath;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5f);
mPath = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 创建封闭曲线路线
mPath.moveTo(100, 100);
mPath.lineTo(200, 300);
mPath.quadTo(300, 200, 400, 300);
mPath.lineTo(500, 100);
mPath.close();
// 绘制曲线
canvas.drawPath(mPath, mPaint);
}
}