2013-10-20 4 views
0

텍스트를 읽는 응용 프로그램을 작성하고 줄을 읽을 때마다 각 단어를 연속적으로 강조 표시합니다. 아이디어는 라인을 재생하는 것입니다 (아이들을위한 그림책이므로 한 번에 한 줄 씩). 텍스트를 읽습니다. 각 단어의 길이는 밀리 초 단위로 읽은 다음 적절한시기에 단어를 텍스트보기로 강조 표시합니다.연속 지연을위한 안드로이드 처리기

나의 접근 방식은 다음과 같습니다. 문장의 단어를 배열에 넣으십시오. (그리고 결국에는 각 작업의 길이도 있지만, 각각 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; 
} 
} 

과 로그 캣 출력 :

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 루프에 문제가 있습니다. 실행 파일을 게시하면 현재 시점에서 앞으로 2000ms를 실행하도록 항상 게시됩니다. 코드가 실행되면 거의 동시에 이러한 작업이 게시됩니다. 따라서 출력은 2 초 후에 발생하며 동시에 발생합니다. 대신, 아래에서 내가하는 일을 할 수 있습니다. 여기서 나는 여러분이 작업하고있는 단어에 기초하여 앞으로 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)); 
} 

귀하의 구현에 따라 이전에 완료 한 각 실행 파일을 게시하는 것이 좋습니다. 그렇지 않으면, 많은 실행 파일을 생성하고 게시하고, 불필요하게 메모리 사용량을 줄이며, 처리기를 정리하는 데 어려움을 겪을 수 있습니다. 초기 POC의 경우이 값은 나쁘지 않으며 나중에 쉽게 변경할 수 있습니다.

+0

정말 고마워요. 근데 자네들이 자네를 청소해야한다고 제안하는거야? 나는 그것이 좋은 습관이 될 것임을 알 수있다. 나는 그것을 공부해야 할 것이다. – dulciepercy

+0

정확하게 기억이 안나지만, 예를 들어 활동이 멈춘다면 당신의 주자가 자동으로 정리되는 것을 믿지 않습니다. 내가 당신의 앱을 사용하고 있었다면, 나는 당신의 앱을 떠나면 짜증이 났을 것이고, 당신의 책은 백그라운드에서 계속 읽혀질 것입니다! 확인을 위해 문서를 확인하는 것이 가장 좋습니다. – MJD