开发者

how to pause and stop a changing ImageView

i have an ImageView in which the picture switches every 5s, i am trying to add a pause and

resume button that can stop and restart the action. i am using a Handler, Runnable, and

postDelay() for image switch, and i put the code on onResume. i am thinking about using

wait and notify for the pause and resume, but that would mean creating an extra thread. so

far for the thread, i have this:

class RecipeDisplayThread extends Thread { 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
        }
    }

}   

and in the main activity's 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();
                }
                }

            }
        });

the pause button seems to work, but then after that i can't press any other button, such as

the resume button.

thanks.


i am using a Handler, Runnable, and postDelay() for image switch

Why? Why not use postDelayed() and get rid of the Thread and Handler?

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

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

When you want to start updating, set areWeStillRunning to true and call doTheImageUpdate(). When you want to stop updating, set areWeStillRunning to false. You'll need to work out the edge case of where the user presses pause and resume within 5 seconds, to prevent doubling things up, but I leave that as an exercise for the reader.

If you really want to use a background thread, you will want to learn more about how to use background threads. For example, while(true) {} without any form of exit will never work. There are some good books on the subject, such as this one.


sorry to answer my own question, but the comment section is too small.

i did this (per 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;

            }
        });

again pause works but resume doesn't. and i have already simplified my problem by making the text, the image, and delay time the same for all the steps. i was hoping to make these variable, in additional to the number of steps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