1、volley
VolleyUtils.getLoader(context).get(datas.get(position), ImageLoader.getImageListener(
holder.iv, R.drawable.ic_launcher, android.R.drawable.ic_menu_delete));
2、NUtils
vHolder.coverImgView.setTag(imgPath);
if(imgPath.length()>10){
//存在图片路径
vHolder.coverImgView.setVisibility(View.VISIBLE);
//从SDCard读取,若不存在,再从网络中下载
Bitmap bitmap=ImageUtils.getImg(imgPath);
if(bitmap!=null){
vHolder.coverImgView.setImageBitmap(bitmap);
}else{
NUtils.get(NUtils.TYPE_IMG, imgPath, new NUtils.Callback() {
@Override
public void response(String url, byte[] bytes) {
// TODO 图片下载完成后,进行显示
ImageView imgView=(ImageView) parent.findViewWithTag(url);
if(imgView!=null){
imgView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}
}
@Override
public boolean isCancelled(String url) {
// TODO 判断是否取消下载图片
return parent.findViewWithTag(url)==null;
}
});
3、networkImageView
requestQueue = Volley.newRequestQueue(context);
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
// 参数 在没有重写 sizeOf的时候,代表 最多存10个
private LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(
20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
holder.iv.setImageUrl(datas.get(position), imageLoader);
4 ImageLoader
requestQueue = Volley.newRequestQueue(this);
// 1.1 ImageLoader 创建
// 参数1 : 用于实际的网络请求
// 参数2 : ImageCache 内存缓存
imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
// 参数 在没有重写 sizeOf的时候,代表 最多存10个
private LruCache<String, Bitmap> cache = new LruCache<>(10);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
imageLoader.get(
"https://www.baidu.com/img/bd_logo1.png",
ImageLoader.getImageListener(imageView, R.drawable.ic_launcher, android.R.drawable.ic_delete)
);
5xUtils
1 package com.example.networkvolley; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.ImageView; 11 12 import com.android.volley.RequestQueue; 13 import com.android.volley.toolbox.ImageLoader; 14 import com.android.volley.toolbox.NetworkImageView; 15 import com.example.utils.VolleyUtils; 16 import com.lidroid.xutils.BitmapUtils; 17 18 public class adapter extends BaseAdapter { 19 20 private RequestQueue requestQueue; 21 private ImageLoader imageLoader; 22 23 private Context context; 24 private List<String> datas; 25 26 private static BitmapUtils bitmapUtils; 27 28 public adapter(Context context, List<String> datas) { 29 this.context = context; 30 this.datas = datas; 31 if(bitmapUtils==null) 32 bitmapUtils=new BitmapUtils(context); 33 bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher); 34 } 35 36 @Override 37 public int getCount() { 38 // TODO Auto-generated method stub 39 int count = 0; 40 if (datas != null) 41 count = datas.size(); 42 return count; 43 } 44 45 @Override 46 public Object getItem(int position) { 47 // TODO Auto-generated method stub 48 return datas.get(position); 49 } 50 51 @Override 52 public long getItemId(int position) { 53 // TODO Auto-generated method stub 54 return position; 55 } 56 57 @Override 58 public View getView(int position, View convertView, ViewGroup parent) { 59 ViewHolder holder = null; 60 if (convertView == null) { 61 convertView = LayoutInflater.from(context).inflate(R.layout.item, 62 null); 63 holder = new ViewHolder(); 64 holder.iv = (ImageView) convertView 65 .findViewById(R.id.image_view); 66 convertView.setTag(holder); 67 } else 68 holder = (ViewHolder) convertView.getTag(); 69 //---------------1 70 /*requestQueue = Volley.newRequestQueue(context); 71 imageLoader = new ImageLoader(requestQueue, 72 new ImageLoader.ImageCache() { 73 74 // 参数 在没有重写 sizeOf的时候,代表 最多存10个 75 private LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>( 76 20); 77 78 @Override 79 public Bitmap getBitmap(String url) { 80 return cache.get(url); 81 } 82 83 @Override 84 public void putBitmap(String url, Bitmap bitmap) { 85 cache.put(url, bitmap); 86 } 87 88 });*/ 89 //--------------2 90 /*VolleyUtils.getLoader(context).get(datas.get(position), ImageLoader.getImageListener( 91 holder.iv, R.drawable.ic_launcher, android.R.drawable.ic_menu_delete));*/ 92 /*holder.iv.setImageUrl(datas.get(position), imageLoader);*/ 93 94 95 //---------------3 96 bitmapUtils.display(holder.iv, datas.get(position)); 97 98 99 return convertView; 100 } 101 102 class ViewHolder { 103 //NetworkImageView iv; 104 ImageView iv; 105 } 106 107 }
6、Picasso
Picasso.with(context).load(path).into(holder.iv);