一、适配器类中图片设置
ImageCacheTask imageCache = new ImageCacheTas(context);
final String imgCover = list.get(position).getCover();
vh.img_movie_item_listview.setTag(imgCover);
Bitmap bm = imageCache.loadImage(imgCover, new ImageCallche() {
@Override
public void onImageLoad(String imageUrl, Bitmap image) {
ImageView imageView = (ImageView) parent
.findViewWithTag(imgCover);
if (imageView != null && image != null) {
imageView.setImageBitmap(image);
}
}
});
if (bm != null) {
vh.img_movie_item_listview.setImageBitmap(bm);
} else {
vh.img_movie_item_listview.setImageResource(R.drawable.ic_launcher);
}
二、定义类 ImageCacheTask :
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
public class ImageCacheTask {
private HashMap<String, SoftReference<Bitmap>> imageCache;
private Context context;
public ImageCacheTask(Context context) {
this.context = context;
this.imageCache = new HashMap<String, SoftReference<Bitmap>>();
}
// 接口供回调
public interface ImageCallche {
void onImageLoad(String imageUrl, Bitmap image);
}
// 调用此方法先到缓存中找照片的bitmap对象是否存在,存在直接用(return回去),不存在则启动线程到服务器取,然后放到缓存中供下次使用
// callback对象是调用时创建的对象,传递过来,供若干时间后,到服务取得照片以后,还能够把此张照片对号入座到他所属的ImageView
public Bitmap loadImage(final String imgUrl, final ImageCallche callback) {
// 从缓存中使用图片
if (imageCache.containsKey(imgUrl)) {
Bitmap img = imageCache.get(imgUrl).get();
if (img != null) {
return img;
} else {
imageCache.remove(imgUrl);
}
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
callback.onImageLoad(imgUrl, (Bitmap) msg.obj);
break;
case 2:
Toast.makeText(context, "网络异常", 0).show();
break;
}
}
};
// 从服务器加载图片并使用
new Thread(new Runnable() {
@Override
public void run() {
if (isNetAvailable(context)) {
// 从服务器请求图片
Bitmap img = getImageFromUrl(imgUrl);
// 把图片放入缓存中
imageCache.put(imgUrl, new SoftReference<Bitmap>(img));
Message msg = handler.obtainMessage(1, img);
handler.sendMessage(msg);
} else {
handler.sendEmptyMessage(2);
}
}
}).start();
return null;
}
// 从服务器请求图片资源
public Bitmap getImageFromUrl(String imgUrl) {
URL url;
HttpURLConnection conn = null;
try {
url = new URL(imgUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
if (conn.getResponseCode() == 200) {
return BitmapFactory.decodeStream(conn.getInputStream());
} else {
Log.d("Bright", "服务器异常");
}
} catch (MalformedURLException e) {
Log.e("Bright", "" + e.getMessage());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
Log.e("Bright", "" + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("Bright", "" + e.getMessage());
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return null;
}
// 判断网络是否通畅
public boolean isNetAvailable(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connMgr.getActiveNetworkInfo();// 得到网络通信信息
if (netInfo == null || !netInfo.isAvailable()) {// 判断网络是否畅通
return false;
}
return true;
}
// 判断网络是否通畅
public boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
} else {
NetworkInfo[] info = cm.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}