使用SurfaceView实现简单的红包雨动画,供大家参考,具体内容如下
具体代码:SurfaceViewDemo
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
|
public class TranslateSurfaceView extends SurfaceView implements DrawInterface { private DrawHandler drawHandler; private int width; private int height; private Bitmap bitmap; private int bitmapWidth; private int bitmapHeight; private Toast toast; private int count = 0 ; private ArrayList<MoveModel> moveList = new ArrayList<>(); public TranslateSurfaceView(Context context) { this (context, null ); } public TranslateSurfaceView(Context context, AttributeSet attrs) { this (context, attrs, - 1 ); } public TranslateSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { super (context, attrs, defStyleAttr); init(); } private void init() { SurfaceHolder holder = getHolder(); setZOrderOnTop( true ); holder.setFormat(PixelFormat.TRANSLUCENT); } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { super .onMeasure(widthMeasureSpec, heightMeasureSpec); width = getMeasuredWidth(); height = getMeasuredHeight(); } public void prepare() { DrawThread drawThread = new DrawThread(); drawThread.start(); drawHandler = new DrawHandler(drawThread.getLooper(), this ); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_redenvelope2); bitmapWidth = bitmap.getWidth(); bitmapHeight = bitmap.getHeight(); } public void addMoveModel(MoveModel moveModel) { moveList.add(moveModel); } public void start() { count = 0 ; moveList.clear(); for ( int i = 0 ; i < 10 ; i++) { generateModel(); } drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } public void resume() { drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } public void pause() { drawHandler.sendEmptyMessage(DrawHandler.STOP_DRAW_KEY); } public void quit() { if ( null != bitmap) { bitmap.recycle(); } if ( null != drawHandler) { drawHandler.removeCallbacksAndMessages( null ); drawHandler.getLooper().quit(); } } @Override public void startDraw() { SurfaceHolder holder = getHolder(); Canvas canvas = holder.lockCanvas(); if ( null == canvas) { return ; } Paint paint = new Paint(); paint.setAntiAlias( true ); canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); for (MoveModel moveModel : moveList) { canvas.drawBitmap(bitmap, moveModel.x, moveModel.y, paint); if (moveModel.x > width || moveModel.y > height) { resetMoveModel(moveModel); } else { moveModel.y += moveModel.randomY; } } holder.unlockCanvasAndPost(canvas); drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } @Override public void stopDraw() { drawHandler.removeMessages(DrawHandler.START_DRAW_KEY); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_DOWN: checkInRect(( int ) event.getX(), ( int ) event.getY()); break ; } return true ; } /** * 是否点击在红包区域 * @param x * @param y */ private void checkInRect( int x, int y) { int length = moveList.size(); for ( int i = 0 ; i < length; i++) { MoveModel moveModel = moveList.get(i); Rect rect = new Rect(( int ) moveModel.x, ( int ) moveModel.y, ( int ) moveModel.x + bitmapWidth, ( int ) moveModel.y + bitmapHeight); if (rect.contains(x, y)) { count++; resetMoveModel(moveModel); if (toast == null ) { toast = Toast.makeText(getContext(), "" , Toast.LENGTH_SHORT); } toast.setText( "抢到红包" + count); toast.show(); break ; } } } private void resetMoveModel(MoveModel moveModel) { Random random = new Random(); moveModel.x = random.nextInt( 11 ) * (width / 10 ); moveModel.y = 0 ; moveModel.randomY = (random.nextInt( 5 ) + 2 ) * getResources().getDisplayMetrics().density * 1 .4f; } private void generateModel() { Random random = new Random(); MoveModel moveModel = new MoveModel(); moveModel.moveId = moveList.size() + 1 ; moveModel.x = random.nextInt( 11 ) * (width / 10 ); moveModel.y = 0 ; moveModel.randomY = (random.nextInt( 5 ) + 2 ) * getResources().getDisplayMetrics().density * 1 .4f; moveList.add(moveModel); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wsdaijianjun/article/details/75646081