如何暂停和停止变化的ImageView

问题描述:

我有其中的每一个画面切换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

我使用的是处理器,可运行,并 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

抱歉回答我自己的问题,但评论部分太小。

我这样做(每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; 

      } 
     }); 

再次暂停工作,但简历没有。而且我已经通过使文本,图像和延迟时间在所有步骤中保持一致来简化了我的问题。除了步骤数之外,我希望能够使这些变量变化。