Created
January 17, 2017 12:47
-
-
Save neworld/0f34a5bd3f867fa0d189f7621b40a915 to your computer and use it in GitHub Desktop.
TimedDynamicText
This file contains hidden or 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
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