Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Created September 6, 2013 10:50
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save slightfoot/6462294 to your computer and use it in GitHub Desktop.
FadeyTextView example.. will animate text in like "H, He, Hel, Hell, Hello, etc"
package com.example.fadeytextthingy;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.UpdateAppearance;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewDebug.CapturedViewProperty;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.TextView;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
int padding = (int)(16 * getResources().getDisplayMetrics().density);
final FadeyTextView textView = new FadeyTextView(this);
textView.setPadding(padding, padding, padding, padding);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22.0f);
textView.setTextColor(Color.GREEN);
textView.setTypeface(Typeface.MONOSPACE);
setContentView(textView);
textView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
textView.setText("Hello World");
}
});
}
private static class FadeyTextView extends TextView
{
private Interpolator mInterpolator;
private long mStart, mDurationPerLetter;
private boolean mAnimating = false;
private SpannableString mFadeyText;
private CharSequence mText;
public FadeyTextView(Context context)
{
super(context);
initView();
}
public FadeyTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
initView();
}
public FadeyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initView();
}
private void initView()
{
// Set defaults
mInterpolator = new DecelerateInterpolator();
mDurationPerLetter = 250;
}
public void setInterpolator(Interpolator interpolator)
{
mInterpolator = interpolator;
}
public void setDurationPerLetter(long durationPerLetter)
{
mDurationPerLetter = durationPerLetter;
}
@Override
public void setText(CharSequence text, BufferType type)
{
mText = text;
mFadeyText = new SpannableString(text);
FadeyLetterSpan[] letters = mFadeyText.getSpans(0, mFadeyText.length(), FadeyLetterSpan.class);
for(FadeyLetterSpan letter : letters){
mFadeyText.removeSpan(letter);
}
final int length = mFadeyText.length();
for(int i = 0; i < length; i++){
mFadeyText.setSpan(new FadeyLetterSpan(), i, i + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
super.setText(mFadeyText, BufferType.SPANNABLE);
mAnimating = true;
mStart = AnimationUtils.currentAnimationTimeMillis();
ViewCompat.postInvalidateOnAnimation(this);
}
@Override
@CapturedViewProperty
public CharSequence getText()
{
return mText;
}
public boolean isAnimating()
{
return mAnimating;
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if(mAnimating){
long mDelta = AnimationUtils.currentAnimationTimeMillis() - mStart;
FadeyLetterSpan[] letters = mFadeyText.getSpans(0, mFadeyText.length(), FadeyLetterSpan.class);
final int length = letters.length;
for(int i = 0; i < length; i++){
FadeyLetterSpan letter = letters[i];
float delta = (float)Math.max(Math.min((mDelta - (i * mDurationPerLetter)), mDurationPerLetter), 0);
letter.setAlpha(mInterpolator.getInterpolation(delta / (float)mDurationPerLetter));
}
if(mDelta < mDurationPerLetter * length){
ViewCompat.postInvalidateOnAnimation(this);
}else{
mAnimating = false;
}
}
}
private class FadeyLetterSpan extends CharacterStyle implements UpdateAppearance
{
private float mAlpha = 0.0f;
public void setAlpha(float alpha)
{
mAlpha = Math.max(Math.min(alpha, 1.0f), 0.0f);
}
@Override
public void updateDrawState(TextPaint tp)
{
int color = ((int)(0xFF * mAlpha) << 24) | (tp.getColor() & 0x00FFFFFF);
tp.setColor(color);
}
}
}
}
@michaelye
Copy link

Hi, first, nice work!
But something doesn't work well ,when I set
textView.setEms(1);
I want the animate text in like
"H,
e,
l,
l,
o,
etc"

but fail.

@michaelye
Copy link

Hi,I'm so sorry ,it works fine. I found that, when using textView.setEms(),the width of the textView should NOT be match_parent,thanks again!

@pkpk123
Copy link

pkpk123 commented Aug 20, 2015

Hi,
Thank you for this. I was searching for it for a long time.
A thousand thanks

@iUmarov
Copy link

iUmarov commented Jul 1, 2017

Needs to be taken care of the situation when text is null

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