-
Notifications
You must be signed in to change notification settings - Fork 12
/
BloomEffector.java
358 lines (303 loc) 路 12.6 KB
/
BloomEffector.java
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
package me.samlss.bloom.effector;
import android.animation.TimeInterpolator;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import me.samlss.bloom.particle.BloomParticle;
/**
* @author SamLeung
* @e-mail samlssplus@gmail.com
* @github https://github.com/samlss
* @description The bloom effector.
*/
public class BloomEffector {
private Map<BloomParticle, ParticleFiled> mParticleFieldMap;
private float mMinSpeed = 0.1f;
private float mMaxSpeed = 0.5f;
private float mMinScale;
private float mMaxScale;
private float mMinSkew;
private float mMaxSkew;
private float mMinAcceleration;
private float mMaxAcceleration;
private int mMinAccelerationAngel;
private int mMaxAccelerationAngel;
private float mMinRotationSpeed;
private float mMaxRotationSpeed;
private Random mRandom;
private float mAnchorX;
private float mAnchorY;
private long mFadeOutStartTime;
private TimeInterpolator mFadeOutInterpolator;
/**
* Effect execution time, the default is 800ms
* */
private long mDuration = 300;
/**
* Effect execution interpolator, the default is {@link AccelerateDecelerateInterpolator}
* */
private TimeInterpolator mEffectInterpolator = new AccelerateDecelerateInterpolator();
private BloomEffector() {
mRandom = new Random();
mParticleFieldMap = new HashMap<>();
}
/**
* Get the duration of the bloom effect animation.
* */
public long getDuration() {
return mDuration;
}
/**
* Set the duration of the bloom effect animation.
* */
public void setDuration(long duration) {
this.mDuration = duration;
}
/**
* Get the interpolator of the bloom effect animation.
* */
public TimeInterpolator getInterpolator() {
return mEffectInterpolator;
}
/**
* Set the interpolator of the bloom effect animation.
* */
public void setInterpolator(TimeInterpolator interpolator) {
this.mEffectInterpolator = interpolator;
}
/**
* This is used to handle the animation of particles.
* When you need to customize the animation effect, you can extend this class and implement this method.
*
* @param milliSecond The animated value, [0, {@link #getDuration()}].
* @param particle The particle.
* */
public void apply(long milliSecond, BloomParticle particle) {
if (particle == null){
return;
}
ParticleFiled particleFiled = mParticleFieldMap.get(particle);
if (particleFiled == null){
particleFiled = createParticleFiled(particle);
}
particle.setRotation(particle.getRotation() + milliSecond * particleFiled.rotationSpeed);
particle.setDrawX(particle.getInitialX()+particleFiled.speedX * milliSecond + particleFiled.accelerationX*milliSecond*milliSecond);
particle.setDrawY(particle.getInitialY()+particleFiled.speedY * milliSecond + particleFiled.accelerationY*milliSecond*milliSecond);
updateFadeOut(milliSecond, particle);
}
private void updateFadeOut(long milliSecond, BloomParticle particle){
if (milliSecond < mFadeOutStartTime
|| mFadeOutInterpolator == null
|| particle.getInitialAlpha() == 0) {
return;
}
float fadeOutInterpolatorValue = mFadeOutInterpolator.getInterpolation((milliSecond- mFadeOutStartTime)* 1f / (mDuration - mFadeOutStartTime));
int alpha = (int) (particle.getInitialAlpha() - particle.getInitialAlpha()*fadeOutInterpolatorValue);
particle.setAlpha(alpha);
}
private ParticleFiled createParticleFiled(BloomParticle particle){
ParticleFiled particleFiled = new ParticleFiled();
float randomSpeed = mRandom.nextFloat()*(mMaxSpeed-mMinSpeed) + mMinSpeed;
double angleInRads = getRadian(particle.getInitialX(), particle.getInitialY(), mAnchorX, mAnchorY);
particleFiled.speedX = (float) (randomSpeed * Math.cos(angleInRads));
particleFiled.speedY = (float) (randomSpeed * Math.sin(angleInRads));
float angle = mMinAccelerationAngel;
if (mMaxAccelerationAngel != mMinAccelerationAngel) {
angle = mRandom.nextInt(mMaxAccelerationAngel - mMinAccelerationAngel) + mMinAccelerationAngel;
}
float angleInRadsAcc = (float) (angle*Math.PI / 180f);
float value = mRandom.nextFloat()*(mMaxAcceleration-mMinAcceleration)+mMinAcceleration;
particleFiled.accelerationX = (float) (value * Math.cos(angleInRadsAcc));
particleFiled.accelerationY = (float) (value * Math.sin(angleInRadsAcc));
if (mMinScale >= 0
&& mMaxScale > mMinScale) {
particle.setScale(mRandom.nextFloat() * (mMaxScale - mMinScale) + mMinScale);
}
if (mMinSkew >= 0
&& mMaxSkew > mMinSkew) {
particle.setSkew(mRandom.nextFloat() * (mMaxSkew - mMinSkew) + mMinSkew);
}
particleFiled.rotationSpeed = mRandom.nextFloat()*(mMaxRotationSpeed-mMinRotationSpeed) + mMinRotationSpeed;
mParticleFieldMap.put(particle, particleFiled);
return particleFiled;
}
public void destroy() {
mParticleFieldMap.clear();
}
/**
* Get the radian from two points.
*
* @param x1 The x coordinate of point1.
* @param x2 The y coordinate of point1.
* @param y1 The x coordinate of point2.
* @param y2 The y coordinate of point2.
* */
public static float getRadian(float x1, float y1, float x2, float y2) {
float lenX = x1 - x2;
float lenY = y1 - y2;
float lenXY = (float) Math.sqrt((double) (lenX * lenX + lenY * lenY));
return (float) (Math.acos(lenX / lenXY) * (y1 < y2 ? -1 : 1));
}
/**
* The filed of particle.
* */
private class ParticleFiled{
float speedX;
float speedY;
float accelerationX;
float accelerationY;
float rotationSpeed;
}
/**
* To build a {@link BloomEffector}
* */
public static class Builder{
private BloomEffector mBloomEffector;
public Builder(){
mBloomEffector = new BloomEffector();
}
/**
* Sets the length of the bloom effect animation in milliseconds.
*
* @param duration The length of the animation, in milliseconds. This value cannot be negative.
* */
public Builder setDuration(long duration){
mBloomEffector.setDuration(duration);
return this;
}
/**
* Set the interpolator of the bloom effect animation.
* the default is {@link AccelerateDecelerateInterpolator}.
*
* @param interpolator The animation interpolator.
* */
public Builder setInterpolator(TimeInterpolator interpolator) {
mBloomEffector.setInterpolator(interpolator);
return this;
}
/**
* Set anchor points for all the particles.
*
* @param anchorX The bloom anchor x coordinate.
* @param anchorY The bloom anchor y coordinate.
* */
public Builder setAnchor(float anchorX, float anchorY){
mBloomEffector.mAnchorX = anchorX;
mBloomEffector.mAnchorY = anchorY;
return this;
}
/**
* Set the speed range for the particles.
*
* @param minSpeed The minimum speed value, the default is 0.1.
* @param maxSpeed The maximum speed value, the default is 0.5.
* */
public Builder setSpeedRange(float minSpeed, float maxSpeed){
mBloomEffector.mMinSpeed = minSpeed;
mBloomEffector.mMaxSpeed = maxSpeed;
return this;
}
/**
* Set the scale range for the particles.
*
* @param minScale The minimum scale value.
* @param maxScale The maximum scale value.
* */
public Builder setScaleRange(float minScale, float maxScale){
mBloomEffector.mMinScale = minScale;
mBloomEffector.mMaxScale = maxScale;
return this;
}
/**
* Set the skew range for the particles.
*
* @param minSkew The minimum speed value.
* @param maxSkew The maximum speed value.
* */
public Builder setSkewRange(float minSkew, float maxSkew){
mBloomEffector.mMinSkew = minSkew;
mBloomEffector.mMaxSkew = maxSkew;
return this;
}
/**
* Set the rotation speed range for the particles.
*
* @param minRotationSpeedRange The minimum speed value.
* @param maxRotationSpeedRange The maximum speed value.
* */
public Builder setRotationSpeedRange(float minRotationSpeedRange, float maxRotationSpeedRange){
mBloomEffector.mMinRotationSpeed = minRotationSpeedRange;
mBloomEffector.mMaxRotationSpeed = maxRotationSpeedRange;
return this;
}
/**
* Set particle acceleration, the acceleration is measured in pixels per square millisecond.
* The angel controls the acceleration direction.
*
* Calculated as follows:
*
* float angelInRadsAcc = (float) (accelerationAngle*Math.PI / 180f)
*
* The final x axis acceleration: accelerationX = (float) (value * Math.cos(angleInRadsAcc));
* The final y axis acceleration: accelerationY = (float) (value * Math.sin(angleInRadsAcc));
*
* @param acceleration The acceleration value.
* @param accelerationAngle The acceleration angele [0-360].
* */
public Builder setAcceleration(float acceleration, int accelerationAngle) {
return setAccelerationRange(acceleration, acceleration, accelerationAngle, accelerationAngle);
}
/**
* This method takes random acceleration and acceleration angles from the range of acceleration and acceleration angles you set.
*
* Set particle acceleration, the acceleration is measured in pixels per square millisecond.
* The angel controls the acceleration direction.
*
* Calculated as follows:
*
* float angelInRadsAcc = (float) (accelerationAngle*Math.PI / 180f)
*
* The final x axis acceleration: accelerationX = (float) (value * Math.cos(angleInRadsAcc));
* The final y axis acceleration: accelerationY = (float) (value * Math.sin(angleInRadsAcc));
*
* @param minAcceleration The minimum acceleration.
* @param maxAcceleration The maximum acceleration.
* @param minAccelerationAngel The minimum acceleration angel[0-360].
* @param maxAccelerationAngel The maximum acceleration angel[0-360].
* */
public Builder setAccelerationRange(float minAcceleration, float maxAcceleration, int minAccelerationAngel,
int maxAccelerationAngel) {
mBloomEffector.mMinAcceleration = minAcceleration;
mBloomEffector.mMaxAcceleration = maxAcceleration;
mBloomEffector.mMinAccelerationAngel = minAccelerationAngel;
mBloomEffector.mMaxAccelerationAngel = maxAccelerationAngel;
return this;
}
/**
* Set the fade out effect for all particle.
*
* @param startTime Start time that relative of {@link #setDuration(long)}. For example, if you set the duration to 800,
* then you can set the fadeout start time to 600, that is, start the fade out effect when the particle animation is executed to 600.
* @param interpolator The interpolator of the fade out effect.
* */
public Builder setFadeOut(long startTime, TimeInterpolator interpolator){
mBloomEffector.mFadeOutStartTime = startTime;
mBloomEffector.mFadeOutInterpolator = interpolator;
return this;
}
/**
* Set the fade out effect for all particle.
*
* @param startTime Start time that relative of {@link #setDuration(long)}. For example, if you set the duration to 800,
* then you can set the fadeout start time to 600, that is, start the fade out effect when the particle animation is executed to 600.
* */
public Builder setFadeOut(long startTime){
return setFadeOut(startTime, new LinearInterpolator());
}
public BloomEffector build(){
return mBloomEffector;
}
}
}