I Got câu trả lời này trong đường dẫn sau: Lazy load of images in ListView
package com.wilson.android.library;
/* Licensed to Apache Software Foundation (ASF) dưới một hay các thỏa thuận giấy phép người đóng góp. Xem tệp THÔNG BÁO được phân phối với tác phẩm này để biết thêm thông tin liên quan đến quyền sở hữu bản quyền. ASF cấp phép cho tệp này cho bạn theo Giấy phép Apache, Phiên bản 2.0 ( "Giấy phép"); bạn không được sử dụng tệp này ngoại trừ việc tuân thủ bằng Giấy phép. Bạn có thể có được một bản sao của Giấy phép tại
http://www.apache.org/licenses/LICENSE-2.0
Trừ khi yêu cầu của pháp luật hiện hành hoặc đồng ý bằng văn bản, phần mềm phân phối theo giấy phép được phân phối trên một "NGUYÊN TRẠNG", KHÔNG CÓ BẢO ĐẢM HOẶC ĐIỀU KIỆN OF ANY LOẠI, thể hiện hoặc ngụ ý. Xem Giấy phép cho các quyền hạn và điều khoản ngôn ngữ cụ thể của cụ thể theo Giấy phép.
*/
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() {
@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();
}
}
Hy vọng nó giúp.
Nguồn
2012-09-26 06:24:26
điều gì đó tương tự: http://stackoverflow.com/questions/2912054/lazy-load-images-on-listview-in-androidbeginner-level – ahsan