# 自定义 View,代码如下:
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
package net.cloudelevator.anim;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List
public class MyCanvas extends View {
Paint paint;
Paint tpaint;
private int[] list = new int[192];
Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what) {
case 1:
postInvalidate();
handler.sendEmptyMessageDelayed(1, 0);
break;
}
}
};

public MyCanvas(Context context) {
super(context);
for (int i = 0; i < 192; i++) {
list[i] = 1;
}
for (int j = 0; j < 100; j++) {
for (int i = 0; i < list.length; i++) {
if (list[i] > 758 + Math.random() * 1e4) {
list[i] = 0;
} else {
list[i] = list[i] + 10;
}
}
}
handler.sendEmptyMessageDelayed(1, 0);
}

public MyCanvas(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
for (int i = 0; i < 192; i++) {
list[i] = 1;
}
for (int j = 0; j < 100; j++) {
for (int i = 0; i < list.length; i++) {
if (list[i] > 758 + Math.random() * 1e4) {
list[i] = 0;
} else {
list[i] = list[i] + 10;
}
}
}
handler.sendEmptyMessageDelayed(1, 0);
}

public MyCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
for (int i = 0; i < 192; i++) {
list[i] = 1;
}
for (int j = 0; j < 100; j++) {
for (int i = 0; i < list.length; i++) {
if (list[i] > 758 + Math.random() * 1e4) {
list[i] = 0;
} else {
list[i] = list[i] + 10;
}
}
}
handler.sendEmptyMessageDelayed(1, 0);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
tpaint = new Paint();
tpaint.setColor(Color.WHITE);
paint.setStrokeWidth(0);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
canvas.drawBitmap(bitmap, 0, 0, paint);
for (int i = 0; i < list.length; i++) {
canvas.drawText("\uD83C\uDF42", i * 50, list[i], tpaint);
if (list[i] > 758 + Math.random() * 1e4) {
list[i] = 0;
} else {
list[i] = list[i] + 10;
}
}
}
}