三次贝塞尔曲线学习

今天看三次贝塞尔曲线练习之弹性的圆后,决定自己也画着试试,才有这这篇、

也上图吧,看看仿得如何

P:真心不像,华为手机录视频的是不是有点烂,我手机上不会如此抖动,截帧数据太稀了??没办法就这样吧,其实效果不错~

首先看到这种图,第一直觉就是怎么画的?看了之后才知道是用贝塞尔曲线,且讲得容易看懂 - 其实我也只看了大概,然后自己来画了,说实话看别人纯算法代码,思路没理上真不好根~你懂的,这不我来分享自己画的图了~

简易学塞贝尔曲线3步曲:

  1. 看图

  2. 塞贝尔相关文章和玩一下bezier

  3. 看图

是不是有点豁然开朗的感觉,下面是我自己画的样例的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
* Created by xuie on 17-3-2.
*/

public class DropWater extends View {
private static final String TAG = "DropWater";
private Context mContext;
private Path mPath;
private Paint mPaint;
private VPoint a = new VPoint();
private HPoint b = new HPoint();
private VPoint c = new VPoint();
private HPoint d = new HPoint();
private int width;
private int height;
private float mInterpolatedTime;
private int centerX;
private int centerY;
private static final float blackMagic = 0.552284749831f;
private float magic;
private float radius;


public DropWater(Context context) {
super(context);
mContext = context;
init();
}

public DropWater(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}

public DropWater(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(ContextCompat.getColor(mContext, android.R.color.holo_red_light));
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(1);
mPaint.setAntiAlias(true);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPath.reset();
canvas.translate((width - radius * 2) * (mInterpolatedTime - 0.5f), 0);

if (mInterpolatedTime >= 0 && mInterpolatedTime < 0.2) {
a.x = centerX - radius;
a.y = centerY;
a.top.x = a.x;
a.top.y = a.y - magic;
a.bottom.x = a.x;
a.bottom.y = a.y + magic;

b.x = centerX;
b.y = centerY - radius * (1 - getOffset(mInterpolatedTime, 0.2f));
b.left.x = b.x - magic;
b.left.y = b.y;
b.right.x = b.x + magic;
b.right.y = b.y;

c.x = centerX + radius + (mInterpolatedTime) / 0.2f * radius;
c.y = centerY;
c.top.x = c.x;
c.top.y = c.y - magic;
c.bottom.x = c.x;
c.bottom.y = c.y + magic;

d.x = centerX;
d.y = centerY + radius * (1 - getOffset(mInterpolatedTime, 0.2f));
d.left.x = d.x - magic;
d.left.y = d.y;
d.right.x = d.x + magic;
d.right.y = d.y;
} else if (mInterpolatedTime >= 0.2 && mInterpolatedTime < 0.5) {
a.x = centerX - radius - (mInterpolatedTime - 0.2f) / 0.3f * radius * 0.5f;
a.y = centerY;
a.top.x = a.x;
a.top.y = a.y - magic;
a.bottom.x = a.x;
a.bottom.y = a.y + magic;

b.x = centerX;
b.y = centerY - radius * (1 - getOffset(1, 1));
b.left.x = b.x - magic;
b.left.y = b.y;
b.right.x = b.x + magic;
b.right.y = b.y;

c.x = centerX + radius * 2;
c.y = centerY;
c.top.x = c.x;
c.top.y = c.y - magic;
c.bottom.x = c.x;
c.bottom.y = c.y + magic;

d.x = centerX;
d.y = centerY + radius * (1 - getOffset(1, 1));
d.left.x = d.x - magic;
d.left.y = d.y;
d.right.x = d.x + magic;
d.right.y = d.y;
} else if (mInterpolatedTime >= 0.5 && mInterpolatedTime < 0.8) {
a.x = centerX - radius * 1.5f;
a.y = centerY;
a.top.x = a.x;
a.top.y = a.y - magic;
a.bottom.x = a.x;
a.bottom.y = a.y + magic;

b.x = centerX;
b.y = centerY - radius * (1 - getOffset(1, 1));
b.left.x = b.x - magic;
b.left.y = b.y;
b.right.x = b.x + magic;
b.right.y = b.y;

c.x = centerX + radius * 2 - (mInterpolatedTime - 0.5f) / 0.3f * radius;
c.y = centerY;
c.top.x = c.x;
c.top.y = c.y - magic;
c.bottom.x = c.x;
c.bottom.y = c.y + magic;

d.x = centerX;
d.y = centerY + radius * (1 - getOffset(1, 1));
d.left.x = d.x - magic;
d.left.y = d.y;
d.right.x = d.x + magic;
d.right.y = d.y;
} else if (mInterpolatedTime >= 0.8 && mInterpolatedTime < 0.9) {
a.x = centerX - radius * 1.5f + (mInterpolatedTime - 0.8f) / 0.1f * radius * 0.5f;
a.y = centerY;
a.top.x = a.x;
a.top.y = a.y - magic;
a.bottom.x = a.x;
a.bottom.y = a.y + magic;

b.x = centerX;
b.y = centerY - radius * (1 - getOffset(0.1f - (mInterpolatedTime - 0.8f), 0.1f));
b.left.x = b.x - magic;
b.left.y = b.y;
b.right.x = b.x + magic;
b.right.y = b.y;

c.x = centerX + radius;
c.y = centerY;
c.top.x = c.x;
c.top.y = c.y - magic;
c.bottom.x = c.x;
c.bottom.y = c.y + magic;

d.x = centerX;
d.y = centerY + radius * (1 - getOffset(0.1f - (mInterpolatedTime - 0.8f), 0.1f));
d.left.x = d.x - magic;
d.left.y = d.y;
d.right.x = d.x + magic;
d.right.y = d.y;
} else if (mInterpolatedTime >= 0.9) {
a.x = (float) (centerX - radius + radius * 0.2f * Math.sin(
Math.toRadians(
((mInterpolatedTime - 0.9f) / 0.1f) * 180
)
)
);
a.y = centerY;
a.top.x = a.x;
a.top.y = a.y - magic;
a.bottom.x = a.x;
a.bottom.y = a.y + magic;

b.x = centerX;
b.y = centerY - radius;
b.left.x = b.x - magic;
b.left.y = b.y;
b.right.x = b.x + magic;
b.right.y = b.y;

c.x = centerX + radius;
c.y = centerY;
c.top.x = c.x;
c.top.y = c.y - magic;
c.bottom.x = c.x;
c.bottom.y = c.y + magic;

d.x = centerX;
d.y = centerY + radius;
d.left.x = d.x - magic;
d.left.y = d.y;
d.right.x = d.x + magic;
d.right.y = d.y;
}

mPath.moveTo(a.x, a.y);
mPath.cubicTo(a.top.x, a.top.y, b.left.x, b.left.y, b.x, b.y);
mPath.cubicTo(b.right.x, b.right.y, c.top.x, c.top.y, c.x, c.y);
mPath.cubicTo(c.bottom.x, c.bottom.y, d.right.x, d.right.y, d.x, d.y);
mPath.cubicTo(d.left.x, d.left.y, a.bottom.x, a.bottom.y, a.x, a.y);
canvas.drawPath(mPath, mPaint);
}

private float getOffset(float present, float base) {
return present / base * 0.11f;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
centerX = width / 2;
centerY = height / 2;

radius = 50;
magic = blackMagic * radius;
}

private class MoveAnimation extends Animation {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
mInterpolatedTime = interpolatedTime;
invalidate();
}
}

private class VPoint {
float x;
float y;
PointF top = new PointF();
PointF bottom = new PointF();
}

private class HPoint {
float x;
float y;
PointF left = new PointF();
PointF right = new PointF();
}

public void startAnimation() {
mPath.reset();
mInterpolatedTime = 0;
MoveAnimation move = new MoveAnimation();
move.setDuration(2000);
move.setInterpolator(new AccelerateDecelerateInterpolator());
// move.setInterpolator(new AnticipateOvershootInterpolator());
//move.setRepeatCount(Animation.INFINITE);
//move.setRepeatMode(Animation.REVERSE);
startAnimation(move);
}
}

参考:

http://bezier.method.ac/

http://www.jianshu.com/p/791d3a791ec2

文章作者: 二十I邊界
文章链接: https://xuie0000.com/post/2017-03-03-2019/三次贝塞尔曲线学习.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 二十I邊界