0

我正在编写一个应用程序来读取一行文本,并在读取该行时连续突出显示每个单词。这个想法是开始播放这行(这是一本儿童图画书,所以一次一行),然后阅读文本,每个单词的长度(以毫秒为单位),然后在正确的时间在 textview 中突出显示该单词。

我的方法是:将句子的单词放入一个数组中(最后是每个作品的长度,但目前只假设每个 1000 毫秒);将单词写入 textViewPartial;字的延迟长度;将下一个单词添加到句子中并将其写入 textViewPartial....等。

但我无法计算出时间。阅读我在处理程序和异步上可以找到的所有内容,我能想到的最好的方法如下 - 我将一个后延迟处理程序放在一个 for 循环中。我的大脑说每次循环循环时它都会延迟,但你可以从 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

第一个问题——这首先是正确的方法吗?第二个问题 - 我怎样才能让它工作?

非常感谢。

4

1 回答 1

1

问题出在你的 for 循环中。当您发布可运行文件时,您总是从当前时间点开始在未来运行 2000 毫秒。当您的代码运行时,它几乎同时发布这些操作。因此,您会在 2 秒后看到您的输出同时发生。相反,你可以做我在下面做的事情,我会在未来发布 2000 毫秒的倍数,具体取决于你正在处理的单词。

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

至于您的实现,我建议在前一个完成时发布每个新的可运行文件。否则,您可能会创建和发布许多可运行文件,不必要地占用您的内存使用量,并使处理程序的清理变得很痛苦。对于初始 POC,这并不算太糟糕,以后可以轻松更改。

于 2013-10-20T04:15:30.097 回答