DivergeView.java
8.44 KB
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
package com.cnlive.goldenline.ui.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Created by Dean.zhang on 16/3/28.
* 飘心
*/
public class DivergeView extends View implements Runnable{
public static final float mDuration = 0.010F;
public static final int mDefaultHeight = 100;
protected static final long mQueenDuration = 200;
protected final Random mRandom = new Random();
@Nullable
protected ArrayList<DivergeInfo> mDivergeInfos;
@Nullable
protected List<Object> mQueen;
@Nullable
protected PointF mPtStart;
@Nullable
protected PointF mPtEnd;
@Nullable
protected ArrayList<DivergeInfo> mDeadPool = new ArrayList<>();
private Paint mPaint;
// private static final int mDefaultWidth = 100;
// private static final int mAlphaOffset = 50;
private DivergeViewProvider mDivergeViewProvider;
private long mLastAddTime = 0;
private Thread mThread;
private boolean mRunning = true;
private boolean mIsDrawing = false;
public DivergeView(Context context) {
this(context, null);
}
public DivergeView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DivergeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* Loop
*/
@Override
public void run() {
while(mRunning){
if(mDivergeViewProvider == null){
continue;
}
if(mQueen == null){
continue;
}
if(mIsDrawing){
//如果正在绘制,不要处理数据
continue;
}
if(mDivergeInfos == null){
continue;
}
dealQueen();
if(mDivergeInfos.size() == 0){
continue;
}
dealDiverge();
mIsDrawing = true;
postInvalidate();
}
//停止
release();
}
private void dealDiverge(){
for (int i = 0; i < mDivergeInfos.size(); i++) {
DivergeInfo divergeInfo = mDivergeInfos.get(i);
float timeLeft = 1.0F - divergeInfo.mDuration;
divergeInfo.mDuration += mDuration;
float x, y;
//二次贝塞尔
float time1 = timeLeft * timeLeft;
float time2 = 2 * timeLeft * divergeInfo.mDuration;
float time3 = divergeInfo.mDuration * divergeInfo.mDuration;
x = time1 * (mPtStart.x)
+ time2 * (divergeInfo.mBreakPoint.x)
+ time3 * (divergeInfo.mEndPoint.x);
divergeInfo.mX = x;
y = time1 * (mPtStart.y)
+ time2 * (divergeInfo.mBreakPoint.y)
+ time3 * (divergeInfo.mEndPoint.y);
divergeInfo.mY = y;
if (divergeInfo.mY <= divergeInfo.mEndPoint.y) {
mDivergeInfos.remove(i);
mDeadPool.add(divergeInfo);
i--;
continue;
}
}
}
private void dealQueen(){
long now = System.currentTimeMillis();
if(mQueen.size() > 0 && now - mLastAddTime > mQueenDuration){
mLastAddTime = System.currentTimeMillis();
DivergeInfo divergeInfo = null;
if(mDeadPool.size() > 0){
//死池里面有空闲的divergeNode
divergeInfo = mDeadPool.get(0);
mDeadPool.remove(0);
}
if(divergeInfo == null){
divergeInfo = createDivergeNode(mQueen.get(0));
}
divergeInfo.reset();
divergeInfo.mType = mQueen.get(0);
mDivergeInfos.add(divergeInfo);
mQueen.remove(0);
}
}
public interface DivergeViewProvider{
@NonNull
public Bitmap getBitmap(Object obj);
}
private void init(){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//不需要支持wrap_content
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
public void setDivergeViewProvider(DivergeViewProvider divergeViewProvider){
mDivergeViewProvider = divergeViewProvider;
}
@Nullable
public PointF getStartPoint(){
return mPtStart;
}
public boolean isRunning(){
return mRunning;
}
public void startDiverges(Object obj){
if(mDivergeInfos == null){
mDivergeInfos = new ArrayList<>(30);
}
if(mQueen == null){
mQueen = Collections.synchronizedList(new ArrayList<>(30));
}
mQueen.add(obj);
// for(Object obj : objs) {
// mQueen.add(obj);
// }
if(mThread == null) {
mThread = new Thread(this);
mThread.start();
}
}
public void stop(){
if(mDivergeInfos != null){
mDivergeInfos.clear();
}
if(mQueen != null){
mQueen.clear();
}
if(mDeadPool != null){
mDeadPool.clear();
}
}
public void release(){
stop();
mPtEnd = null;
mPtStart = null;
mDivergeInfos = null;
mQueen = null;
mDeadPool = null;
}
public void setStartPoint(PointF point){
mPtStart = point;
}
public void setEndPoint(PointF point){
mPtEnd = point;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mRunning = false;
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
if(mRunning && mDivergeViewProvider != null && mDivergeInfos != null){
for(DivergeInfo divergeInfo : mDivergeInfos){
mPaint.setAlpha((int)(255 * divergeInfo.mY / mPtStart.y));
canvas.drawBitmap(mDivergeViewProvider.getBitmap(divergeInfo.mType),
divergeInfo.mX,
divergeInfo.mY,
mPaint);
}
}
mIsDrawing = false;
}
@NonNull
private PointF getBreakPointF(int scale1, int scale2) {
PointF pointF = new PointF();
pointF.x = mRandom.nextInt((getMeasuredWidth() - getPaddingRight() + getPaddingLeft()) / scale1) + getMeasuredWidth() / scale2;
pointF.y = mRandom.nextInt((getMeasuredHeight() - getPaddingBottom() + getPaddingTop()) / scale1) + getMeasuredHeight() / scale2;
return pointF;
}
@NonNull
protected DivergeInfo createDivergeNode(Object type){
PointF endPoint = mPtEnd;
if(endPoint == null){
endPoint = new PointF(mRandom.nextInt(getMeasuredWidth()),0);
}
// int height = mDivergeViewProvider == null ? mDefaultHeight : mDivergeViewProvider.getBitmap(type).getHeight();
if(mPtStart == null) {
mPtStart = new PointF(getMeasuredWidth() / 2, getMeasuredHeight() - mDefaultHeight);//默认起始高度
}
return new DivergeInfo(
mPtStart.x,
mPtStart.y,
getBreakPointF(2, 3),
endPoint,
type);
}
public class DivergeInfo {
public float mDuration;
public PointF mBreakPoint;
public PointF mEndPoint;
public float mX;
public float mY;
public Object mType;
public float mStartX;
public float mStartY;
public DivergeInfo(float x, float y, PointF breakPoint, PointF endPoint, Object type){
mDuration = 0.0f;
mEndPoint = endPoint;
mX = x;
mY = y;
mStartX = x;
mStartY = y;
mBreakPoint = breakPoint;
mType = type;
}
public void reset(){
mDuration = 0.0f;
mX = mStartX;
mY = mStartY;
}
}
}