【问题标题】:Downloading images in background while showing the listview在显示列表视图时在后台下载图像
【发布时间】:2011-03-23 09:36:11
【问题描述】:
我有一个包含我在列表中显示的图像的所有 URI 的数组。
现在我想在后台运行一个线程,从网络获取这些图像并将它们存储在 SD 卡上。
因此,当我单击列表中的特定元素而不是从网络中获取时,它应该从新活动中的 SD 卡中获取。
我该怎么做?
【问题讨论】:
标签:
android
listview
image
【解决方案1】:
您可以尝试使用我的惰性绘图框架(GPL v3 许可):
https://github.com/rtyley/lazy-drawables
-不幸的是,它还没有完全准备好投入生产,但它试图处理的事情包括:
- 图片资源的后台(非 UI 线程)加载
- 缩放位图的高效缓存 - 适用于您正在下载单个大图像并希望在列表视图或其他内容中以较小的图标格式显示数百次的用例
- 使用的透明度,我的意思是......
...一旦你设置了一个惰性绘制会话,填充一个图像视图就像这样简单:
Drawable avatarDrawable = imageSession.get(someUserIdentifier); // doesn't block
imageView.setImageDrawable(avatarDrawable);
您不必在意它可能立即为您返回了一个普通的 bitmapdrawable,或者可能产生了一个异步任务并返回了一个占位符 drawable - 当异步时,drawable 将自行更新并且它是宿主 ImageView任务已完成。
无论如何,这就是理论。到目前为止,它似乎有点工作,但有时这种自发的 UI 更新不会发生......不知道为什么:-}
【解决方案2】:
使用AsyncTask在后台下载,同时显示您的用户界面
File imageFile;
从网上保存图片
public View getView(int position, View convertView, ViewGroup parent) {
imageFile = new File("/sdcard/jorgesysTemp/" + Arraypicname(position);
if(!imageFile.exists()){
AsyncTaskProcess() //Download your images to sdcard
}
当您单击列表视图时,从 sdcard 加载图像
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
//load from sdcard
Bitmap bitmapImg = BitmapFactory.decodeFile("/sdcard/jorgesysTemp/" + Arraypicname(position));
BitmapDrawable drawableImg = new BitmapDrawable(bitmapImg );
myimageview.setImageDrawable(drawableImg );
...
...
...
【解决方案3】:
检查WebImageView by matthias käppler。
您可以用您想要的 SD 卡功能替换他缓存图像的方式。