2016-11-05 114 views
0

以下是我爲了將某些視圖的背景色的亮度從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 
+0

什麼你期待在logcat中看到?你是什​​麼意思處理程序無法正常工作?另外,請將它作爲問題粘貼,以便我們無需在新窗口中打開即可看到它。 –

+0

@DavidRawson。我在這裏添加了logcat語句。我希望現在這個問題很清楚。您的幫助將受到高度讚賞。謝謝。 –

+0

謝謝!這使得它更容易回答 –

回答

0

您在logcat中所得到的結果如預期。讓我們通過你寫的:

(假設每條語句需要1毫秒)

1. first iteration of loop at time 1 ms 
2. print "i :: 0" 
3. print "random :: 1" 
4. print "Pattern incoming :: 128" 
5. post a task to the `Handler` to set the color 2 seconds from now (time 2005 ms) 
6. print "Outside handler :: 128" 
7. go on to next iteration of loop without waiting for the previous task to complete 

8. second iteration of loop at time 8 ms: 
9. print print "i :: 1" 
10. print "random :: 3" 
11. print "Pattern incoming :: 128" 
12. post a task to the `Handler` to set the color 2 seconds from now (time 2012ms) 
.... 
**loop terminates** 
.... 
(finally a long time after the loop has completed the first scheduled task will be triggered) 
.... 
2005. print "Inside handler: 128" 

循環將不會等待Handler完成任務纔去到下一個迭代。

你的任務現在是重構你的代碼,以便你可以得到你想要的效果。您可能必須使用Handler中的代碼進行某種迭代,而不是將大量任務同步轉儲。

+0

非常感謝。我剛剛意識到我的錯誤。該應用程序現在正常工作。我使用遞歸做了它。在處理程序中調用函數本身工作得很好。 –

+0

好聽!你能接受答案嗎? –

+0

是的。絕對。 –