Skip to content

Commit

Permalink
fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
plattysoft committed Jun 11, 2015
1 parent be1604b commit a163b65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public void setParticles(ArrayList<Particle> particles) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw all the particles
for (int i=0; i<mParticles.size(); i++) {
mParticles.get(i).draw(canvas);
synchronized (mParticles) {
for (int i = 0; i < mParticles.size(); i++) {
mParticles.get(i).draw(canvas);
}
}
}
}
32 changes: 17 additions & 15 deletions LeonidsLib/src/main/java/com/plattysoft/leonids/ParticleSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ParticleSystem {
private ParticleField mDrawingView;

private ArrayList<Particle> mParticles;
private ArrayList<Particle> mActiveParticles;
private final ArrayList<Particle> mActiveParticles = new ArrayList<Particle>();
private long mTimeToLive;
private long mCurrentTime = 0;

Expand Down Expand Up @@ -72,7 +72,7 @@ private ParticleSystem(Activity a, int maxParticles, long timeToLive, int parent

mMaxParticles = maxParticles;
// Create the particles
mActiveParticles = new ArrayList<Particle>();

mParticles = new ArrayList<Particle> ();
mTimeToLive = timeToLive;

Expand Down Expand Up @@ -142,9 +142,9 @@ else if (drawable instanceof AnimationDrawable) {
mParticles.add (new AnimatedParticle (animation));
}
}
else {
// else {
// Not supported, no particles are being created
}
// }
}

public float dpToPx(float dp) {
Expand Down Expand Up @@ -210,7 +210,7 @@ public ParticleSystem(Activity a, int maxParticles, AnimationDrawable animation,
/**
* Adds a modifier to the Particle system, it will be executed on each update.
*
* @param modifier
* @param modifier modifier to be added to the ParticleSystem
*/
public ParticleSystem addModifier(ParticleModifier modifier) {
mModifiers.add(modifier);
Expand Down Expand Up @@ -357,7 +357,7 @@ private void startEmiting(int particlesPerSecond) {
mDrawingView.setParticles (mActiveParticles);
updateParticlesBeforeStartTime(particlesPerSecond);
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
mTimer.schedule(new TimerTask() {
@Override
public void run() {
onUpdate(mCurrentTime);
Expand Down Expand Up @@ -389,7 +389,7 @@ private void startEmiting(int particlesPerSecond, int emitingTime) {
mDrawingView.setParticles (mActiveParticles);
updateParticlesBeforeStartTime(particlesPerSecond);
mEmitingTime = emitingTime;
startAnimator(new LinearInterpolator(), emitingTime+mTimeToLive);
startAnimator(new LinearInterpolator(), emitingTime + mTimeToLive);
}

public void emit (int emitterX, int emitterY, int particlesPerSecond) {
Expand Down Expand Up @@ -429,7 +429,7 @@ public void oneShot(View emiter, int numParticles, Interpolator interpolator) {
// Add a full size view to the parent view
mDrawingView = new ParticleField(mParentView.getContext());
mParentView.addView(mDrawingView);
mDrawingView.setParticles (mActiveParticles);
mDrawingView.setParticles(mActiveParticles);
// We start a property animator that will call us to do the update
// Animate from 0 to timeToLiveMax
startAnimator(interpolator, mTimeToLive);
Expand All @@ -438,7 +438,7 @@ public void oneShot(View emiter, int numParticles, Interpolator interpolator) {
private void startAnimator(Interpolator interpolator, long animnationTime) {
mAnimator = ValueAnimator.ofInt(0, (int) animnationTime);
mAnimator.setDuration(animnationTime);
mAnimator.addUpdateListener(new AnimatorUpdateListener() {
mAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int miliseconds = (Integer) animation.getAnimatedValue();
Expand Down Expand Up @@ -543,12 +543,14 @@ private void onUpdate(long miliseconds) {
// Activate a new particle
activateParticle(miliseconds);
}
for (int i=0; i<mActiveParticles.size(); i++) {
boolean active = mActiveParticles.get(i).update(miliseconds);
if (!active) {
Particle p = mActiveParticles.remove(i);
i--; // Needed to keep the index at the right position
mParticles.add(p);
synchronized(mActiveParticles) {
for (int i = 0; i < mActiveParticles.size(); i++) {
boolean active = mActiveParticles.get(i).update(miliseconds);
if (!active) {
Particle p = mActiveParticles.remove(i);
i--; // Needed to keep the index at the right position
mParticles.add(p);
}
}
}
mDrawingView.postInvalidate();
Expand Down

0 comments on commit a163b65

Please sign in to comment.