在后台Android中执行繁重处理时UI冻结

在后台Android中执行繁重处理时UI冻结,android,Android,在UI线程上: AsyncCaller asyncCaller = new AsyncCaller(); asyncCaller.doInBackground(); 在异步任务中: @凌驾 受保护的Void doInBackground(Void…参数){ Mat orignal=new Mat(image1.getHeight()、image1.getWidth()、CvType.CV_8UC3); Mat v_blur=new M

在UI线程上:

   AsyncCaller asyncCaller = new AsyncCaller();
                           asyncCaller.doInBackground();
在异步任务中: @凌驾 受保护的Void doInBackground(Void…参数){

Mat orignal=new Mat(image1.getHeight()、image1.getWidth()、CvType.CV_8UC3);
Mat v_blur=new Mat(image1.getHeight()、image1.getWidth()、CvType.CV_8UC3);
Utils.bitmapToMat(图像1,原始);
高斯模糊(原始,v_模糊,新尺寸(3,3),0);
//模糊完成
Mat to_zero=新的Mat(image1.getHeight()、image1.getWidth()、CvType.CV_8UC4);
Imgproc.threshold(v_blur,to_zero,100100,Imgproc.THRESH_to zero);
位图bmp=image1;
日志e(“背景”、“背景中”);
Utils.bitmapToMat(bmp,到零);
对于(int j=0;j10&&蓝色>10&&绿色>10){
int led_灯泡=检查是否存在读数(j,i);
如果(led灯灯泡==-1){
int start_x=getMinValue(j);
int start_y=getMinValue(i);
int end_x=getMaxValue(j,bmp.getWidth());
int end_y=getMaxValue(i,bmp.getHeight());
Log.e(“裁剪值”,起始值x+“”+结束值x+“”+起始值y+“”+结束值y);
添加(新的LED边框(LED++,end_x,start_x,end_y,start_y,0,0.0));
}否则{
如果(j>led.get(led灯泡)。内部最大){
led.get(led灯泡)。内部x最大=j;
}
如果(jled.get(led\u灯泡).inner\u y\u max){
led.get(led灯泡)。内部y最大=i;
}
如果(i
我正在使用android OpenCV库进行图像处理。首先,我在UI线程上做了所有这些工作,但面临UI挂起的问题。所以我把它换成了背景线。但我仍然面临着一个悬而未决的问题。有人能解释为什么会发生这种情况以及任何可能的解决办法吗?
感谢您在工作中使用Java线程而不是异步任务! 异步任务仅适用于小型操作

请检查这里:

在工作中使用Java线程而不是异步任务! 异步任务仅适用于小型操作

请检查这里:

使用asyncCaller.execute():,不要直接调用doInBackground()

现在,您正在UI线程上调用此方法,而不是启动AsyncTask并让任务自己调用线程

AsyncTask任务=新建AsyncTask(); task.execute(paramsForDoInBackground)

应该如何调用它使用asyncCaller.execute():,不要直接调用doInBackground()

现在,您正在UI线程上调用此方法,而不是启动AsyncTask并让任务自己调用线程

AsyncTask任务=新建AsyncTask(); task.execute(paramsForDoInBackground)


这是一个最简单、最好、直观的例子,如果你不想冻结用户界面,它应该是什么样的。每个面临此问题的人或任何不知道如何为android项目编码的人都应该看到并遵循以下风格:


如果您不想冻结UI,这是一个最简单、最好、直观的示例,演示了它应该是什么样的。每个面临此问题的人或任何不知道如何为android项目编码的人都应该看到并遵循以下风格:


感谢您的回复。让我试试这个线程方法,很快就会通知你。我尝试了这个方法,但在访问UI元素时出错。这可能是一个很好的方法,但在我的例子中,需要在前面的代码中进行许多更改。我试过布兰登的答案,但没有做任何改变。顺便说一句,感谢您的回复。您可以通过处理程序和lopper来访问UI元素。因为你不能在主线程外更改UI元素。谢谢你的回复。让我试试这个线程方法,很快就会通知你。我尝试了这个方法,但在访问UI元素时出错。这可能是一个很好的方法,但在我的例子中,需要在前面的代码中进行许多更改。我试过布兰登的答案,但没有做任何改变。顺便说一句,感谢您的回复。您可以通过处理程序和lopper来访问UI元素。因为你不能在主线程外更改UI元素。非常感谢兄弟。。工作起来很有魅力:)。非常感谢兄弟。。工作方式类似于魅力:)。
       Mat orignal = new Mat(image1.getHeight(), image1.getWidth(), CvType.CV_8UC3);
       Mat v_blur = new Mat(image1.getHeight(), image1.getWidth(), CvType.CV_8UC3);
       Utils.bitmapToMat(image1, orignal);
       Imgproc.GaussianBlur(orignal, v_blur, new Size(3, 3), 0);
       // blur completed
       Mat to_zero = new Mat(image1.getHeight(), image1.getWidth(), CvType.CV_8UC4);
       Imgproc.threshold(v_blur, to_zero, 100, 100, Imgproc.THRESH_TOZERO);
       Bitmap bmp = image1;
       Log.e("background", "in bg");
       Utils.bitmapToMat(bmp, to_zero);
       for (int j = 0; j < bmp.getWidth(); j++) {
           for (int i = 0; i < bmp.getHeight(); i++) {
               int pixelValue = bmp.getPixel(j, i);
               int red = Color.red(pixelValue);
               int green = Color.green(pixelValue);
               int blue = Color.blue(pixelValue);

               if (red > 10 && blue > 10 && green > 10) {
                   int led_bulb = checkForAlreadyExistance(j, i);
                   if (led_bulb == -1) {
                       int start_x = getMinValue(j);
                       int start_y = getMinValue(i);
                       int end_x = getMaxValue(j, bmp.getWidth());
                       int end_y = getMaxValue(i, bmp.getHeight());
                       Log.e("crop_values", start_x + " " + end_x + " " + start_y + " " + end_y);
                       LEDs.add(new LedBoundingBox(LED++, end_x, start_x, end_y, start_y, 0, 0, 0.0));
                   } else {
                       if (j > LEDs.get(led_bulb).inner_x_max) {
                           LEDs.get(led_bulb).inner_x_max = j;
                       }
                       if (j < LEDs.get(led_bulb).inner_x_min) {
                           LEDs.get(led_bulb).inner_x_min = j;
                       }
                       if (i > LEDs.get(led_bulb).inner_y_max) {
                           LEDs.get(led_bulb).inner_y_max = i;
                       }
                       if (i < LEDs.get(led_bulb).inner_y_min) {
                           LEDs.get(led_bulb).inner_y_min = i;
                       }

                   }

               }
           }
       }
       Log.e("bgComplete", "complete");
       return null;
   }