Skip to content

Instantly share code, notes, and snippets.

@sgnn7
Created January 3, 2012 20:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sgnn7/1556785 to your computer and use it in GitHub Desktop.
Save sgnn7/1556785 to your computer and use it in GitHub Desktop.
Android - Fading in/out textview
package org.sgnn7.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import org.sgnn7.R;
public class AnimatedTextView extends TextView {
private static final int IS_ANIMATING_TAG_ID = "isAnimating".hashCode();
private Animation fadeInAnimation;
private Animation fadeOutAnimation;
public AnimatedTextView(Context context) {
super(context);
initAnimations(context);
}
public AnimatedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAnimations(context);
}
public AnimatedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initAnimations(context);
}
public void initAnimations(Context context) {
fadeInAnimation = AnimationUtils.loadAnimation(this.getContext(), R.anim.fadein);
fadeOutAnimation = AnimationUtils.loadAnimation(this.getContext(), R.anim.fadeout);
fadeInAnimation.setAnimationListener(new AnimationListenerAdapter() {
@Override
public void onAnimationStart(Animation animation) {
setAnimatingFlag(true);
}
@Override
public void onAnimationEnd(Animation animation) {
setAnimatingFlag(false);
}
});
fadeOutAnimation.setAnimationListener(new AnimationListenerAdapter() {
@Override
public void onAnimationStart(Animation animation) {
setAnimatingFlag(false);
}
});
setAnimatingFlag(false);
}
public void fadeOut() {
if (getVisibility() == View.VISIBLE) {
startAnimation(fadeOutAnimation);
setVisibility(View.INVISIBLE);
}
}
public void fadeIn() {
if (getVisibility() == View.INVISIBLE && !isAnimating()) {
startAnimation(fadeInAnimation);
setVisibility(View.VISIBLE);
}
}
private boolean isAnimating() {
return (Boolean) getTag(IS_ANIMATING_TAG_ID) == true;
}
private void setAnimatingFlag(boolean isAnimating) {
setTag(IS_ANIMATING_TAG_ID, new Boolean(isAnimating));
}
}
@SagarRaiyani
Copy link

put the full code bcus the R.anim.fadein & fadeout is not available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment