以下是我爲了將某些視圖的背景色的亮度從128增加到255而編寫的代碼,反之亦然。不幸的是,應該讓它等待的處理程序運行不正常。請幫助我使用此代碼。處理程序在Android中的功能中無法正常工作
有一個3x3矩陣,其中有9個視圖。我隨機更改任何一個單元格的不透明度。
LEVEL:我想要逐個更改的單元格數量。這裏,LEVEL:3
color [9]:3x3包含9個視圖的矩陣。
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
什麼你期待在logcat中看到?你是什麼意思處理程序無法正常工作?另外,請將它作爲問題粘貼,以便我們無需在新窗口中打開即可看到它。 –
@DavidRawson。我在這裏添加了logcat語句。我希望現在這個問題很清楚。您的幫助將受到高度讚賞。謝謝。 –
謝謝!這使得它更容易回答 –