2014-05-01 58 views
3

我希望TextView中的文本能夠在字母之後或字母之後顯示,就像大多數rpgs和帶有文本框的冒險一樣。一個很好的例子文本流應該是什麼樣子就像是遊戲鳳凰賴特(http://youtu.be/2OOX2Gv0768?t=1m7s如何在TextView中延遲顯示文本(word for word)

我曾嘗試到現在爲止是這樣的:

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); 
} 

我在模擬器上測試,這似乎是低性能,如果我在顯示每個字母后開始延遲,則更是如此。延遲不一致。

除此之外,我希望有一個更優雅的解決方案。也許有些方法可以更靈活地延遲?例如。在一個句子之後更大的延遲等等。

非常感謝!

+1

你的方法是好的,儘量降低延遲一點呃你的口味。仿真器不會給你真實的性能畫面。嘗試一個真正的設備。 – Nazgul

+0

不要相信模擬器,你的代碼看起來很好 – user3455363

+0

備註:我發現使用Genymotion和VirtualBox要優於默認模擬器。如果您沒有真正的設備在手邊測試,也許可以試試看。即使只是加速啓動時間,也是值得的。 – indivisible

回答

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"); 
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()+" ");   
} 
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到打印您的TextView:

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

希望這能回答你的問題,並幫助!祝你好運:)

+0

像這樣忙着等待不是一個好主意。 – Ridcully

相關問題