Created
January 3, 2012 20:31
-
-
Save sgnn7/1556785 to your computer and use it in GitHub Desktop.
Android - Fading in/out textview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
put the full code bcus the R.anim.fadein & fadeout is not available