Skip to content

Remove updateListener when the view detached/GONE #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions library/src/main/java/com/tuyenmonkey/mkloader/MKLoader.java
Original file line number Diff line number Diff line change
@@ -4,7 +4,11 @@
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.tuyenmonkey.mkloader.callback.InvalidateListener;
import com.tuyenmonkey.mkloader.type.LoaderView;
@@ -69,13 +73,25 @@ private void initialize(Context context, AttributeSet attrs, int defStyleAttr) {
super.onAttachedToWindow();
if (loaderView != null && loaderView.isDetached()) {
loaderView.setInvalidateListener(this);

loaderView.setUpAnimation();
}
}

@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (loaderView != null) {
loaderView.onDetach();
loaderView.removeUpdateListener();
}
}

@Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if(visibility == GONE) {
loaderView.removeUpdateListener();
}
}

}
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ public class Balloon extends LoaderView {

}


@Override public void draw(Canvas canvas) {

}
Original file line number Diff line number Diff line change
@@ -2,8 +2,12 @@

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.util.Log;

import com.tuyenmonkey.mkloader.model.Circle;

import java.util.ArrayList;

/**
* Created by Tuyen Nguyen on 2/10/17.
*/
@@ -12,6 +16,7 @@ public class ClassicSpinner extends LoaderView {
private Circle[] circles;
private int circlesSize;


public ClassicSpinner() {
circlesSize = 8;
}
@@ -31,6 +36,9 @@ public ClassicSpinner() {
}

@Override public void setUpAnimation() {
if(circles == null) return;
removeUpdateListener();

for (int i = 0; i < circlesSize; i++) {
final int index = i;

@@ -48,9 +56,11 @@ public ClassicSpinner() {
});

fadeAnimator.start();
valueAnimators.add(fadeAnimator);
}
}


@Override public void draw(Canvas canvas) {
for (int i = 0; i < circlesSize; i++) {
canvas.save();
@@ -59,4 +69,5 @@ public ClassicSpinner() {
canvas.restore();
}
}

}
Original file line number Diff line number Diff line change
@@ -32,6 +32,9 @@ public FishSpinner() {
}

@Override public void setUpAnimation() {
if(circles == null) return;
removeUpdateListener();

for (int i = 0; i < numberOfCircle; i++) {
final int index = i;

@@ -49,6 +52,7 @@ public FishSpinner() {
});

fadeAnimator.start();
valueAnimators.add(fadeAnimator);
}
}

@@ -60,4 +64,6 @@ public FishSpinner() {
canvas.restore();
}
}


}
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ public LineSpinner() {
}

@Override public void setUpAnimation() {
if(lines == null) return;
removeUpdateListener();
for (int i = 0; i < numberOfLine; i++) {
final int index = i;

@@ -50,6 +52,7 @@ public LineSpinner() {
});

fadeAnimator.start();
valueAnimators.add(fadeAnimator);
}
}

@@ -61,4 +64,6 @@ public LineSpinner() {
canvas.restore();
}
}


}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.tuyenmonkey.mkloader.type;

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.PointF;
import com.tuyenmonkey.mkloader.callback.InvalidateListener;

import java.util.ArrayList;

/**
* Created by Tuyen Nguyen on 2/11/17.
*/
@@ -15,6 +18,8 @@ public abstract class LoaderView {
protected PointF center;
protected InvalidateListener invalidateListener;

protected ArrayList<ValueAnimator> valueAnimators = new ArrayList<ValueAnimator>();

public LoaderView() {
this.desiredWidth = 150;
this.desiredHeight = 150;
@@ -57,4 +62,14 @@ public void onDetach() {
invalidateListener = null;
}
}

public void removeUpdateListener() {
for(ValueAnimator va: valueAnimators) {
va.setRepeatCount(0);
va.end();
va.cancel();
va.removeAllUpdateListeners();
}
}

}
Original file line number Diff line number Diff line change
@@ -36,6 +36,8 @@ public PhoneWave() {
}

@Override public void setUpAnimation() {
if(arcs == null) return;
removeUpdateListener();
for (int i = 0; i < numberOfArc; i++) {
final int index = i;

@@ -53,6 +55,7 @@ public PhoneWave() {
});

fadeAnimator.start();
valueAnimators.add(fadeAnimator);
}
}

