【问题标题】:how to pause and stop a changing ImageView如何暂停和停止不断变化的 ImageView
【发布时间】:2010-03-18 21:43:53
【问题描述】:

我有一个 ImageView,其中图片每 5 秒切换一次,我正在尝试添加暂停和

恢复按钮,可以停止和重新启动操作。我正在使用 Handler、Runnable 和

postDelay() 用于图像切换,我将代码放在 onResume 上。我正在考虑使用

等待并通知暂停和恢复,但这意味着创建一个额外的线程。所以

远远的线程,我有这个:

类RecipeDisplayThread 扩展线程 { boolean 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();
                }
                }

            }
        });

暂停按钮似乎可以工作,但之后我无法按下任何其他按钮,例如

恢复按钮。

谢谢。

【问题讨论】:

  • 请选择比“暂停”和“继续”更有用的标签。

标签: android


【解决方案1】:

我正在使用 Handler、Runnable 和 postDelay() 用于图像切换

为什么?为什么不使用postDelayed() 并摆脱ThreadHandler

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

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

当您想开始更新时,将areWeStillRunning 设置为true 并调用doTheImageUpdate()。如果要停止更新,请将areWeStillRunning 设置为false。您需要计算出用户在 5 秒内按下暂停和恢复的边缘情况,以防止重复操作,但我将其作为练习留给读者。

如果您真的想使用后台线程,您需要了解更多关于如何使用后台线程的信息。例如,while(true) {} 没有任何形式的退出将永远无法工作。有一些关于这个主题的好书,例如this one

【讨论】:

  • 它现在可以工作了,我只需要解决边缘情况。感谢您的帮助。
【解决方案2】:

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

我这样做了(根据 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;

            }
        });

再次暂停有效,但恢复无效。我已经通过使所有步骤的文本、图像和延迟时间相同来简化我的问题。除了步骤数之外,我希望使这些变量成为变量。

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多