From 7010db55f6cdd49177008d9d10eae1c2b5317c9a Mon Sep 17 00:00:00 2001 From: Julio Yaguez Date: Mon, 9 May 2016 13:07:48 +0200 Subject: [PATCH] - extracted TypeEvaluator anonymous class to an actual class and same for the update listeners --- .../java/pl/tajchert/sample/DotsTextView.java | 37 ++++--------------- .../sample/InvalidateViewOnUpdate.java | 24 ++++++++++++ .../pl/tajchert/sample/SinTypeEvaluator.java | 10 +++++ 3 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 lib/src/main/java/pl/tajchert/sample/InvalidateViewOnUpdate.java create mode 100644 lib/src/main/java/pl/tajchert/sample/SinTypeEvaluator.java diff --git a/lib/src/main/java/pl/tajchert/sample/DotsTextView.java b/lib/src/main/java/pl/tajchert/sample/DotsTextView.java index 074de59..f665b42 100644 --- a/lib/src/main/java/pl/tajchert/sample/DotsTextView.java +++ b/lib/src/main/java/pl/tajchert/sample/DotsTextView.java @@ -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; @@ -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 { @@ -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(); } @@ -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)); @@ -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() { - @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); @@ -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; @@ -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; diff --git a/lib/src/main/java/pl/tajchert/sample/InvalidateViewOnUpdate.java b/lib/src/main/java/pl/tajchert/sample/InvalidateViewOnUpdate.java new file mode 100644 index 0000000..288eb7f --- /dev/null +++ b/lib/src/main/java/pl/tajchert/sample/InvalidateViewOnUpdate.java @@ -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 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(); + } +} diff --git a/lib/src/main/java/pl/tajchert/sample/SinTypeEvaluator.java b/lib/src/main/java/pl/tajchert/sample/SinTypeEvaluator.java new file mode 100644 index 0000000..c11beb0 --- /dev/null +++ b/lib/src/main/java/pl/tajchert/sample/SinTypeEvaluator.java @@ -0,0 +1,10 @@ +package pl.tajchert.sample; + +import android.animation.TypeEvaluator; + +public class SinTypeEvaluator implements TypeEvaluator { + @Override + public Number evaluate(float fraction, Number from, Number to) { + return Math.max(0, Math.sin(fraction * Math.PI * 2)) * (to.floatValue() - from.floatValue()); + } +}