//绑定数据
MyImageAdpater myImageAdpater =new MyImageAdpater (list);
listView.setAdapter(myImageAdpater);
//自定义的MyImageAdpater 如下:
protected class MyImageAdpater extends BaseAdapter{
private List<Company> list;
public MyImageAdpater(List<Company> result) {
this.list=result;
} @Override
public int getCount() {
return (list==null?0:list.size());
} @Override
public Object getItem(int position) {
return (list==null? null : list.get(position));
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
View view=layoutInflater.inflate(R.layout.company_show_listview, null);
//图片显示 需要异步加载图片
ImageView imageView=(ImageView) view.findViewById(
R.id.CompanyShowImageViewID);
ImgeAsyncLoading imgeAsyncLoading=new ImgeAsyncLoading(); imgeAsyncLoading.loadingImg(imageUrl,imageView);
return view;
}
}ImgeAsyncLoading 异步加载图片的类:如下:
public class ImgeAsyncLoading {
private Handler handler;
private HashMap<String, SoftReference<Bitmap>> imageCache= new HashMap<String, SoftReference<Bitmap>>(); /**
* 异步加载图片方法
* @param imagePath 图片路径
* @param imageView ImageView控件
*/
public void loadingImg(final String imagePath,final ImageView imageView) {
// 检查SoftReference软引用是否存在对象
if (imageCache.containsKey(imagePath)) {
System.out.println("----检查软引用");
SoftReference<Bitmap> softReference = imageCache.get(imagePath);
Bitmap bitmap = softReference.get();
if (bitmap != null) {// 未被回收直接设置图片到控件上
imageView.setImageBitmap(bitmap);
return ;
}
}
//软引用为空 就去网络下载
handler = new Handler() {
//得到下载好的图片
@Override
public void handleMessage(Message msg) {
Bitmap bitmap=(Bitmap) msg.obj;
//放入到软引用中
imageCache.put(imagePath, new SoftReference<Bitmap>(bitmap));
//更新ui界面
imageView.setImageBitmap(bitmap);
}
};
//开启线程下载图片
ImageRunable imageRunable = new ImageRunable(imagePath);
Thread thread = new Thread(imageRunable);
thread.start();
} private class ImageRunable implements Runnable {
private String url; public ImageRunable(String path) {
this.url = path;
} @Override
public void run() {
HttpClient client = null;
try {
HttpGet httpGet = new HttpGet(url);
client = new DefaultHttpClient();
// 请求超时
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
// 读取超时
client.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 60000 * 3);
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);
inputStream.close();
}
} catch (Exception e) {
throw new AskHttpException(e);
} finally {
// 关闭连接
if (client != null) {
client.getConnectionManager().shutdown();
}
}
}
}
}这个有没错误啊,我怎么和网上不一样,网上用了回调函数,回调函数我搞不懂,就没用回调,不知道这样做对不对哦,图片是没有错误,但是我们测试的时候,为什么连System.out.println("----检查软引用");
这句话都没有输出来啊。是没有起作用吗?程序哪里错了吗?
Android图片Bitmapimageview
MyImageAdpater myImageAdpater =new MyImageAdpater (list);
listView.setAdapter(myImageAdpater);
//自定义的MyImageAdpater 如下:
protected class MyImageAdpater extends BaseAdapter{
private List<Company> list;
public MyImageAdpater(List<Company> result) {
this.list=result;
} @Override
public int getCount() {
return (list==null?0:list.size());
} @Override
public Object getItem(int position) {
return (list==null? null : list.get(position));
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
View view=layoutInflater.inflate(R.layout.company_show_listview, null);
//图片显示 需要异步加载图片
ImageView imageView=(ImageView) view.findViewById(
R.id.CompanyShowImageViewID);
ImgeAsyncLoading imgeAsyncLoading=new ImgeAsyncLoading(); imgeAsyncLoading.loadingImg(imageUrl,imageView);
return view;
}
}ImgeAsyncLoading 异步加载图片的类:如下:
public class ImgeAsyncLoading {
private Handler handler;
private HashMap<String, SoftReference<Bitmap>> imageCache= new HashMap<String, SoftReference<Bitmap>>(); /**
* 异步加载图片方法
* @param imagePath 图片路径
* @param imageView ImageView控件
*/
public void loadingImg(final String imagePath,final ImageView imageView) {
// 检查SoftReference软引用是否存在对象
if (imageCache.containsKey(imagePath)) {
System.out.println("----检查软引用");
SoftReference<Bitmap> softReference = imageCache.get(imagePath);
Bitmap bitmap = softReference.get();
if (bitmap != null) {// 未被回收直接设置图片到控件上
imageView.setImageBitmap(bitmap);
return ;
}
}
//软引用为空 就去网络下载
handler = new Handler() {
//得到下载好的图片
@Override
public void handleMessage(Message msg) {
Bitmap bitmap=(Bitmap) msg.obj;
//放入到软引用中
imageCache.put(imagePath, new SoftReference<Bitmap>(bitmap));
//更新ui界面
imageView.setImageBitmap(bitmap);
}
};
//开启线程下载图片
ImageRunable imageRunable = new ImageRunable(imagePath);
Thread thread = new Thread(imageRunable);
thread.start();
} private class ImageRunable implements Runnable {
private String url; public ImageRunable(String path) {
this.url = path;
} @Override
public void run() {
HttpClient client = null;
try {
HttpGet httpGet = new HttpGet(url);
client = new DefaultHttpClient();
// 请求超时
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
// 读取超时
client.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 60000 * 3);
HttpResponse response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);
inputStream.close();
}
} catch (Exception e) {
throw new AskHttpException(e);
} finally {
// 关闭连接
if (client != null) {
client.getConnectionManager().shutdown();
}
}
}
}
}这个有没错误啊,我怎么和网上不一样,网上用了回调函数,回调函数我搞不懂,就没用回调,不知道这样做对不对哦,图片是没有错误,但是我们测试的时候,为什么连System.out.println("----检查软引用");
这句话都没有输出来啊。是没有起作用吗?程序哪里错了吗?
Android图片Bitmapimageview
this.list=result;
notifyDataSetChanged();//加一句这个试试;
}
反正是没有输出过一次!