3

我希望 TextView 中的文本逐个单词甚至逐个字母地显示,就像在大多数 rpgs 和带有文本框的冒险中一样。文本流应该是什么样子的一个很好的例子是游戏 phoenix wright ( 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);
}

我在模拟器上进行了测试,它似乎性能低下,如果我在显示每个字母后开始延迟,性能就更差了。延迟不一致。

除此之外,我希望有一个更优雅的解决方案。也许某种方式可以更灵活地处理延迟?例如,一个句子后更大的延迟等等。

非常感谢你!

4

4 回答 4

4
public class Typewriter extends TextView {

private CharSequence mText;
private int mIndex;
private long mDelay = 500; //Default 500ms delay


public Typewriter(Context context) {
    super(context);
}

public Typewriter(Context context, AttributeSet attrs) {
    super(context, attrs);
}

private Handler mHandler = new Handler();
private Runnable characterAdder = new Runnable() {
    @Override
    public void run() {
        setText(mText.subSequence(0, mIndex++));
        if(mIndex <= mText.length()) {
            mHandler.postDelayed(characterAdder, mDelay);
        }
    }
};

public void animateText(CharSequence text) {
    mText = text;
    mIndex = 0;

    setText("");
    mHandler.removeCallbacks(characterAdder);
    mHandler.postDelayed(characterAdder, mDelay);
}

public void setCharacterDelay(long millis) {
    mDelay = millis;
}
}

并在您的活动中使用上述类,如下所示:

    Typewriter writer = new Typewriter(this);
    //Add a character every 200ms
    writer.setCharacterDelay(200);
    writer.animateText("Sample String");
于 2014-05-01T11:08:43.510 回答
1

试试这个:

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() 
{ 
    public void run() 
    { 
            if (words.size()>0)
            showNextWord(words);
    } 
}, 500);

public void showNextWord(words)
{
        TextView t = (TextView) findViewById(R.id.textBox);
        t.append(words.pollFirst()+" ");        
}
于 2014-05-01T10:02:26.523 回答
0

好吧,这就是我想出的!

public static void main(String[] args) {
    printWord("Sup dude!", 150);
}

public static void printWord(String word, int time){
    for(int i = 0; i < word.length(); i++){
        double start = System.currentTimeMillis();
        double end = System.currentTimeMillis();
        while(end - start <= time){
            end = System.currentTimeMillis();
        }
        System.out.print(word.charAt(i));
    }
}

我敢打赌,您可以通过将 syso 更改为:

tv.setText(tv.getText() + word.charAt(i));

希望能回答您的问题并有所帮助!祝你好运 :)

于 2014-05-30T07:22:15.620 回答
0

看看这个库:https ://github.com/flavienlaurent/spans它有一个称为打字机的功能,可能适合您的需求。您可以在此博客文章中看到它的实际效果:http: //flavienlaurent.com/blog/2014/01/31/spans/

于 2014-05-30T07:53:50.877 回答