|
| 1 | +# 三阶贝塞尔曲线 |
| 2 | + |
| 3 | +如果您对贝塞尔曲线还不是很了解的话,可以看一下[二阶贝塞尔曲线](https://github.com/linsir6/AndroidNote/blob/master/Android%E8%87%AA%E5%AE%9A%E4%B9%89View/%E4%BA%8C%E9%98%B6%E8%B4%9D%E5%A1%9E%E5%B0%94%E6%9B%B2%E7%BA%BF.md),下面我们看一下三阶曲线的效果图: |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +实现的思路和二阶的几乎一样,只是换了个函数,加了个控制点。 |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +代码: |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +```java |
| 22 | +public class Bezier3 extends View { |
| 23 | + |
| 24 | + |
| 25 | + private Paint mPaint; |
| 26 | + private int centerX, centerY; |
| 27 | + |
| 28 | + private PointF start, end, control1, control2; |
| 29 | + private boolean mode = true; |
| 30 | + |
| 31 | + public Bezier3(Context context) { |
| 32 | + this(context, null); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + public Bezier3(Context context, AttributeSet attrs) { |
| 37 | + super(context, attrs); |
| 38 | + |
| 39 | + mPaint = new Paint(); |
| 40 | + mPaint.setColor(Color.BLACK); |
| 41 | + mPaint.setStrokeWidth(8); |
| 42 | + mPaint.setStyle(Paint.Style.STROKE); |
| 43 | + mPaint.setTextSize(60); |
| 44 | + |
| 45 | + start = new PointF(0, 0); |
| 46 | + end = new PointF(0, 0); |
| 47 | + control1 = new PointF(0, 0); |
| 48 | + control2 = new PointF(0, 0); |
| 49 | + } |
| 50 | + |
| 51 | + public void setMode(boolean mode) { |
| 52 | + this.mode = mode; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 57 | + super.onSizeChanged(w, h, oldw, oldh); |
| 58 | + centerX = w / 2; |
| 59 | + centerY = h / 2; |
| 60 | + |
| 61 | + // 初始化数据点和控制点的位置 |
| 62 | + start.x = centerX - 200; |
| 63 | + start.y = centerY; |
| 64 | + end.x = centerX + 200; |
| 65 | + end.y = centerY; |
| 66 | + control1.x = centerX; |
| 67 | + control1.y = centerY - 100; |
| 68 | + control2.x = centerX; |
| 69 | + control2.y = centerY - 100; |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean onTouchEvent(MotionEvent event) { |
| 75 | + // 根据触摸位置更新控制点,并提示重绘 |
| 76 | + if (mode) { |
| 77 | + control1.x = event.getX(); |
| 78 | + control1.y = event.getY(); |
| 79 | + } else { |
| 80 | + control2.x = event.getX(); |
| 81 | + control2.y = event.getY(); |
| 82 | + } |
| 83 | + invalidate(); |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void onDraw(Canvas canvas) { |
| 89 | + super.onDraw(canvas); |
| 90 | + //drawCoordinateSystem(canvas); |
| 91 | + |
| 92 | + // 绘制数据点和控制点 |
| 93 | + mPaint.setColor(Color.GRAY); |
| 94 | + mPaint.setStrokeWidth(20); |
| 95 | + canvas.drawPoint(start.x, start.y, mPaint); |
| 96 | + canvas.drawPoint(end.x, end.y, mPaint); |
| 97 | + canvas.drawPoint(control1.x, control1.y, mPaint); |
| 98 | + canvas.drawPoint(control2.x, control2.y, mPaint); |
| 99 | + |
| 100 | + // 绘制辅助线 |
| 101 | + mPaint.setStrokeWidth(4); |
| 102 | + canvas.drawLine(start.x, start.y, control1.x, control1.y, mPaint); |
| 103 | + canvas.drawLine(control1.x, control1.y, control2.x, control2.y, mPaint); |
| 104 | + canvas.drawLine(control2.x, control2.y, end.x, end.y, mPaint); |
| 105 | + |
| 106 | + // 绘制贝塞尔曲线 |
| 107 | + mPaint.setColor(Color.RED); |
| 108 | + mPaint.setStrokeWidth(8); |
| 109 | + |
| 110 | + Path path = new Path(); |
| 111 | + |
| 112 | + path.moveTo(start.x, start.y); |
| 113 | + path.cubicTo(control1.x, control1.y, control2.x, control2.y, end.x, end.y); |
| 114 | + |
| 115 | + canvas.drawPath(path, mPaint); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | +[代码地址](https://github.com/linsir6/mCustomView/tree/master/Bezier3):https://github.com/linsir6/mCustomView/tree/master/Bezier3 |
| 125 | + |
| 126 | + |
0 commit comments