三种方式:
1、new Thread 加载到的图片会出现错位的现象,本来该显示在第二的图片也许会显示到第一,且每次都要加载,耗流量
2、AsyncTask + LruCache 解决了图片显示错位问题,和缓存问题,缺点是代码写起来太繁琐
3、FinalBitmap 代码简洁且图片可正常显示和缓存
1、new Thread方法异步加载图片
package com.example.logintest;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GuildAdapter extends BaseAdapter {
private ArrayList<DefineClass> list;
private LayoutInflater inflater;
private Bitmap bitmap;
GuildAdapter(Context context,ArrayList<DefineClass> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.guild_list, null);
holder.picima = (ImageView)convertView.findViewById(R.id.guildima);
holder.nametxt = (TextView)convertView.findViewById(R.id.name);
holder.numbertxt = (TextView)convertView.findViewById(R.id.number);
holder.Notxt = (TextView)convertView.findViewById(R.id.No);
holder.introductiontxt = (TextView)convertView.findViewById(R.id.guildintroduction);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final DefineClass bean = list.get(position);
String url = bean.getpic();
<span style="color:#993399;">holder.picima.setImageBitmap(GetBitmap(url));</span>
holder.nametxt.setText(bean.getname());
holder.numbertxt.setText(bean.getnumber());
holder.Notxt.setText(bean.getNo());
holder.introductiontxt.setText(bean.getintroduction());
return convertView;
}
public class ViewHolder {
TextView nametxt,introductiontxt,numbertxt,Notxt;
ImageView picima;
}
<span style="color:#993399;">private Bitmap GetBitmap(final String url) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL urls = new URL(url);
InputStream is = urls.openStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
return bitmap;
}</span>
}
2、AsyncTask+LruCache下载缓存图片
package com.example.logintest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
@SuppressLint("NewApi")
public class LoadImageThread {
private LruCache<String,Bitmap> mcaches;
LoadImageThread() {//构造函数 <span style="color:#993399;">此处主要对LruCache缓存进行了初步设置</span>
int maxMemory = (int)Runtime.getRuntime().maxMemory(); <span style="color:#993399;">获取最大可用内存</span>
int cacheSize = maxMemory / 4; <span style="color:#993399;">设定缓存大小</span>
mcaches = new LruCache<String,Bitmap>(cacheSize) { <span style="color:#993399;">初始化LruCache,每次存入缓存的时候调用,返回bitmap的大小</span>
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
//主要方法,在特定的url下下载图片,并给imageview设置图片
public void ImageAsyncTask(ImageView iv,String url) {
Bitmap bitmap = mcaches.get(url); //从缓存中取出图片
if(bitmap == null) {//缓存中没有图片
new myAsyncTask(iv,url).execute(url); //AsyncTask下载图片
} else {
iv.setImageBitmap(bitmap); //直接将缓存中的图片设置显示
}
}
private class myAsyncTask extends AsyncTask<String,Void,Bitmap> { <span style="color:#993399;"> AsyncTask异步加载</span>
private String murl;
private ImageView mimg;
public myAsyncTask(ImageView iv,String url) { <span style="color:#993399;">这一部分是为了给控件设置要显示的图片</span>
mimg = iv;
murl = url;
}
@Override
protected Bitmap <span style="color:#993399;">doInBackground</span>(String... params) { <span style="color:#993399;">此方法必须在这里重写,异步执行后台将要完成的任务。</span>
//从网络上获取图片
Bitmap bitmap = getImage(params[0]); //调用getImage方法下载图片
String url = params[0];
if(bitmap != null) {
//将获取到的图片加入缓存
addBitmaptoCache(url,bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) { //为主线程设置显示的图片
super.onPostExecute(result);
if(mimg.getTag().equals(murl)) {
mimg.setImageBitmap(result);
}
}
}
//给缓存中添加数据
public void addBitmaptoCache(String url,Bitmap bitmap) {
if(mcaches.get(url) == null) { //判断缓存中是否空
mcaches.put(url, bitmap); //为空,则设置数据
}
}
private Bitmap getImage(String url) { //被调用的下载图片方法
HttpURLConnection connection;
InputStream in = null;
URL urls;
Bitmap bitmap = null;
try {
urls = new URL(url);
connection = (HttpURLConnection) urls.openConnection();
in = connection.getInputStream();
BufferedInputStream buffer = new BufferedInputStream(in);
bitmap = BitmapFactory.decodeStream(buffer);
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
}
设置图片显示:
package com.example.logintest;
import java.util.List;
import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GuildAdapter extends BaseAdapter {
private List<DefineClass> mlist;
private LayoutInflater inflater;
private LoadImageThread loadimage;
GuildAdapter(Context context,List<DefineClass> list) {
this.inflater = LayoutInflater.from(context);
this.mlist = list;
loadimage = new LoadImageThread();
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.guild_list, null);
<span style="color:#993399;">holder.picimg = (ImageView)convertView.findViewById(R.id.guildima);</span>
holder.nametxt = (TextView)convertView.findViewById(R.id.name);
holder.numbertxt = (TextView)convertView.findViewById(R.id.number);
holder.Notxt = (TextView)convertView.findViewById(R.id.No);
holder.introductiontxt = (TextView)convertView.findViewById(R.id.guildintroduction);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DefineClass bean = new DefineClass(); <span style="color:#993399;">一个DefineClass实体,主要利用</span><span style="color: rgb(153, 51, 153); font-family: Arial, Helvetica, sans-serif;">bean.picurl给</span><span style="color: rgb(153, 51, 153); font-family: Arial, Helvetica, sans-serif;">loadimage.ImageAsyncTask(holder.picimg, bean.picurl);传递url图片的网络地址</span><span style="color: rgb(153, 51, 153); font-family: Arial, Helvetica, sans-serif;">
</span><span style="color:#993399;">
</span> bean = mlist.get(position);
holder.picimg.setTag(bean.picurl);
loadimage.ImageAsyncTask(holder.picimg, bean.picurl); <span style="color:#993399;">调用上面的类<span style="font-family: Arial, Helvetica, sans-serif;">LoadImageThread 的ImageAsyncTask方法</span></span>
holder.nametxt.setText(bean.name);
holder.numbertxt.setText(bean.number);
holder.Notxt.setText(bean.No);
holder.introductiontxt.setText(bean.introduction);
return convertView;
}
public class ViewHolder {
TextView nametxt,introductiontxt,numbertxt,Notxt;
<span style="color:#993399;">ImageView picimg;</span>
}
}
很方便的就可以实现上面两个类合在一起的功能,当然还得需要afinal-master包 (Copyright (c) 2012-2013, Michael Yang 杨福海 (www.yangfuhai.com))
package com.example.logintest;
import java.util.List;
import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GuildAdapter extends BaseAdapter {
private List<DefineClass> mlist;
private LayoutInflater inflater;
// private LoadImageThread loadimage;
<span style="color:#993399;">private FinalBitmap fb;</span>
GuildAdapter(Context context,List<DefineClass> list) {
this.inflater = LayoutInflater.from(context);
this.mlist = list;
// loadimage = new LoadImageThread();
<span style="color:#993399;">this.fb = FinalBitmap.create(context);</span>
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.guild_list, null);
holder.picimg = (ImageView)convertView.findViewById(R.id.guildima);
holder.nametxt = (TextView)convertView.findViewById(R.id.name);
holder.numbertxt = (TextView)convertView.findViewById(R.id.number);
holder.Notxt = (TextView)convertView.findViewById(R.id.No);
holder.introductiontxt = (TextView)convertView.findViewById(R.id.guildintroduction);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DefineClass bean = new DefineClass();
bean = mlist.get(position);
holder.picimg.setTag(bean.picurl);
// loadimage.ImageAsyncTask(holder.picimg, bean.picurl);
<span style="color:#993399;">holder.picimg.setImageResource(R.drawable.def_grid_zz_list);
if(holder.picimg.getTag() != null && holder.picimg.getTag() == bean.picurl) {
fb.display(holder.picimg, bean.picurl);
}</span>
holder.nametxt.setText(bean.name);
holder.numbertxt.setText(bean.number);
holder.Notxt.setText(bean.No);
holder.introductiontxt.setText(bean.introduction);
return convertView;
}
public class ViewHolder {
TextView nametxt,introductiontxt,numbertxt,Notxt;
ImageView picimg;
}
}