Skip to content

Instantly share code, notes, and snippets.

@neworld
Created January 17, 2017 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neworld/0f34a5bd3f867fa0d189f7621b40a915 to your computer and use it in GitHub Desktop.
Save neworld/0f34a5bd3f867fa0d189f7621b40a915 to your computer and use it in GitHub Desktop.
TimedDynamicText
public class TimedDynamicText extends SpannableStringBuilder {
private final ArrayDeque<Pair<Integer, CharSequence>> list;
private Handler handler = new Handler();
private CharSequence nextText = "";
public TimedDynamicText(CharSequence initialText, Collection<Pair<Integer, CharSequence>> list) {
this.list = new ArrayDeque<>(list);
nextText = initialText;
next();
}
private void next() {
if (length() == 0) {
append(nextText);
} else {
replace(0, length(), nextText);
}
Pair<Integer, CharSequence> item = list.pollFirst();
if (item != null) {
nextText = item.second;
handler.postDelayed(next, item.first);
} else {
handler = null;
}
}
private final Runnable next = new Runnable() {
@Override
public void run() {TimedDynamicText.this.next();}
};
public static class Builder {
private ArrayList<Pair<Integer, CharSequence>> list = new ArrayList<>();
private CharSequence initialText;
public Builder(CharSequence initialText) {
this.initialText = initialText;
}
public Builder addTimedText(int time, CharSequence text) {
list.add(new Pair<>(time, text));
return this;
}
public TimedDynamicText build() {
return new TimedDynamicText(initialText, list);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment