Created
February 3, 2020 11:13
-
-
Save cliffgr/c3fe742d86ed3bd718431acb8a24d238 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wappier.wappierSDK.loyalty.base.wrappers.particlesystem; | |
import android.animation.ValueAnimator; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import java.util.HashSet; | |
import java.util.Random; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class WPParticleSystem extends View { | |
private static final int MAX_PARTICLE = 100; | |
private HashSet<Particle> particles; | |
private int startYPos; | |
private int startXPos; | |
private float minSpeed; | |
private float speedRange; | |
private Random rnd; | |
private ValueAnimator animator; | |
private ExecutorService executorService; | |
boolean mainJobFinished = true; | |
Runnable runnable = new Runnable() { | |
@Override | |
public void run() { | |
mainJobFinished = false; | |
for (Particle particle : particles) { | |
particle.updatePhysics(3f); | |
} | |
mainJobFinished = true; | |
} | |
}; | |
public WPParticleSystem(Context context) { | |
super(context); | |
init(); | |
} | |
public WPParticleSystem(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public WPParticleSystem(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
startYPos = h; | |
startXPos = w; | |
for (int i = 0; i < MAX_PARTICLE; i++) { | |
particles.add(new Particle(startYPos, startXPos, minSpeed, speedRange, Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)))); | |
} | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
startNewAnimation(); | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
super.onDetachedFromWindow(); | |
if (!executorService.isShutdown()) { | |
executorService.shutdownNow(); | |
} | |
particles.clear(); | |
animator.cancel(); | |
animator.removeAllUpdateListeners(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
for (Particle particle : particles) { | |
particle.doDraw(canvas); | |
} | |
} | |
private void startNewAnimation() { | |
// Never-ending animator, we will cancel once the termination condition is reached. | |
animator = ValueAnimator.ofInt(0) | |
.setDuration(Long.MAX_VALUE); | |
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(ValueAnimator valueAnimator) { | |
executorService.execute(runnable); | |
if (mainJobFinished) | |
invalidate(); | |
} | |
}); | |
animator.start(); | |
} | |
private void init() { | |
rnd = new Random(); | |
executorService = Executors.newSingleThreadExecutor(); | |
particles = new HashSet<>(); | |
minSpeed = 0.2f; | |
speedRange = 0.5f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment