动画练手

最近看了VectorDrawable的动画,正巧看到微博的这个动画,正常也能用上,所以起手自己来画一下,是不是不像~ - -!! 66666…

废话不多说了,上两张图片对比照

原图:

我画的图,录屏手机 - 华为P9

感觉有几分像了,233,就不再雕工了,上代码吧,算是这几天的成果、

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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

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

public class ShortCandle extends View {
private static final String TAG = "ShortCandle";
private Paint mLinePaint;
private Paint mCirclePaint;
private Paint mFillPaint;
private Paint mFillBgPaint;
private Paint mAngryEyePaint;
private Paint mFirePaint;
private Paint mLightPaint;
private Path mPath;
private int mWidth;
private int mHeight;
private RectF rectF;
private float floatTime;
private int angryColor = Color.parseColor("#c57fbb");
private int normalColor = Color.parseColor("#ffffff");
private boolean firstStage;
private boolean secondStage;
private boolean thirdStage;
private boolean fourStage;

private PathMeasure mPathMeasure;
private float[] position = new float[2];

public ShortCandle(Context context) {
super(context);
init();
}

public ShortCandle(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

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

private void init() {
mLinePaint = new Paint();
mLinePaint.setStrokeWidth(8);
mLinePaint.setColor(Color.parseColor("#643a60"));
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeCap(Paint.Cap.ROUND);
mLinePaint.setAntiAlias(true);
mCirclePaint = new Paint();
mCirclePaint.setStrokeWidth(8);
mCirclePaint.setColor(Color.parseColor("#643a60"));
mCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
mCirclePaint.setStrokeCap(Paint.Cap.ROUND);
mCirclePaint.setAntiAlias(true);
mFillPaint = new Paint();
mFillPaint.setStrokeWidth(8);
mFillPaint.setColor(normalColor);
mFillPaint.setStyle(Paint.Style.FILL);
mFillPaint.setStrokeCap(Paint.Cap.ROUND);
mFillPaint.setAntiAlias(true);
mFillBgPaint = new Paint();
mFillBgPaint.setStrokeWidth(8);
mFillBgPaint.setColor(Color.parseColor("#a5b0d9"));
mFillBgPaint.setStyle(Paint.Style.FILL);
mFillBgPaint.setStrokeCap(Paint.Cap.ROUND);
mFillBgPaint.setAntiAlias(true);
mAngryEyePaint = new Paint();
mAngryEyePaint.setStrokeWidth(5);
mAngryEyePaint.setColor(Color.RED);
mAngryEyePaint.setStyle(Paint.Style.STROKE);
mAngryEyePaint.setStrokeCap(Paint.Cap.ROUND);
mAngryEyePaint.setAntiAlias(true);
mFirePaint = new Paint();
mFirePaint.setStrokeWidth(5);
mFirePaint.setColor(Color.RED);
mFirePaint.setStyle(Paint.Style.FILL);
mFirePaint.setAntiAlias(true);
mLightPaint = new Paint();
mLightPaint.setStrokeWidth(12);
mLightPaint.setColor(Color.parseColor("#bbffffff"));
mLightPaint.setStyle(Paint.Style.STROKE);
mLightPaint.setAntiAlias(true);

mPath = new Path();
mPathMeasure = new PathMeasure(mPath, true);

rectF = new RectF();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBackground(canvas);
drawLandLine(canvas);
drawGirlCandle(canvas);
drawBoyCandle(canvas);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mWidth = getWidth();
mHeight = getHeight();
}


private void drawBackground(Canvas canvas) {
canvas.drawColor(Color.parseColor("#f7edab"));
}

private void drawLandLine(Canvas canvas) {
float px = mWidth / 4;
float py = mHeight / 3;
canvas.drawLine(px, py * 2, px * 3, py * 2, mLinePaint);
}

private void drawGirlCandle(Canvas canvas) {
float widthOffset = (float) (4 * Math.sin(Math.toRadians(90 * floatTime)));
float width = 120;
float left = mWidth / 3 - widthOffset;
float top = mHeight / 3 + floatTime * mHeight / 10;
if (thirdStage || fourStage) {
top = mHeight / 3;
}
float right = left + width + widthOffset;
float bottom = mHeight / 3 * 2;
float height = bottom - top;

double angle = Math.toRadians(floatTime * 360);
double sin = Math.sin(angle);

// 画填充
rectF.set(left, top, right, bottom);
if (thirdStage || fourStage) {
mFillPaint.setColor(normalColor);
} else {
int r = Color.red(normalColor) - (int) ((Color.red(normalColor) - Color.red(angryColor)) * floatTime);
int g = Color.green(normalColor) - (int) ((Color.green(normalColor) - Color.green(angryColor)) * floatTime);
int b = Color.blue(normalColor) - (int) ((Color.blue(normalColor) - Color.blue(angryColor)) * floatTime);
mFillPaint.setColor(Color.argb(255, r, g, b));
}
canvas.drawRoundRect(rectF, 4, 4, mFillPaint);

rectF.set(right - 10, top, right, bottom);
canvas.drawRoundRect(rectF, 4, 4, mFillBgPaint);

// 画身材
rectF.set(left, top, right, bottom);
// canvas.drawRect(rectF, mLinePaint);
canvas.drawRoundRect(rectF, 4, 4, mLinePaint);

// 火芯旋转
if (secondStage) {
canvas.drawLine((left + right) / 2, top, (float) ((left + right) / 2 - sin * 30), (float) (top - Math.cos(angle) * 30), mLinePaint);
} else {
canvas.drawLine((left + right) / 2, top, (left + right) / 2, top - 30, mLinePaint);
}

// 光圈部分绘制
if (thirdStage) {
float w = 0;
float cx = (left + right) / 2;
float cy = top - height / 4;
if (floatTime > 0f && floatTime < 0.3f) {
w = height / 3 + floatTime * 120;
float dx = cx - w + 15;
float dy = cy - floatTime * 100;
mPath.reset();
mPath.moveTo(dx, dy);
mPath.lineTo(dx + 20, dy - 20);
mPath.lineTo(dx + 40, dy);
mPath.lineTo(dx + 20, dy + 20);
mPath.close();
canvas.drawPath(mPath, mFirePaint);
float ex = cx + w - 45;
float ey = cy - 15 - floatTime * 100;
mPath.reset();
mPath.moveTo(ex, ey);
mPath.lineTo(ex + 20, ey - 20);
mPath.lineTo(ex + 40, ey);
mPath.lineTo(ex + 20, ey + 20);
mPath.close();
canvas.drawPath(mPath, mFirePaint);
} else if (floatTime > 0.3f && floatTime < 0.6f) {
w = height / 3 + (floatTime - 0.3f) * 120;
mPath.reset();
float dx = cx - w + 15;
float dy = cy - (floatTime - 0.3f) * 100;
mPath.moveTo(dx, dy);
mPath.lineTo(dx + 20, dy - 20);
mPath.lineTo(dx + 40, dy);
mPath.lineTo(dx + 20, dy + 20);
mPath.close();
canvas.drawPath(mPath, mFirePaint);
float ex = cx + w - 45;
float ey = cy - 15 - (floatTime - 0.3f) * 100;
mPath.reset();
mPath.moveTo(ex, ey);
mPath.lineTo(ex + 20, ey - 20);
mPath.lineTo(ex + 40, ey);
mPath.lineTo(ex + 20, ey + 20);
mPath.close();
canvas.drawPath(mPath, mFirePaint);
} else if (floatTime > 0.6f && floatTime < 0.9f) {
w = height / 3 + (floatTime - 0.6f) * 120;
}
canvas.drawCircle(cx, cy, w, mLightPaint);
}

// 画火
if ((secondStage && floatTime < 0.25) || thirdStage || (fourStage && floatTime < 0.4)) {
float startX = (left + right) / 2;
float startY = top - 84;
float endX = startX;
float endY = top - 20;
mPath.reset();
mPath.moveTo(startX, startY);
mPath.quadTo(endX + 36, endY - 14, endX, endY);
float varX = startX;
float varY = startY + floatTime * 100;
if (thirdStage || floatTime < 0.1 || (fourStage && floatTime < 0.4)) {
mPath.quadTo(endX - 36, endY - 14, startX, startY);
} else {
mPath.quadTo(endX - 16, endY - 4, endX - 26, endY - 22);
mPath.quadTo(varX, varY, startX, startY);
}
mPath.close();
canvas.drawPath(mPath, mFirePaint);
}

// 吹我,i'll go to die
if (fourStage && floatTime > 0.4) {
double a = Math.toRadians(floatTime * 90);
double c = Math.cos(a);
double s = Math.sin(a);
float cx = (float) (left - 100 - s * 100);
float cy = top - 40;
canvas.drawLine(cx, cy, (float) (cx + c * 240), cy, mLinePaint);
if (floatTime < 0.95) {
canvas.drawCircle((float) (left + width / 2 - s * 30), (float) (top - 50 - s * 22), 15 * (floatTime - 0.4f), mLinePaint);
}
}

// eye
if (firstStage && floatTime > 0.0f) {
canvas.translate((float) sin * 3, 0);
canvas.drawLine(left + width * 1 / 4 - 5, top + height * 1 / 3 - 6, left + width * 1 / 4 + 5, top + height * 1 / 3, mAngryEyePaint);
canvas.drawLine(left + width * 1 / 4 - 5, top + height * 1 / 3 + 6, left + width * 1 / 4 + 5, top + height * 1 / 3, mAngryEyePaint);

canvas.drawLine(left + width * 3 / 4 + 5, top + height * 1 / 3 - 6, left + width * 3 / 4 - 5, top + height * 1 / 3, mAngryEyePaint);
canvas.drawLine(left + width * 3 / 4 + 5, top + height * 1 / 3 + 6, left + width * 3 / 4 - 5, top + height * 1 / 3, mAngryEyePaint);
} else {
canvas.drawCircle(left + width * 1 / 4, top + height * 1 / 3, 5, mCirclePaint);
canvas.drawCircle(left + width * 3 / 4, top + height * 1 / 3, 5, mCirclePaint);
}

}

private void drawBoyCandle(Canvas canvas) {
float width = 120;
float left = mWidth / 2;
float top = mHeight / 9;
float right = left + width;
float bottom = mHeight / 3 * 2;
float height = bottom - top;

rectF.set(left, top, right, bottom);
mFillPaint.setColor(Color.parseColor("#ffffff"));
canvas.drawRoundRect(rectF, 4, 4, mFillPaint);

rectF.set(right - 10, top, right, bottom);
canvas.drawRoundRect(rectF, 4, 4, mFillBgPaint);

rectF.set(left, top, right, bottom);
// canvas.drawRect(rectF, mLinePaint);
canvas.drawRoundRect(rectF, 4, 4, mLinePaint);
canvas.drawLine((left + right) / 2, top, (left + right) / 2, top - 40, mLinePaint);

float lx = left + width * 1 / 3;
float ly = top + height * 1 / 6;
float rx = left + width * 2 / 3;
float ry = top + height * 1 / 6;
mPath.reset();
mPath.moveTo(lx, ly);
mPath.lineTo(lx - 30, ly - 20);
mPath.quadTo(lx - 35, ly + 5, lx, ly);
mPath.close();
mPathMeasure.setPath(mPath, true);
if (fourStage) {
canvas.drawCircle(position[0], position[1], 5, mCirclePaint);
canvas.drawCircle(position[0] + rx - lx, position[1], 5, mCirclePaint);
} else {
canvas.drawCircle(lx, ly, 5, mCirclePaint);
canvas.drawCircle(rx, ry, 5, mCirclePaint);
}

// draw mouth
float mx = (lx + rx) / 2;
float my = top + height * 1 / 3;
if (fourStage && floatTime < 0.98f) {
double a = Math.toRadians(floatTime * 180);
double s = Math.sin(a);
canvas.drawCircle(mx - floatTime * 32f, my - floatTime * 24f, (float) (s * 16), mCirclePaint);
}
}

private void switchStage(int s) {
switch (s) {
case 1:
firstStage = true;
secondStage = false;
thirdStage = false;
fourStage = false;
break;
case 2:
firstStage = false;
secondStage = true;
thirdStage = false;
fourStage = false;
break;
case 3:
firstStage = false;
secondStage = false;
thirdStage = true;
fourStage = false;
break;
case 4:
firstStage = false;
secondStage = false;
thirdStage = false;
fourStage = true;
break;
}
}

public void startAnimation() {
switchStage(1);
firstStageAnimation();
}

private void firstStageAnimation() {
ValueAnimator firstAnimator = ValueAnimator.ofFloat(0, 1);
firstAnimator.setDuration(1000);
firstAnimator.setInterpolator(new LinearInterpolator());
firstAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
floatTime = (float) animation.getAnimatedValue();
postInvalidate();
}
});
firstAnimator.start();
firstAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
switchStage(2);
secondStageAnimation();
}
});
}

