ListView異步加載圖片是很是實用的方法,凡是是要經過網絡獲取圖片資源通常使用這種方法比較好,用戶體驗好,下面就說實現方法,先貼上主方法的代碼:javascript
- package cn.wangmeng.test;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.lang.ref.SoftReference;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
-
- import android.graphics.drawable.Drawable;
- import android.os.Handler;
- import android.os.Message;
-
- public class AsyncImageLoader {
-
- private HashMap<String, SoftReference<Drawable>> p_w_picpathCache;
-
- public AsyncImageLoader() {
- p_w_picpathCache = new HashMap<String, SoftReference<Drawable>>();
- }
-
- public Drawable loadDrawable(final String p_w_picpathUrl, final ImageCallback p_w_picpathCallback) {
- if (p_w_picpathCache.containsKey(p_w_picpathUrl)) {
- SoftReference<Drawable> softReference = p_w_picpathCache.get(p_w_picpathUrl);
- Drawable drawable = softReference.get();
- if (drawable != null) {
- return drawable;
- }
- }
- final Handler handler = new Handler() {
- public void handleMessage(Message message) {
- p_w_picpathCallback.p_w_picpathLoaded((Drawable) message.obj, p_w_picpathUrl);
- }
- };
- new Thread() {
- @Override
- public void run() {
- Drawable drawable = loadImageFromUrl(p_w_picpathUrl);
- p_w_picpathCache.put(p_w_picpathUrl, new SoftReference<Drawable>(drawable));
- Message message = handler.obtainMessage(0, drawable);
- handler.sendMessage(message);
- }
- }.start();
- return null;
- }
-
- public static Drawable loadImageFromUrl(String url) {
- URL m;
- InputStream i = null;
- try {
- m = new URL(url);
- i = (InputStream) m.getContent();
- } catch (MalformedURLException e1) {
- e1.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- Drawable d = Drawable.createFromStream(i, "src");
- return d;
- }
-
- public interface ImageCallback {
- public void p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl);
- }
-
- }
package cn.wangmeng.test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
public class AsyncImageLoader {
private HashMap<String, SoftReference<Drawable>> p_w_picpathCache;
public AsyncImageLoader() {
p_w_picpathCache = new HashMap<String, SoftReference<Drawable>>();
}
public Drawable loadDrawable(final String p_w_picpathUrl, final ImageCallback p_w_picpathCallback) {
if (p_w_picpathCache.containsKey(p_w_picpathUrl)) {
SoftReference<Drawable> softReference = p_w_picpathCache.get(p_w_picpathUrl);
Drawable drawable = softReference.get();
if (drawable != null) {
return drawable;
}
}
final Handler handler = new Handler() {
public void handleMessage(Message message) {
p_w_picpathCallback.p_w_picpathLoaded((Drawable) message.obj, p_w_picpathUrl);
}
};
new Thread() {
@Override
public void run() {
Drawable drawable = loadImageFromUrl(p_w_picpathUrl);
p_w_picpathCache.put(p_w_picpathUrl, new SoftReference<Drawable>(drawable));
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
}
}.start();
return null;
}
public static Drawable loadImageFromUrl(String url) {
URL m;
InputStream i = null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(i, "src");
return d;
}
public interface ImageCallback {
public void p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl);
}
}
以上代碼是實現異步獲取圖片的主方法,SoftReference是軟引用,是爲了更好的爲了系統回收變量,重複的URL直接返回已有的資源,實現回調函數,讓數據成功後,更新到UI線程。
幾個輔助類文件:java
- package cn.wangmeng.test;
-
- public class ImageAndText {
- private String p_w_picpathUrl;
- private String text;
-
- public ImageAndText(String p_w_picpathUrl, String text) {
- this.p_w_picpathUrl = p_w_picpathUrl;
- this.text = text;
- }
- public String getImageUrl() {
- return p_w_picpathUrl;
- }
- public String getText() {
- return text;
- }
- }
package cn.wangmeng.test;
public class ImageAndText {
private String p_w_picpathUrl;
private String text;
public ImageAndText(String p_w_picpathUrl, String text) {
this.p_w_picpathUrl = p_w_picpathUrl;
this.text = text;
}
public String getImageUrl() {
return p_w_picpathUrl;
}
public String getText() {
return text;
}
}
- package cn.wangmeng.test;
-
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
-
- public class ViewCache {
-
- private View baseView;
- private TextView textView;
- private ImageView p_w_picpathView;
-
- public ViewCache(View baseView) {
- this.baseView = baseView;
- }
-
- public TextView getTextView() {
- if (textView == null) {
- textView = (TextView) baseView.findViewById(R.id.text);
- }
- return textView;
- }
-
- public ImageView getImageView() {
- if (p_w_picpathView == null) {
- p_w_picpathView = (ImageView) baseView.findViewById(R.id.p_w_picpath);
- }
- return p_w_picpathView;
- }
-
- }
package cn.wangmeng.test;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewCache {
private View baseView;
private TextView textView;
private ImageView p_w_picpathView;
public ViewCache(View baseView) {
this.baseView = baseView;
}
public TextView getTextView() {
if (textView == null) {
textView = (TextView) baseView.findViewById(R.id.text);
}
return textView;
}
public ImageView getImageView() {
if (p_w_picpathView == null) {
p_w_picpathView = (ImageView) baseView.findViewById(R.id.p_w_picpath);
}
return p_w_picpathView;
}
}
ViewCache是輔助獲取adapter的子元素佈局
android
- package cn.wangmeng.test;
-
- import java.util.List;
-
- import cn.wangmeng.test.AsyncImageLoader.ImageCallback;
-
- import android.app.Activity;
- import android.graphics.drawable.Drawable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
-
- public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
-
- private ListView listView;
- private AsyncImageLoader asyncImageLoader;
-
- public ImageAndTextListAdapter(Activity activity, List<ImageAndText> p_w_picpathAndTexts, ListView listView) {
- super(activity, 0, p_w_picpathAndTexts);
- this.listView = listView;
- asyncImageLoader = new AsyncImageLoader();
- }
-
- public View getView(int position, View convertView, ViewGroup parent) {
- Activity activity = (Activity) getContext();
-
-
- View rowView = convertView;
- ViewCache viewCache;
- if (rowView == null) {
- LayoutInflater inflater = activity.getLayoutInflater();
- rowView = inflater.inflate(R.layout.p_w_picpath_and_text_row, null);
- viewCache = new ViewCache(rowView);
- rowView.setTag(viewCache);
- } else {
- viewCache = (ViewCache) rowView.getTag();
- }
- ImageAndText p_w_picpathAndText = getItem(position);
-
-
- String p_w_picpathUrl = p_w_picpathAndText.getImageUrl();
- ImageView p_w_picpathView = viewCache.getImageView();
- p_w_picpathView.setTag(p_w_picpathUrl);
- Drawable cachedImage = asyncImageLoader.loadDrawable(p_w_picpathUrl, new ImageCallback() {
- public void p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl) {
- ImageView p_w_picpathViewByTag = (ImageView) listView.findViewWithTag(p_w_picpathUrl);
- if (p_w_picpathViewByTag != null) {
- p_w_picpathViewByTag.setImageDrawable(p_w_picpathDrawable);
- }
- }
- });
- if (cachedImage == null) {
- p_w_picpathView.setImageResource(R.drawable.default_p_w_picpath);
- }else{
- p_w_picpathView.setImageDrawable(cachedImage);
- }
-
- TextView textView = viewCache.getTextView();
- textView.setText(p_w_picpathAndText.getText());
-
- return rowView;
- }
-
- }
package cn.wangmeng.test;
import java.util.List;
import cn.wangmeng.test.AsyncImageLoader.ImageCallback;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
private ListView listView;
private AsyncImageLoader asyncImageLoader;
public ImageAndTextListAdapter(Activity activity, List<ImageAndText> p_w_picpathAndTexts, ListView listView) {
super(activity, 0, p_w_picpathAndTexts);
this.listView = listView;
asyncImageLoader = new AsyncImageLoader();
}
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
// Inflate the views from XML
View rowView = convertView;
ViewCache viewCache;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.p_w_picpath_and_text_row, null);
viewCache = new ViewCache(rowView);
rowView.setTag(viewCache);
} else {
viewCache = (ViewCache) rowView.getTag();
}
ImageAndText p_w_picpathAndText = getItem(position);
// Load the p_w_picpath and set it on the ImageView
String p_w_picpathUrl = p_w_picpathAndText.getImageUrl();
ImageView p_w_picpathView = viewCache.getImageView();
p_w_picpathView.setTag(p_w_picpathUrl);
Drawable cachedImage = asyncImageLoader.loadDrawable(p_w_picpathUrl, new ImageCallback() {
public void p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl) {
ImageView p_w_picpathViewByTag = (ImageView) listView.findViewWithTag(p_w_picpathUrl);
if (p_w_picpathViewByTag != null) {
p_w_picpathViewByTag.setImageDrawable(p_w_picpathDrawable);
}
}
});
if (cachedImage == null) {
p_w_picpathView.setImageResource(R.drawable.default_p_w_picpath);
}else{
p_w_picpathView.setImageDrawable(cachedImage);
}
// Set the text on the TextView
TextView textView = viewCache.getTextView();
textView.setText(p_w_picpathAndText.getText());
return rowView;
}
}
ImageAndTextListAdapter是實現ListView的Adapter,裏面有個技巧就是p_w_picpathView.setTag(p_w_picpathUrl),setTag是存儲數據的,這樣是爲了保證在回調函數時,listview去更新本身對應item,你們仔細閱讀就知道了。
最後貼出佈局文件:
網絡
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
-
- <ImageView android:id="@+id/p_w_picpath"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
-
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
-
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/p_w_picpath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
http://www.iteye.com/topic/685986
原文地址:http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2app