麦子学院android老师分享android实现listview异步加载图片的方法
针对listview异步加载图片这个问题,麦子学院android开发老师讲了一种非常实用的方法,麦子学院android开发老师说凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码:
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
public AsyncImageLoader() {
imageCache = new HashMap
}
public Drawable loadDrawable(final StringimageUrl, final ImageCallback imageCallback) {
if (imageCache.containsKey(imageUrl)){
SoftReference
Drawable drawable = softReference.get();
if (drawable != null) {
return drawable;
}
}
final Handler handler = new Handler(){
public void handleMessage(Messagemessage) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}
};
new Thread() {
@Override
public void run() {
Drawable drawable =loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, newSoftReference
Message message =handler.obtainMessage(0, drawable);
handler.sendMessage(message);
}
}.start();
return null;
}
publicstatic Drawable loadImageFromUrl(String url) {
URLm;
InputStreami = null;
try{
m= new URL(url);
i= (InputStream) m.getContent();
}catch (MalformedURLException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
Drawabled = Drawable.createFromStream(i, "src");
returnd;
}
public interface ImageCallback {
public void imageLoaded(DrawableimageDrawable, String imageUrl);
}
}
以上代码是实现异步获取图片的主方法,SoftReference是软引用,是为了更好的为了系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。
几个辅助类文件:
package cn.wangmeng.test;
public class ImageAndText {
private String imageUrl;
private String text;
public ImageAndText(String imageUrl, Stringtext) {
this.imageUrl = imageUrl;
this.text = text;
}
public String getImageUrl() {
return imageUrl;
}
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 imageView;
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 (imageView == null) {
imageView = (ImageView)baseView.findViewById(R.id.image);
}
return imageView;
}
}
ViewCache是辅助获取adapter的子元素布局
package cn.wangmeng.test;
import java.util.List;
importcn.wangmeng.test.AsyncImageLoader.ImageCallback;
import android.app.Activity;
importandroid.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 ImageAndTextListAdapterextends ArrayAdapter
private ListView listView;
private AsyncImageLoader asyncImageLoader;
public ImageAndTextListAdapter(Activityactivity, List
super(activity, 0, imageAndTexts);
this.listView = listView;
asyncImageLoader = newAsyncImageLoader();
}
public View getView(int position, ViewconvertView, 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.image_and_text_row, null);
viewCache = new ViewCache(rowView);
rowView.setTag(viewCache);
} else {
viewCache = (ViewCache)rowView.getTag();
}
ImageAndText imageAndText =getItem(position);
// Load the image and set it on theImageView
String imageUrl =imageAndText.getImageUrl();
ImageView imageView =viewCache.getImageView();
imageView.setTag(imageUrl);
Drawable cachedImage =asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
public void imageLoaded(DrawableimageDrawable, String imageUrl) {
ImageView imageViewByTag =(ImageView) listView.findViewWithTag(imageUrl);
if (imageViewByTag != null) {
imageViewByTag.setImageDrawable(imageDrawable);
}
}
});
if(cachedImage == null) {
imageView.setImageResource(R.drawable.default_image);
}else{
imageView.setImageDrawable(cachedImage);
}
// Set the text on the TextView
TextView textView =viewCache.getTextView();
textView.setText(imageAndText.getText());
return rowView;
}
}
ImageAndTextListAdapter是实现ListView的Adapter,里面有个技巧就是imageView.setTag(imageUrl),setTag是存储数据的,这样是为了保证在回调函数时,listview去更新自己对应item,大家仔细阅读就知道了。
最后贴出布局文件:
<?xml version="1.0"encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
/>
android:layout_height="wrap_content"/>
更多相关文章
- Android 更新UI的两种方法——handler和runOnUiThread(
- android在一个app程序中,打开另一个app的方法
- Android Glide加载图片,宽度占满屏幕高度自适应
- 小米5手机Android运行程序闪退出错解决方法
- Android 查看SHA1值的方法
- 如何正确实现Android启动屏画面的方法(避免白屏)
- Android 控件ImageView图片框