对话框在异步任务 android 上冻结

发布于 2024-12-20 17:05:22 字数 1487 浏览 1 评论 0原文

在我的活动中,我下载了几张图像,然后通过将每一张图像添加到我的主布局上来显示它们。我尝试了这 3 种方法,但每次我的进度对话框都会冻结,直到加载图像:

第一种方法:使用异步任务:我在 doInBackground() 上下载图像并将它们添加到 onPostExecute () 的主布局中

第二种:与上面相同,但我将每个图像添加到 onProgressUpdate()

第三:使用处理程序和线程

每次进度对话框都会冻结几秒钟(实际上整个应用程序都会冻结),直到图像被下载并显示在 屏幕。

我已经尝试解决这个问题很长时间了,但还没有解决。有什么想法吗?这是我的代码的一部分:

     protected Integer doInBackground(Void... params) {         
        //...
            try {
                //download images
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (!loadTask.isCancelled()) {                  
                //Display images        
                int length=imageList.size();        

                for (i=0; i<length; i++) {                      
                    publishProgress(i);                     
                }
            }               

        return 1;                       
    }

  protected void onProgressUpdate(Integer... a) {
        //interaction with UI thread
        int i = a[0];
        ImageView im = new ImageView (MyActivity.this);
        im.setImageBitmap(getRemoteImage(imageUrl));
        im.setLayoutParams(Params);
        im.setAdjustViewBounds(true);
        im.setPadding(px2, px2, px2, px2);
        im.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  //...
              }               
        });
    }

In my activity, I download several images and then display them by adding each one of them on my main layout. I tried these 3 approaches but each time my progress dialog freezes until the images are loaded:

1st approach: Using an async task: I download the images on doInBackground() and add them to the main layout onPostExecute ()

2nd: The same as above, but I add each one of the images onProgressUpdate()

3rd: Using a handler and a thread

Each time the progress dialog freezes for a few seconds (actually the whole application freezes) until the images are downloaded and displayed on the screen.

I have been trying to fix this for a long time but nothing yet. Any ideas? Here's a part of my code:

     protected Integer doInBackground(Void... params) {         
        //...
            try {
                //download images
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (!loadTask.isCancelled()) {                  
                //Display images        
                int length=imageList.size();        

                for (i=0; i<length; i++) {                      
                    publishProgress(i);                     
                }
            }               

        return 1;                       
    }

  protected void onProgressUpdate(Integer... a) {
        //interaction with UI thread
        int i = a[0];
        ImageView im = new ImageView (MyActivity.this);
        im.setImageBitmap(getRemoteImage(imageUrl));
        im.setLayoutParams(Params);
        im.setAdjustViewBounds(true);
        im.setPadding(px2, px2, px2, px2);
        im.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  //...
              }               
        });
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-12-27 17:05:22
im.setImageBitmap(getRemoteImage(imageUrl));

如果 getRemoteImage() 执行其名称和参数所建议的操作(从网络下载图像),那么这一行就是您的问题,因为它是在 onProgressUpdate( )。

onProgressUpdate() 在 UI 线程中运行,而不是在后台线程中运行,它应该在进度更改时执行 UI 工作。当您在此处下载图像时,您会阻止 UI 线程完成图像下载所需的时间。由于您循环遍历所有图像,因此它将保留您的 UI 线程,直到下载所有图像。

而是在 doInBackground() 中运行下载(方法顶部的注释表明您已经这样做了),并将下载的图像传递给 onProgressUpdate< /code> 通过 publishProgress() 所采用的参数。

im.setImageBitmap(getRemoteImage(imageUrl));

If getRemoteImage() does what it's name and argument suggests (downloading an image from the web), then this line is your problem, since it's executed in onProgressUpdate().

onProgressUpdate() runs in the UI-thread and not in the background thread, it's supposed to do UI work when the progress changed. When you download your images here, you block the UI thread for a the amount of time it takes for the download of an image to finish. Since you loop over all images, it will hold your UI thread until all your images are downloaded.

Rather run the download in doInBackground() (your comment at the top of the method suggests that you already do that) and pass the downloaded image over to onProgressUpdate via the argument that publishProgress() takes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文