Skip to content
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

Memory Leak #11

Merged
merged 1 commit into from
May 10, 2016
Merged
Show file tree
Hide file tree
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
37 changes: 7 additions & 30 deletions lib/src/main/java/pl/tajchert/sample/DotsTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
Expand All @@ -14,7 +12,6 @@
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.TextView;

import pl.tajchert.waitingdots.R;

public class DotsTextView extends TextView {
Expand Down Expand Up @@ -61,7 +58,8 @@ private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaitingDots);
period = typedArray.getInt(R.styleable.WaitingDots_period, 6000);
jumpHeight = typedArray.getInt(R.styleable.WaitingDots_jumpHeight, (int) (getTextSize() / 4));
jumpHeight = typedArray.getInt(
R.styleable.WaitingDots_jumpHeight, (int) (getTextSize() / 4));
autoPlay = typedArray.getBoolean(R.styleable.WaitingDots_autoplay, true);
typedArray.recycle();
}
Expand All @@ -78,13 +76,8 @@ private void init(Context context, AttributeSet attrs) {
textWidth = getPaint().measureText(".", 0, 1);

ObjectAnimator dotOneJumpAnimator = createDotJumpAnimator(dotOne, 0);
dotOneJumpAnimator.addUpdateListener(new AnimatorUpdateListener() {
dotOneJumpAnimator.addUpdateListener(new InvalidateViewOnUpdate(this));

@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
invalidate();
}
});
mAnimatorSet.playTogether(dotOneJumpAnimator, createDotJumpAnimator(dotTwo,
period / 6), createDotJumpAnimator(dotThree, period * 2 / 6));

Expand All @@ -102,13 +95,9 @@ public void start() {

private ObjectAnimator createDotJumpAnimator(JumpingSpan jumpingSpan, long delay) {
ObjectAnimator jumpAnimator = ObjectAnimator.ofFloat(jumpingSpan, "translationY", 0, -jumpHeight);
jumpAnimator.setEvaluator(new TypeEvaluator<Number>() {

@Override
public Number evaluate(float fraction, Number from, Number to) {
return Math.max(0, Math.sin(fraction * Math.PI * 2)) * (to.floatValue() - from.floatValue());
}
});
jumpAnimator.setEvaluator(new SinTypeEvaluator());

jumpAnimator.setDuration(period);
jumpAnimator.setStartDelay(delay);
jumpAnimator.setRepeatCount(ValueAnimator.INFINITE);
Expand All @@ -134,13 +123,7 @@ public void hide() {
createDotHideAnimator(dotThree, 2).start();

ObjectAnimator dotTwoMoveRightToLeft = createDotHideAnimator(dotTwo, 1);
dotTwoMoveRightToLeft.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
invalidate();
}
});
dotTwoMoveRightToLeft.addUpdateListener(new InvalidateViewOnUpdate(this));

dotTwoMoveRightToLeft.start();
isHide = true;
Expand All @@ -152,13 +135,7 @@ public void show() {
dotThreeMoveRightToLeft.start();

ObjectAnimator dotTwoMoveRightToLeft = createDotShowAnimator(dotTwo, 1);
dotTwoMoveRightToLeft.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
invalidate();
}
});
dotTwoMoveRightToLeft.addUpdateListener(new InvalidateViewOnUpdate(this));

dotTwoMoveRightToLeft.start();
isHide = false;
Expand Down
24 changes: 24 additions & 0 deletions lib/src/main/java/pl/tajchert/sample/InvalidateViewOnUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.tajchert.sample;

import android.animation.ValueAnimator;
import android.view.View;
import java.lang.ref.WeakReference;

public class InvalidateViewOnUpdate implements ValueAnimator.AnimatorUpdateListener {
private final WeakReference<View> viewRef;

public InvalidateViewOnUpdate(View view) {
this.viewRef = new WeakReference<>(view);
}

@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
final View view = viewRef.get();

if (view == null) {
return;
}

view.invalidate();
}
}
10 changes: 10 additions & 0 deletions lib/src/main/java/pl/tajchert/sample/SinTypeEvaluator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pl.tajchert.sample;

import android.animation.TypeEvaluator;

public class SinTypeEvaluator implements TypeEvaluator<Number> {
@Override
public Number evaluate(float fraction, Number from, Number to) {
return Math.max(0, Math.sin(fraction * Math.PI * 2)) * (to.floatValue() - from.floatValue());
}
}