本文实例为大家分享了Android自定义View实现雪花特效展示的具体代码,供大家参考,具体内容如下
效果图
1.SnowView 类
复制代码
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
217package com.ilz.rocketapplication.handaccount.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.widget.RelativeLayout; import com.ilz.rocketapplication.handaccount.R; import com.ilz.rocketapplication.handaccount.bean.SnowBean; import com.ilz.rocketapplication.handaccount.utils.ColorUtils; import com.ilz.rocketapplication.handaccount.utils.Tools; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class SnowView extends RelativeLayout { // private final String SNOW = "❄"; // private final String SNOW = "☀❆★❉❈❀✿❃❁"; private final String SNOW = "❄"; private float vX = 2.5f;//风向 >0 右边飘 <0 左边飘 private float vY = 5f;//下落速度 <0你的雪花要往上飘呀 private int snowCount = 50;//雪花个数 private List<SnowBean> snowBeanList = new ArrayList<>(); private int XB = Tools.getWindowsWidth(); private int YB = Tools.getWindowsHeight(); private Paint paint = new Paint(); private Timer timer; private boolean isStart = false; public SnowView(Context context) { this(context, null); } public SnowView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SnowView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } private void initView() { paint.setAntiAlias(true); initSnowData(); } public void start() { if (timer == null) { timer = new Timer(); } isStart = true; timer.schedule(new TimerTask() { @Override public void run() { if (!isStart) return; for (int i = 0; i < snowBeanList.size(); i++) { snowBeanList.get(i).setX(snowBeanList.get(i).getX() + vX); snowBeanList.get(i).setY(snowBeanList.get(i).getY() + vY); if (snowBeanList.get(i).getX() < 0 || snowBeanList.get(i).getX() > XB) { snowBeanList.get(i).setX(getRandomX()); } if (snowBeanList.get(i).getY() < 0 || snowBeanList.get(i).getY() > YB) { snowBeanList.get(i).setY(0f); } } postInvalidate(); } }, 0, 15); } public void resume() { if (timer == null) { start(); } isStart = true; } public void pause(){ isStart = false; } public void destroy() { isStart = false; if (snowBeanList != null) { snowBeanList.clear(); } invalidate(); if (timer != null) { timer.cancel(); timer = null; } } private void initSnowData() { for (int i = 0; i < snowCount; i++) { SnowBean bean = new SnowBean(); bean.setX(getRandomX()); bean.setY(getRandomY()); bean.setSize((float) (Math.random() * 50) + 5); snowBeanList.add(bean); } } private float getRandomX() { return (float) (Math.random() * Tools.getWindowsWidth()); } private float getRandomY() { return (float) (Math.random() * Tools.getWindowsHeight()); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < snowBeanList.size(); i++) { SnowBean bean = snowBeanList.get(i); paint.setTextSize(bean.getSize()); paint.setColor(bean.getColor()); canvas.drawText(SNOW, bean.getX(), bean.getY(), paint); } } private GestureDetector detector = new GestureDetector(getContext(),new MyGestureDetector()); private boolean isPoint = false; private long pointTime = 0; @Override public boolean onTouchEvent(MotionEvent event) { // switch (event.getAction()) { // case MotionEvent.ACTION_DOWN: // pointTime = 0; // int pCount = event.getPointerCount(); // if (pCount >= 2) { // isPoint = true; // pointTime = System.currentTimeMillis(); // } // break; // case MotionEvent.ACTION_MOVE: // break; // case MotionEvent.ACTION_UP: // isPoint = false; // pointTime = 0; // break; // } // return super.onTouchEvent(event); return detector.onTouchEvent(event); } private class MyGestureDetector implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener { @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { return false; } @Override public boolean onDoubleTap(MotionEvent e) { return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { return false; } } }
2.SnowBean
复制代码
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
44package com.ilz.rocketapplication.handaccount.bean; import android.graphics.Color; import com.ilz.rocketapplication.handaccount.utils.ColorUtils; public class SnowBean { float x; float y; float size; int color = Color.WHITE; public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; } public float getSize() { return size; } public void setSize(float size) { this.size = size; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } }
3.Tools
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21/** * 获取屏幕的宽度 */ public static int getWindowsWidth() { WindowManager wm = (WindowManager) (MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE)); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); int mScreenWidth = dm.widthPixels; return mScreenWidth; } /** * 获取屏幕的高度 */ public static int getWindowsHeight() { WindowManager wm = (WindowManager) (MyApplication.getInstance().getSystemService(Context.WINDOW_SERVICE)); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); int mScreenHeigh = dm.heightPixels; return mScreenHeigh; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是疯狂唇膏最近收集整理的关于Android自定义View实现雪花特效的全部内容,更多相关Android自定义View实现雪花特效内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复