代码之家  ›  专栏  ›  技术社区  ›  Aman Kimothi

处理程序在Android中的函数中工作不正常

  •  0
  • Aman Kimothi  · 技术社区  · 8 年前

    下面是我编写的代码,用于将某些视图的背景颜色亮度从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
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   David Rawson B Aristide    8 年前

    您在logcat中得到的结果与预期一样。让我们看看你写的:

    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 在进入下一个迭代之前完成任务。

    您现在的任务是重构代码,以便获得所需的效果。您可能需要使用 处理者 ,而不是将大量任务同步转储到。