我想在展示新背景图像之前清除当前背景图像。我也尝试过setBackgroundResource(0)和setBackgroundColor(Color.TRANSPARENT),但它不起作用。
爪哇:
public void decideclick(){
decidebutton = (Button) findViewById(R.id.decideimg);
resultview = (ImageView) findViewById(R.id.imageView);
resultview2 = (ImageView) findViewById(R.id.imageView2);
decidebutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
resultview.setBackgroundResource(0);
resultview2.setBackgroundColor(Color.TRANSPARENT);
SystemClock.sleep(2000);
if(player_choose == 5)
resultview.setBackgroundResource(R.drawable.abc);
else if(player_choose == 2)
resultview.setBackgroundResource(R.drawable.edf);
else if(player_choose == 0)
resultview.setBackgroundResource(R.drawable.ghi);
}
});
}
结果是当前的imageView和imageView2没有消失,新图像在2秒后出现。为什么setBackgroundResource(0)没有工作?
答案 0 :(得分:1)
这很可能是因为您在有机会重绘该视图之前正在休眠该线程。在完成此特定消息的处理之前,您将无法看到更改,从而使UI有机会重绘。
试试这个:
resultview.setBackgroundResource(0);
new Handler().postDelayed(new Runnable() {
public void run() {
if(player_choose == 5)
resultview.setBackgroundResource(R.drawable.abc);
else if(player_choose == 2)
resultview.setBackgroundResource(R.drawable.edf);
else if(player_choose == 0)
resultview.setBackgroundResource(R.drawable.ghi);
}
}, 2000);