0

我发现很多帖子解释了如何在ListViewusing中加载图像AsyncTask,问题是所有的,至少我发现的那些,都是通过在适配器内部调用方法AsyncTask时实例化并启动一个来完成的。getViewListView

我已经这样做了,一切正常,但是当我ListView快速完成时,它会抛出一个异常,.execute说它拒绝创建更多线程。

所以我的问题是如何在继续使用的同时克服这个问题AsyncTasks?(我可以使用单一的“普通 Java”,Thread但项目规范说我不允许。

谢谢和欢呼

4

4 回答 4

2

这是在 gridview、listview 和 pager 中显示图像的绝佳示例。看一下这个

https://github.com/nostra13/Android-Universal-Image-Loader

于 2013-08-30T11:23:43.990 回答
2

我会选择 Aquery for Android

aq.id(R.id.image1).image("http://www.vikispot.com/z/images/vikispot/android-w.png");

了解更多:android-query - ImageLoading

或 Volley: Volley:适用于 Android 的简单、快速的网络

于 2013-08-30T11:38:54.287 回答
2

我发现这非常有用:

    class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    private String url;
    private final WeakReference<ImageView> imageViewReference;

    public BitmapDownloaderTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(String... params) {
         // params comes from the execute() call: params[0] is the url.
         return downloadBitmap(params[0]);
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

在这里查看完整的帖子

于 2013-08-30T11:00:59.703 回答
1

您应该使用IntentService而不是AsyncTask缓存您已经下载的图像,以便下次不需要获取数据。或者你可以看看现有的项目,比如SmartImageView应该可以满足你的所有需求

于 2013-08-30T10:59:54.853 回答