2016-11-05 112 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

是的。绝对。 –