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

The Handler is not working properly in a function in Android

以下是我编写的代码,用于将某些视图的背景色亮度从 128 增加到 255,反之亦然。不幸的是,应该让它等待的处理程序没有正常运行。请帮助我使用这段代码。

有一个包含 9 个视图的 3x3 矩阵。我正在随机更改任何一个单元格的不透明度。

LEVEL :我要逐个更改的单元格数。此处,级别: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

如您所见,"Inside handler" 在运行 3 次的循环结束时打印。我期待 "Inside handler" 以下列方式在 "Pattern incoming" 之后和 "Outside Handler" 之前执行:

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 中获得的结果符合预期。让我们看看您写的内容:

(假设每条语句耗时 1ms)

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 中的代码进行某种迭代,而不是将大量任务同步转储到