2010-03-18 32 views
0

我有其中的每一個畫面切換5秒一個的ImageView,我試圖添加一個暫停和如何暫停和停止變化的ImageView

恢復按鈕,可以停止和重新啓動的操作。我正在使用處理程序,Runnable和

postDelay()進行圖像切換,並將代碼放在onResume上。我正在考慮使用

等待並通知暫停和恢復,但這意味着要創建一個額外的線程。所以

遠的線頭,我有這樣的:

類RecipeDisplayThread擴展Thread { 布爾PLEASEWAIT = FALSE;

// This method is called when the thread runs 
    public void run() { 
     while (true) { 
      // Do work 

      // Check if should wait 
      synchronized (this) { 
       while (pleaseWait) { 
        try { 
         wait(); 
        } catch (Exception e) { 
        } 
       } 
      } 

      // Do work 
     } 
    } 

} 

,並在主要活動的onCreate():

 Button pauseButton = (Button) findViewById(R.id.pause); 

     pauseButton.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 
       while (true) { 

       synchronized (thread) 
       { 
        thread.pleaseWait = true; 
        } 
       } 

      } 
     }); 

     Button resumeButton = (Button) findViewById(R.id.resume); 

     resumeButton.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 
       while (true) { 

       // Resume the thread 
       synchronized (thread) 
       { 
        thread.pleaseWait = false; 
        thread.notify(); 
       } 
       } 

      } 
     }); 

暫停按鈕似乎工作,但後來經過我不能按其他任何按鈕,如

的恢復按鈕。

謝謝。

+0

請選擇比「暫停」和「恢復」更有用的標籤。 – skaffman 2010-03-18 22:02:36

回答

2

我使用的是處理器,可運行,並 postDelay()進行圖像切換

爲什麼?爲什麼不使用postDelayed()並擺脫ThreadHandler

void doTheImageUpdate() { 
    if (areWeStillRunning) { 
     myImageView.setImageResource(R.drawable.whatever); 
     myImageView.postDelayed(updater, 5000); 
    } 
    } 

    Runnable updater=new Runnable() { 
    public void run() { 
     doTheImageUpdate(); 
    } 
    }; 

當你要開始更新,設置areWeStillRunningtrue並調用doTheImageUpdate()。當您想停止更新時,請將areWeStillRunning設置爲false。你需要弄清楚用戶在5秒鐘內按下暫停和恢復的邊界情況,以防止出現翻倍現象,但我把這留給讀者作爲練習。

如果你真的想使用後臺線程,你會想了解更多關於如何使用後臺線程。例如,沒有任何形式退出的while(true) {}將永遠不會工作。有關這方面的一些好書,如this one

+0

它現在可以工作,我只需要制定邊緣案例。謝謝你的幫助。 – user270811 2010-03-23 09:02:06

0

抱歉回答我自己的問題,但評論部分太小。

我這樣做(每CommonsWare):

private Runnable mWaitRunnable = new Runnable() { 
    public void run() 
    { 
     doTheImageUpdate(); 
    } 
}; 

private void doTheImageUpdate() 
{ 
    try 
    { 
     if(bStillRunning) 
     { 

      sText = "Step one: unpack egg"; 
      iDrawable = R.drawable.monster2; 

      t.setText(sText); 
      mImage = BitmapFactory.decodeResource(getResources(), iDrawable); 

      imageView.setImageBitmap(mImage); 

      tts1 = new TTS(getApplicationContext(), ttsInitListener, true);    

      mHandler.postDelayed(mWaitRunnable, 5000); 
     } 
    } 
    catch (Exception e) 
    { 
     Log.e("mWaitRunnable.run()", e.getMessage()); 
    } 

} 



     Button pauseButton = (Button) findViewById(R.id.pause); 

     pauseButton.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 

       bStillRunning = false; 

      } 
     }); 

     Button resumeButton = (Button) findViewById(R.id.resume); 

     resumeButton.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 

       bStillRunning = true; 

      } 
     }); 

再次暫停工作,但簡歷沒有。而且我已經通過使文本,圖像和延遲時間在所有步驟中保持一致來簡化了我的問題。除了步驟數之外,我希望能夠使這些變量變化。