我希望TextView中的文本能够逐词或逐字地显示,就像大多数RPG和冒险游戏中的文本框一样。一个很好的例子是游戏《逆转裁判》中的文本流(http://youtu.be/2OOX2Gv0768?t=1m7s)。
到目前为止,我尝试过以下方法:
我在模拟器上测试了一下,发现性能很低,特别是在每个字母显示后加入延迟的情况下。延迟不稳定。
此外,希望有更优雅的解决方案。也许可以更灵活地处理延迟?例如,在句子后增加更长的延迟等。
非常感谢!
到目前为止,我尝试过以下方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String text = "test test test test";
String[] split = text.split(" ");
Deque<String> words = new LinkedList<String>();
for (int i = 0; i<split.length; i++)
{
words.addLast(split[i]);
}
showNextWord(words);
}
public void showNextWord(final Deque<String> words)
{
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
TextView t = (TextView) findViewById(R.id.textBox);
t.append(words.pollFirst()+" ");
if (words.size()>0)
showNextWord(words);
}
}, 500);
}
我在模拟器上测试了一下,发现性能很低,特别是在每个字母显示后加入延迟的情况下。延迟不稳定。
此外,希望有更优雅的解决方案。也许可以更灵活地处理延迟?例如,在句子后增加更长的延迟等。
非常感谢!