Android - 在两次暂停之间更改后台资源两次

时间:2014-06-09 18:32:23

标签: java android android-resources

我试图将按钮设置为绿色1秒,然后再设置为红色。但它不再变为绿色,如果我注释掉“变红”部分,它将变为绿色。我使用了Log.d,它表明从“更改为绿色”更改为“更改为红色”之间存在第二个区别,因此您应该在红色之前看到绿色,但由于某种原因,这不起作用。

任何想法?

public void level1() throws InterruptedException {
    int Low = 1000;
    int High = 3000;
    int t = r.nextInt(High-Low) + Low;
    Thread.sleep(t);
    handleTime.post(new Runnable() {
        @Override
        public void run() {
            int i = r.nextInt(5);
            switch(i) {
            case 1:
                try {
                    setGreen(tLeft);
                    tLActive = true;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    tLActive = false;
                    setRed(tLeft);
                }
                break;
            case 2:
                try {
                    setGreen(tRight);
                    tRActive = true;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    tRActive = false;
                    setRed(tRight);
                }
                break;
            case 3:
                try {
                    setGreen(center);
                    cActive = true;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    cActive = false;
                    setRed(center);
                }
                break;
            case 4:
                try {
                    setGreen(bLeft);
                    bLActive = true;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    bLActive = false;
                    setRed(bLeft);
                }
                break;
            case 5:
                try {
                    setGreen(bRight);
                    bRActive = true;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    bRActive = false;
                    setRed(bRight);
                }
                break;
            }
        }
    });
}

private void setGreen(ImageButton b) {
    b.setBackgroundResource(R.drawable.green);
    Log.d("green", "green");
}

private void setRed(ImageButton b) {
    b.setBackgroundResource(R.drawable.red);
    Log.d("red", "red");
}

2 个答案:

答案 0 :(得分:1)

您可以使用Handler.class

简单的例子:

setGreenColor();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
   setRedColor();
  }
}, 1000);

其中postDelayed将在UI线程中调用。

答案 1 :(得分:0)

在这种情况下,Runnable在UI线程上运行。负责绘图的同一个线程。但是绘图不是立即的,而是UI元素被无效,因为它想要重绘,当UI线程有时间时它会预先形成重绘。

{ //the essence of the runnable code
  setGreen(bRight); //UI element is invalidated, it wants to be redrawn green
  Thread.sleep(1000); //UI thread is tied up here (blocked) so nothing can happen on UI
  setRed(bRight); //UI element is invalidated again, it wants to be redrawn red now
                  // replacing the green before it's even been seen
} //end of runnable code
//now the redrawing occurs, and it will only be red.

正如Eldar Mensutov指出的一个解决方案是延迟发布另一个可运行的。这样,Thread.sleep就不会阻止UI线程。