@@ -61,4 +64,6 @@ public PhoneWave() {
arcs[i].draw(canvas);
}
}


}
Original file line number Diff line number Diff line change
@@ -41,6 +41,8 @@ public Pulse(int numberOfLines) throws InvalidNumberOfPulseException {
}

@Override public void setUpAnimation() {
if(lines == null) return;
removeUpdateListener();
for (int i = 0; i < numberOfLines; i++) {
final int index = i;
ValueAnimator scaleAnimator = ValueAnimator.ofFloat(1f, 1.5f, 1f);
@@ -57,6 +59,7 @@ public Pulse(int numberOfLines) throws InvalidNumberOfPulseException {
});

scaleAnimator.start();
valueAnimators.add(scaleAnimator);
}
}

@@ -69,4 +72,5 @@ public Pulse(int numberOfLines) throws InvalidNumberOfPulseException {
canvas.restore();
}
}

}
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ public class Radar extends LoaderView {
}

@Override public void setUpAnimation() {
if(line == null) return;
removeUpdateListener();
ValueAnimator animator = ValueAnimator.ofFloat(0, 359);
animator.setDuration(1000);
animator.setRepeatCount(ValueAnimator.INFINITE);
@@ -36,6 +38,7 @@ public class Radar extends LoaderView {
});

animator.start();
valueAnimators.add(animator);
}

@Override public void draw(Canvas canvas) {
@@ -44,4 +47,6 @@ public class Radar extends LoaderView {
line.draw(canvas);
canvas.restore();
}


}
Original file line number Diff line number Diff line change
@@ -50,6 +50,8 @@ public Sharingan() {
}

@Override public void setUpAnimation() {
if(sharingans == null) return;
removeUpdateListener();
ValueAnimator rotateAnimator = ValueAnimator.ofFloat(0, 360);
rotateAnimator.setDuration(1500);
rotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
@@ -77,6 +79,9 @@ public Sharingan() {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(rotateAnimator).with(scaleAnimator);
animatorSet.start();

valueAnimators.add(rotateAnimator);
valueAnimators.add(scaleAnimator);
}

@Override public void draw(Canvas canvas) {
@@ -93,4 +98,6 @@ public Sharingan() {
}
canvas.restore();
}


}
Original file line number Diff line number Diff line change
@@ -39,6 +39,8 @@ public TwinFishesSpinner() {
}

@Override public void setUpAnimation() {
if(circles == null) return;
removeUpdateListener();
for (int i = 0; i < numberOfCircle; i++) {
final int index = i;

@@ -56,6 +58,7 @@ public TwinFishesSpinner() {
});

fadeAnimator.start();
valueAnimators.add(fadeAnimator);
}
}

@@ -67,4 +70,6 @@ public TwinFishesSpinner() {
canvas.restore();
}
}


}
Original file line number Diff line number Diff line change
@@ -37,6 +37,8 @@ public Whirlpool() {
}

@Override public void setUpAnimation() {
if(arcs == null) return;
removeUpdateListener();
for (int i = numberOfArc - 1; i >= 0; i--) {
final int index = i;

@@ -54,6 +56,7 @@ public Whirlpool() {
});

fadeAnimator.start();
valueAnimators.add(fadeAnimator);
}
}

@@ -65,4 +68,6 @@ public Whirlpool() {
canvas.restore();
}
}


}
4 changes: 4 additions & 0 deletions library/src/main/java/com/tuyenmonkey/mkloader/type/Worm.java
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ public Worm() {
}

@Override public void setUpAnimation() {
if(circles == null) return;
removeUpdateListener();
for (int i = 0; i < circlesSize; i++) {
final int index = i;
ValueAnimator translateAnimator = ValueAnimator.ofFloat(center.y, height / 4f, height * 3 / 4f, center.y);
@@ -48,6 +50,7 @@ public Worm() {
});

translateAnimator.start();
valueAnimators.add(translateAnimator);
}
}

@@ -59,4 +62,5 @@ public Worm() {
canvas.restore();
}
}

}