0

我需要将 30 多张图片加载到一个页面(带有预告文字)。我通过 AsyncTask 加载所有图像,但我无法修改当前布局,因为您只能对主线程上的视图执行修改。异步加载所有内容的最佳方法是什么,这样我的应用程序就不必在“页面加载”期间挂起?

4

3 回答 3

0

Why cannot modify layout? for AsyncTask, you have onProgressUpdate that guarantees to run on UI Thread. You just need to call publishProgress in your doInBackground code.

==Update==

private class LoadImageTask<Url, String, String> extends AsynTask{
    doInBackground(Url... urls){
        for(int i=0; i<urls.length; i++){
            File img = DownloadImage(urls[i]); // Your download method.. 
            publishProgress(img.filename);     // Suppose it is saved in sd card
            // publish progress will call onProgressUpdate immediately, but on different thread
        }
    }

    onProgressUpdate(String filename){
        // Update the UI.. this method will run on UI thread
        displayImage(filename);
    }
}
于 2012-05-09T00:33:44.470 回答
0

I don't think you can build a view in the background. You're best bet would be to show a progress dialoge so the user understands why the app is busy.

于 2012-05-09T00:34:58.093 回答
0

如果您的课程扩展了活动,您可以使用

runOnUiThread(new Runnable() {
    public void run() {
        //Update UI elements in this block
    }  
});
于 2012-05-09T00:36:08.780 回答