private void secondStageAnimation() {
ValueAnimator secondAnimator = ValueAnimator.ofFloat(1, 0);
secondAnimator.setDuration(1000);
secondAnimator.setInterpolator(new BounceInterpolator());
secondAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
floatTime = (float) animation.getAnimatedValue();
postInvalidate();
}
});
secondAnimator.start();
secondAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
switchStage(3);
thirdStageAnimation();
}
});
}

private void thirdStageAnimation() {
ValueAnimator thirdAnimator = ValueAnimator.ofFloat(0, 1);
thirdAnimator.setDuration(1500);
thirdAnimator.setInterpolator(new LinearInterpolator());
thirdAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
floatTime = (float) animation.getAnimatedValue();
postInvalidate();
}
});
thirdAnimator.start();
thirdAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
switchStage(4);
fourStageAnimation();
}
});
}

private void fourStageAnimation() {
// ValueAnimator fourAnimator = ValueAnimator.ofFloat(0, 1);
// fourAnimator.setDuration(1000);
// fourAnimator.setInterpolator(new LinearInterpolator());
// fourAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// floatTime = (float) animation.getAnimatedValue();
// invalidate();
// }
// });
// fourAnimator.start();

ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, mPathMeasure.getLength());
valueAnimator.setDuration(1000);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
mPathMeasure.getPosTan(value, position, null);
floatTime = value * 1.0f / mPathMeasure.getLength();
postInvalidate();
}
});
valueAnimator.start();
}

}

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