如何在画布上绘制贝塞尔曲线。如果我知道所有点及其枢轴的坐标。
class BezierPoint {
float pivot_one_x, pivot_one_y;
float pivot_two_x, pivot_two_y;
float point_x, point_y;
}
现在我有这个类的数组:
BezierPoint [] points = {...};
现在我想绘制整行:
从points[0]
到points[1]
,points[1]
到points[2]
,......
是的,android有Path.cubicTo().lineTo().quadTo()...
,但我不知道如何将bezier点转换为正确使用Path
?
答案 0 :(得分:1)
private Paint paintBezBase;
//control variables
int xControl1 = 0;
int yControl1 = 0;
int xControl2 = 0;
int yControl2 = 0;
//
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
Path pathProgress = new Path();
private void init(AttributeSet attrs, int defStyle) {
paintBezBase = new Paint() {
{
setStyle(Style.STROKE);
setStrokeCap(Cap.SQUARE);
setStrokeWidth(1);
setAntiAlias(true);
}
};
paintBezBase.setColor(Color.BLACK);
paintBezBase.setStrokeWidth(1);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
contentWidth = getWidth() - paddingLeft - paddingRight;
contentHeight = getHeight() - paddingTop - paddingBottom;
x1 = 0;
y1 = contentHeight / 2;
x2 = contentWidth;
y2 = contentHeight / 2;
yControl1 = 0 - 40;
yControl2 = contentHeight + 40;
pathProgress.moveTo(x1, y1);
pathProgress.cubicTo(xControl1, yControl1, xControl2, yControl2, x2, y2);
canvas.drawPath(pathProgress, paintBezBase);
}
使用yControl1&amp ;;调整曲线yControl2。