下面是我编写的代码,用于将某些视图的背景颜色亮度从128增加到255,反之亦然。不幸的是,应该让它等待的处理程序没有正常运行。请帮助我这个代码。
有一个3x3矩阵,其中有9个视图。我随机改变任何一个细胞的不透明度。
级别:我要逐个更改的单元格数。这里,级别:3
颜色[9]:包含9个视图的3x3矩阵。
public void pattern() {
for(int i=0;i<LEVEL;i++) {
int rand= 0 + (int)(Math.random() * 8);
computer+=rand;
Log.d(" i :" , ""+i);
Log.d(" random :" , ""+rand);
Log.d("Pattern incoming " , ""+color[rand].getBackground().getAlpha());
color[rand].getBackground().setAlpha(128);
final int random=rand;
handler.postDelayed(new Runnable() {
@Override
public void run() {
color[random].getBackground().setAlpha(128);
Log.d("Inside handler " , ""+color[random].getBackground().getAlpha());
color[random].getBackground().setAlpha(255);
}
},2000);
color[rand].getBackground().setAlpha(128);
Log.d("Outside handler " , ""+color[rand].getBackground().getAlpha());
}
}
Android监视器Logcat
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 0
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 1
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 1
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 3
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 2
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 7
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 7
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128
如您所见,“内部处理程序”在循环结束时打印,循环运行了3次。我希望“内部处理程序”在“模式传入”之后和“外部处理程序”之前以以下方式执行:
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 0
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 1
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 1
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 3
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/ i :: 2
11-06 04:21:27.267 30640-30640/com.example.aman D/ random :: 7
11-06 04:21:27.267 30640-30640/com.example.aman D/Pattern incoming: 128
$$ - 11-06 04:21:27.267 30640-30640/com.example.aman D/Inside handler: 128
11-06 04:21:27.267 30640-30640/com.example.aman D/Outside handler: 128