我有其中的每一個畫面切換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();
}
}
}
});
暫停按鈕似乎工作,但後來經過我不能按其他任何按鈕,如
的恢復按鈕。
謝謝。
請選擇比「暫停」和「恢復」更有用的標籤。 – skaffman 2010-03-18 22:02:36