2013-10-20 92 views
0

我正在編寫一個應用程序來讀取一行文本,並在讀取行時連續突出顯示每個單詞。這個想法是開始玩線(這是一本兒童圖畫書,一次一行),然後閱讀文本,以毫秒爲單位的每個單詞的長度,然後在正確的時間突出顯示textview中的單詞。連續延遲的android處理程序

我的方法是: 將句子的單詞放入一個數組中(並且最終每個作品的長度,但是暫時假設每個都爲1000ms); 將單詞寫入textViewPartial; 單詞的延遲長度; 向句子添加下一個單詞並將其寫入textViewPartial ....等。

但我不能計算出時間。閱讀我在處理程序和異步中可以找到的所有內容,最好的我可以提出如下 - 我在for循環中放置了一個postdelayed處理程序。我的大腦說它每次循環都會延遲,但是你可以從logcat輸出中看到它不會。 for循環開始前只有一個延遲。這是我的第一篇文章,我不明白你是如何從Eclipse獲取彩色代碼的,所以我希望它看起來不錯。

public class LineOutHanler extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_line_out_hanler); 
    TextView t = new TextView(this); 
    t=(TextView) findViewById (R.id.textView10); 
    String textOut = "Oh how my spleen aches to see you again"; 
    final TextView textViewPartial = (TextView) findViewById(R.id.textView11); 
    final String[] wordsOut = textOut.split(" "); 
    final int wordsInSentence = wordsOut.length; 
    int[] wordLength = new int[wordsInSentence]; 
      for (int counter=0;counter<=wordsInSentence-1;counter++){ 
      wordLength[counter]=wordsOut[counter].length();} 
    String partialSentence =""; 
    for (int counter=0; counter<=wordsInSentence-1; counter++){ 
      String c= addWordsOut(wordsOut[counter], partialSentence); 
      textViewPartial.setText(c); 
      partialSentence = c; 
      Log.d("Word", partialSentence); 
    final String partialOut=c; 
    final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
         textViewPartial.setText(partialOut); 
      Log.d("Handler", partialOut); 
     } 
    } , 2000);} 
    } 
public String addWordsOut (String part, String upToHere) { 
upToHere=upToHere+" " + part; 
return upToHere; 
} 
} 

和logcat的輸出:

10-19 23:07:32.248: E/cutils-trace(39): Error opening trace file: No such file or directory (2) 
10-19 23:07:32.368: D/AudioSink(39): bufferCount (4) is too small and increased to 12 
10-19 23:07:32.379: D/Word(821): Oh 
10-19 23:07:32.379: D/Word(821): Oh how 
10-19 23:07:32.388: D/Word(821): Oh how my 
10-19 23:07:32.388: D/Word(821): Oh how my spleen 
10-19 23:07:32.388: D/Word(821): Oh how my spleen aches 
10-19 23:07:32.388: D/Word(821): Oh how my spleen aches to 
10-19 23:07:32.388: D/Word(821): Oh how my spleen aches to see 
10-19 23:07:32.398: D/Word(821): Oh how my spleen aches to see you 
10-19 23:07:32.398: D/Word(821): Oh how my spleen aches to see you again 
10-19 23:07:33.328: I/Choreographer(288): Skipped 30 frames! The application may be doing too much work on its main thread. 
10-19 23:07:33.368: I/ActivityManager(288): Displayed com.example.testtextout/.LineOutHanler: +1s820ms 
10-19 23:07:35.320: W/AudioFlinger(39): write blocked for 1091 msecs, 1 delayed writes, thread 0x40e0b008 
10-19 23:07:35.320: D/Handler(821): Oh 
10-19 23:07:35.329: D/Handler(821): Oh how 
10-19 23:07:35.329: D/Handler(821): Oh how my 
10-19 23:07:35.329: D/Handler(821): Oh how my spleen 
10-19 23:07:35.329: D/Handler(821): Oh how my spleen aches 
10-19 23:07:35.329: D/Handler(821): Oh how my spleen aches to 
10-19 23:07:35.339: D/Handler(821): Oh how my spleen aches to see 
10-19 23:07:35.339: D/Handler(821): Oh how my spleen aches to see you 
10-19 23:07:35.339: D/Handler(821): Oh how my spleen aches to see you again 
10-19 23:08:30.588: D/dalvikvm(396): GC_FOR_ALLOC freed 452K, 15% free 3047K/3556K, paused 40ms, total 65ms 
10-19 23:25:42.149: D/dalvikvm(288): GC_FOR_ALLOC freed 850K, 31% free 5593K/7996K, paused 99ms, total 117ms 

第一個問題 - 這是擺在首位的正確方法? 第二個問題 - 我怎樣才能使它工作?

非常感謝。

回答

0

問題出在您的for循環中。當您發佈可運行的應用程序時,您總是會從當前時間點開始後續運行2000毫秒。當您的代碼運行時,它幾乎同時發佈這些操作。因此,您看到您的輸出在兩秒鐘後發生,同時發生。相反,你可以做下面的工作,根據你正在處理的是哪個單詞,將其發佈到將來2000ms的倍數。

for (int counter=0; counter<=wordsInSentence-1; counter++){ 
     String c= addWordsOut(wordsOut[counter], partialSentence); 
     textViewPartial.setText(c); 
     partialSentence = c; 
     Log.d("Word", partialSentence); 
     final String partialOut=c; 
     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
         textViewPartial.setText(partialOut); 
         Log.d("Handler", partialOut); 
       } 
     } , 2000*(counter+1)); 
} 

至於你的實現,我建議發佈每個新的runnable,因爲前一個完成。否則,您可以創建併發布許多可運行的代碼,不必要地佔用您的內存使用量,並且對處理程序進行清理是一件痛苦的事情。對於最初的POC來說,這並不算太壞,以後可以輕鬆更改。

+0

謝謝你的工作。但是你是否暗示我需要自己清理runnables?我可以看到這將是一個好習慣。我將不得不研究這一點。 – dulciepercy

+0

我不太清楚,但我不相信如果你的Activity停止,你的runnables會自動清理。如果我使用你的應用程序,如果離開你的應用程序,我會很生氣,而你的書一直在後臺閱讀!最好檢查文檔以進行驗證。 – MJD