我正在使用ListView来显示与这些图像相关的一些图像和标题。我正在从互联网上获取图像。是否有一种方法可以延迟加载图像,以便在文本显示时,UI不会被阻止,图像在下载时显示?
图像总数不是固定的。
我正在使用ListView来显示与这些图像相关的一些图像和标题。我正在从互联网上获取图像。是否有一种方法可以延迟加载图像,以便在文本显示时,UI不会被阻止,图像在下载时显示?
图像总数不是固定的。
当前回答
以下是我创建的用于保存应用程序当前显示的图像的内容。请注意,这里使用的“Log”对象是我对Android中最终Log类的自定义包装。
package com.wilson.android.library;
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
import java.io.IOException;
public class DrawableManager {
private final Map<String, Drawable> drawableMap;
public DrawableManager() {
drawableMap = new HashMap<String, Drawable>();
}
public Drawable fetchDrawable(String urlString) {
if (drawableMap.containsKey(urlString)) {
return drawableMap.get(urlString);
}
Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
if (drawable != null) {
drawableMap.put(urlString, drawable);
Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
+ drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
+ drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
} else {
Log.w(this.getClass().getSimpleName(), "could not get thumbnail");
}
return drawable;
} catch (MalformedURLException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
return null;
} catch (IOException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
return null;
}
}
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
if (drawableMap.containsKey(urlString)) {
imageView.setImageDrawable(drawableMap.get(urlString));
}
final Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
imageView.setImageDrawable((Drawable) message.obj);
}
};
Thread thread = new Thread() {
@Override
public void run() {
//TODO : set imageView to a "pending" image
Drawable drawable = fetchDrawable(urlString);
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
};
thread.start();
}
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
其他回答
这是安卓系统上的一个常见问题,许多人已经通过多种方式解决了这个问题。在我看来,我见过的最好的解决方案是相对较新的图书馆毕加索。以下是亮点:
开源,但由著名的ActionBarSherlock的杰克·沃顿领导。使用一行代码从网络或应用程序资源异步加载图像自动ListView检测自动磁盘和内存缓存可以执行自定义转换大量可配置选项超简单API经常更新
我用图像制作了一个懒惰列表(位于GitHub)的简单演示。
基本用途ImageLoader ImageLoader=新的ImageLoader(上下文)。。。imageLoader.DisplayImage(url,imageView);不要忘记添加对AndroidManifest.xml的以下权限:<uses permission android:name=“android.ppermission.INTERNET”/><uses permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE”/>请只创建ImageLoader的一个实例,并在您的应用这样,图像缓存将更加高效。
这可能对某人有帮助。它在后台线程中下载图像。图像缓存在SD卡和内存中。缓存的实现非常简单,对于演示来说就足够了。我使用inSampleSize解码图像以减少内存消耗。我还尝试正确处理回收的视图。
我写了一篇教程,解释如何在列表视图中延迟加载图像。我详细讨论了回收和并发的问题。我还使用固定线程池来防止产生大量线程。
在Listview教程中延迟加载图像
检查我的懒惰列表。基本上,我通过延迟ImageView的调用来改进LazyList,并创建两个方法:
当您需要放置类似“正在加载图像…”的内容时当您需要显示下载的图像时。
我还通过在这个对象中实现一个单例来改进ImageLoader。
我这样做的方式是启动一个线程,在后台下载图像,并为每个列表项传递一个回调。当图像下载完成后,它将调用回调,以更新列表项的视图。
然而,当您回收视图时,这种方法不太有效。