如何从doinbackground执行其它线程()线程、doinbackground

2023-09-07 10:09:21 作者:爱我的那颗心还在吗

我想用 ImageLoader.loadImage 这将启动多线程下载多个图像。因为他们需要一段时间来执行,我不希望锁定用户界面,我想在的AsyncTask的 doInBackground()函数运行它们。

I want to download multiple images using ImageLoader.loadImage which will launch multiple threads. Because they take a while to execute and i don't want to lock up the UI, i want to run them in the doInBackground() function of AsyncTask.

不过,我不能在 doInBackground()功能推出新的线程。有没有解决的办法?

However I cannot launch new threads in the doInBackground() function. Is there a way around this?

推荐答案

我同意323go提出的意见

I agree with the comment made by 323go

的AsyncTask的设计是围绕主题和Handler一个辅助类,并不构成通用线程框架。 AsyncTasks理论上应该用于短操作(在几秒钟之最。)如果你需要保持对长时间运行的线程,强烈建议您使用由Java提供的各种API .util.concurrent pacakge如执行程序,并ThreadPoolExecutor的FutureTask提供。(直接从DOC)

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. (Straight from the doc)

作为一种替代方法,您可以使用 https://github.com/octo-online/robospice。你可以多香料的要求的。

As an alternative you can use https://github.com/octo-online/robospice. You can make multiple spice request's.

通用图像装载机

要下载并显示大量图像,使用列表视图或网格视图。为此,您可以使用通用图像装载机或懒惰名单。通用图像加载器的工作原理对样品原则懒列表。

To download and display large number of images, use a list view or grid view. For this you can use Universal Image Loader or Lazy list. Universal Image loader works on the sample principle as lazy list.

我不得不显示来自Picasa相册公用文件夹(约300 - 500)影像。我做了一个HTTP请求和响应是JSON。我用的AsyncTask张贴http请求,得到响应,JSON解析得到的URL。一旦我得到了网址的,我用通用图像加载器加载图像。所以,你可以使用AsyncTask的一个短期运行的操作。

I had to display images from picasa album public folder (about 300 - 500). I made a http request and the response was json. I used asynctask to post http request, get response , parse json to get the urls. Once i got the url's i used Universal Image Loader to load images. So you can use asynctask for a short running operation.

假设你可以在列表中一次查看3张照片。这三个图像下载,缓存如果它不显示。当您滚动的程序重复。一旦缓存图像无需再次下载。在这种情况下,用户界面​​不会被阻断。您可以向下随时滚动。

Suppose you can view 3 images at a time in a list. The three images are downloaded, cached if its not and displayed. When you scroll the procedure repeats. Once cached images need not be downloaded again. The UI in this case is not blocked. You can scroll down any time.

地址被认为是关键。图像被缓存到SD卡或手机内存。对于高速缓存中的位置可以被指定。如果图像中的缓存中存在。从缓存中显示图像,如果没有下载,缓存和显示图像。

Url is considered as the key. Image is cached to sdcard or phone memory. The location for cache can be specified. If the image exists in cache. display images from cache, if not download, cache and display images.

两者都使用缓存。通用图像装载机有很多的配置选项。 https://github.com/nostra13/Android-Universal-Image-Loader

Both use caching. Universal Image Loader has lot of configuration options. https://github.com/nostra13/Android-Universal-Image-Loader

看在链接的功能。

在自定义适配器的构造函数

In your custom adapter constructor

 File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");

 // Get singletone instance of ImageLoader
 imageLoader = ImageLoader.getInstance();
 // Create configuration for ImageLoader (all options are optional)
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
      // You can pass your own memory cache implementation
     .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
     .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
     .enableLogging()
     .build();
  // Initialize ImageLoader with created configuration. Do it once.
  imageLoader.init(config);
  options = new DisplayImageOptions.Builder()
  .showStubImage(R.drawable.stub_id)//display stub image
  .cacheInMemory()
  .cacheOnDisc()
  .displayer(new RoundedBitmapDisplayer(20))
  .build();

在你的getView()

In your getView()

  ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
   imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options

您可以与其他选项配置,以满足您的需求。

You can configure with other options to suit your needs.

您应该使用平滑滚动和性能viewholder。 http://developer.android.com/training/improving-layouts/smooth -scrolling.html

You should use a viewholder for smooth scrolling and performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html