我有一个列表活动,每一行都有一个我从网络加载的不同图像和一些文本。
我想实现 asynctask 这样图像就会加载到后台。
我搜索了很多教程,但没有找到每行使用不同图像的教程。问题是 doInBackground 方法应该从互联网加载图像。但由于它们都是不同的并且依赖于其行,线程如何加载它们?
全局变量:位图bm; BitmapFactory.Options bmOptions;
在我的 getView 方法中,我有
ImageView img = (ImageView) v.findViewById(R.id.icon);
如果(img!=空){
bm = LoadImage(o.getLink(), bmOptions);
img.setImageBitmap(bm);
与文本加载
一起...这个img应该在doInBackground方法上加载,但是我无法访问我知道的o(User类型)对象,因为getView给了我位置。
有谁知道如何解决这个问题?
谢谢,
丽塔
I have a listactivity and each row has a different image that i load from the web and some text.
I'd like to implement asynctask so the images would load on the background.
I've searched many tutorials but haven't found none that use different images per row. The problem is the method doInBackground that is supposed to load the images from the internet. But since they are all different and they depend on its row, how can the thread load them?
Global variables: Bitmap bm; BitmapFactory.Options bmOptions;
In my getView method, I have
ImageView img = (ImageView) v.findViewById(R.id.icon);
if (img!=null){
bm = LoadImage(o.getLink(), bmOptions);
img.setImageBitmap(bm);
}
along with the text load... this img should be load on the doInBackground method, but then I'd have no access to the o (of type User) object that I know because getView gives me the position.
Does anyone have any idea on how to solve this problem?
Thanks,
Rita
发布评论
评论(3)
有两种方法可以做到这一点:简单的方法或无聊的方法。
如果您可以添加额外的依赖项,那么我强烈建议您采取简单的路线并使用 异步图像视图。我个人会使用后者。
另一方面,无聊的方法是打破
HttpClient
并手动下载图像,然后使用BitmapFactory.decodeStream
方法创建Bitmap< /代码>。
下载需要在 UI 线程之外进行,因此使用
AsyncTask
来运行所有内容并使用 Handler 将 Bitmap 实际插入到ImageView
中可能会感觉很自然code> (注意不要在已经回收的视图上设置位图)。不幸的是,在许多实现中,运行 AsyncTask 的 ExecutorService 仅限于使用一个线程。这给列表带来了一种不幸的感觉,其中图像一张一张地加载,而不是您可能期望的尽可能多的一次加载的感觉。
在这种情况下,您可能希望(本质上)推出自己的
AsyncTask
。正如您所知,我不是
AsyncTask
的忠实粉丝。上面是一个很常见的任务,而且要正确执行是一件很烦人的事情,我非常建议使用其他人的库。
There are two ways you can do this: the easy way or the boring way.
If you can add extra dependencies, then I would strongly recommend you take the easy route and use GreedDroid. You can use the item framework they have in there or use the AsyncImageView. Personally I'd use the latter.
The boring way, on the other hand, would be to break out the
HttpClient
and download the image manually, then useBitmapFactory.decodeStream
method to create aBitmap
.The downloading needs to happen away from the UI thread, so it may feel natural to use an
AsyncTask
to run it all, and the use a Handler to actually poke the Bitmap into theImageView
(being careful not to set the Bitmap on a view which has already been recycled).Unfortunately, on many implementations, the
ExecutorService
that runs the AsyncTasks are limited to using oneThread
. This gives an unfortunate feel to the List where the images load one by one, instead of the as-many-as-possible-all-at-once feel to it you may be expecting.In that case, you'll probably be wanting to (essentially) roll your own
AsyncTask
.I'm not a massive fan of
AsyncTask
s, as you can tell.The above is such a common task, and such an annoying thing to get right, I'd very much recommend using someone else's library.
我相信您需要编写自己的列表适配器,以便随着显示越来越多的列表项(例如通过向下滚动),适配器会创建更多的 AsyncTasks 并在获取图像后显示图像。
http://developer.android.com/reference/android/widget/ListAdapter.html
I believe you will need to write your own List Adapter so that as more and more list items are displayed (by scrolling down for example), the adapter creates more AsyncTasks and displays the images once they have been fetched.
http://developer.android.com/reference/android/widget/ListAdapter.html
这是使用异步任务下载图像的地方,只需看看
http://developer.android.com/resources/articles/painless-threading.html
This is where images will be downloaded using async task just have look
http://developer.android.com/resources/articles/painless-threading.html