上一篇文章中写了如何用volley的imageRequest进行网络图片的加载,这一篇文章介绍如何用volley的imageLoader加载网络图片,实际上imageloader里包含了imageRequest加载图片的方法,并且添加了图片缓存机制,下面通过listview中加载网络图片的实例来介绍如何使用:
1.和imageRequest一样需要创建MyApplication:
public class MyApplication extends Application{
public static RequestQueue queues;
@Override
public void onCreate() {
super.onCreate();
<strong> /*创建volley请求队列queues*/
queues = Volley.newRequestQueue(getApplicationContext());
}
/*创建获取请求队列方法getHttpQueues()*/
public static RequestQueue getHttpQueues(){
return queues;</strong>
}
在manifest中注册MyApplication
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".MyApplication"
android:theme="@style/AppTheme" >
2.创建MainActivity,创建listview并且绑定适配器
<pre name="code" class="java">public class MainActivity extends Activity implements ViewPager.OnPageChangeListener {
private ListView mGoodsPictureListView; //商品listview控件
private GoodsPictureListAdapter mGoodsPictureListAdapter; //商品list适配器
private String[] mListViewUrls;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListViewHeader = getLayoutInflater().inflate(R.layout.activity_main_listview_header, null);
/*初始化图片listview和图片list资源数组*/
mGoodsPictureListView = (ListView) findViewById(R.id.goods_picture_listview);
/*创建和绑定适配器*/
mListViewUrls = new String[]{"http://img1.3lian.com/img13/c2/14/31.jpg","http://img4.imgtn.bdimg.com/it/u=4165359378,1174563998&fm=21&gp=0.jpg","http://img0.imgtn.bdimg.com/it/u=778619511,4035858907&fm=21&gp=0.jpg","http://img01.taopic.com/150417/240415-15041FPH510.jpg","http://img0.imgtn.bdimg.com/it/u=4084186897,3669281913&fm=15&gp=0.jpg"};
mGoodsPictureListAdapter = new GoodsPictureListAdapter(this,mListViewUrls);
mGoodsPictureListView.setAdapter(mGoodsPictureListAdapter);
3.创建GoodPictureListAdapter适配器
public class GoodsPictureListAdapter extends BaseAdapter {
Context context;
private String[] mUrls;
public GoodsPictureListAdapter(Context context,String[] mUrls){
this.context = context;
/*网络获取图片urls地址数组*/
this.mUrls = mUrls;
}
@Override
public int getCount() {
return mUrls.length*3;
}
@Override
public Object getItem(int position) {
return mUrls[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder ;
final NetworkImageView imageView;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.goods_picture_listitem,null);
viewHolder.imageView = (NetworkImageView) convertView.findViewById(R.id.goods_picture_listitem);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
imageView = viewHolder.imageView;
<strong> /*创建imageloader对象,第一个参数为请求队列,第二个参数为bitmapCache缓存对象*/
ImageLoader mLoader = new ImageLoader(MyApplication.getHttpQueues(),new BitmapCache());
/*给imageview设置默认图片及加载失败的图片*/
imageView.setDefaultImageResId(R.drawable.ic_launcher);
imageView.setErrorImageResId(R.drawable.error);
/*给imageview设置网络图片,第一个参数为图片url地址,第二个参数是imageloader对象*/
imageView.setImageUrl(mUrls[position % 5], mLoader);
/*给imageview设置缩放类型,CENTER_CROP表示填充屏幕宽(高),另外一边等比拉伸*/
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);</strong>
/*给imageview设置补间动画*/
Animation layoutAnimation = AnimationUtils.loadAnimation(context,R.anim.listview_fade_scale_out);
imageView.startAnimation(layoutAnimation);
return convertView;
}
class ViewHolder{
<strong>NetworkImageView imageView;</strong>
}
}
在如上代码中,我们看到imageview不是传统的imageview,实际上用的是volley的NetworkImageView,在goods_picture_listitem.xml的布局文件中也必须引用NetworkImageView
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/goods_picture_listitem"
android:layout_width="match_parent"
android:layout_height="150dp" />
4.在创建imageloader对象的时候,传入的第二个参数是图片缓存对象实例,由于imageCache是一个接口不能创建对象,所以需创建一个BitmapCache来实现该imageCache接口:
public class BitmapCache implements ImageLoader.ImageCache {
public LruCache<String,Bitmap> cache;
public int max = 10*1024*1024;
public BitmapCache(){
cache = new LruCache<String,Bitmap>(max){
@Override
protected int sizeOf(String key, Bitmap value){
return value.getRowBytes()*value.getHeight();
}
};
};
@Override
public Bitmap getBitmap(String s) {
return cache.get(s);
};
@Override
public void putBitmap(String s, Bitmap bitmap) {
cache.put(s,bitmap);
}
}