Skip to content

Instantly share code, notes, and snippets.

@olivierperez
Last active September 8, 2016 09:30
Show Gist options
  • Save olivierperez/132f21d74ec87f225380aefaff6c78fc to your computer and use it in GitHub Desktop.
Save olivierperez/132f21d74ec87f225380aefaff6c78fc to your computer and use it in GitHub Desktop.
TextViewAnimator animator = new TextViewAnimator();
animator.flip(myTextView, label);
// animator.fade(myTextView, label);
package fr.o80.android.animator;
import android.animation.Animator;
/**
* @author Olivier Perez
*/
abstract class BaseAnimatorListener implements Animator.AnimatorListener {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
}
package fr.o80.android.animator;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.widget.TextView;
import BaseAnimatorListener;
/**
* @author Olivier Perez
*/
public class TextViewAnimator {
private static final long ANIMATION_SHORT = 300;
public void fade(final TextView textView, final String label) {
final ObjectAnimator fadeOut = ObjectAnimator
.ofFloat(textView, "alpha", 0)
.setDuration(ANIMATION_SHORT / 2);
final ObjectAnimator fadeIn = ObjectAnimator
.ofFloat(textView, "alpha", 1)
.setDuration(ANIMATION_SHORT / 2);
fadeOut.addListener(new BaseAnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
textView.setText(label);
fadeIn.start();
}
});
fadeOut.start();
}
public void flip(final TextView textView, final String label) {
final ObjectAnimator flipOut = ObjectAnimator
.ofFloat(textView, "scaleY", 0)
.setDuration(ANIMATION_SHORT / 2);
final ObjectAnimator flipIn = ObjectAnimator
.ofFloat(textView, "scaleY", 1)
.setDuration(ANIMATION_SHORT / 2);
flipOut.addListener(new BaseAnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
textView.setText(label);
flipIn.start();
}
});
flipOut.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment