首页 > 代码库 > 初识Volley

初识Volley

ImageLoader异步加载图片

技术分享
 1 /** 2      * 异步加载图片 3      */ 4     private void loadImageByVolley(){   5         String imageUrl="http://www.taopic.com/uploads/allimg/120421/107063-12042114025737.jpg";   6         RequestQueue requestQueue = Volley.newRequestQueue(this); 7          8         final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);   9         ImageCache imageCache = new ImageCache() { 10             11             @Override  12             public void putBitmap(String key, Bitmap value) {  13                 lruCache.put(key, value);  14             }  15   16             @Override  17             public Bitmap getBitmap(String key) {  18                 return lruCache.get(key);  19             }  20         };  21         ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache); 22         23         //三个参数分别是imageView组件,默认图片,加载失败图片24         ImageListener listener = ImageLoader.getImageListener(iv, R.drawable.ic_launcher,0);  25         imageLoader.get(imageUrl, listener);  26     } 
View Code

NetworkImageView显示网络图片

技术分享
 1 /** 2      * 使用NetworkImageView显示网络图片,代替传统的ImageView 3      */ 4     private void showImageByNetworkImageView(){   5         String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";   6         RequestQueue requestQueue = Volley.newRequestQueue(this);   7         final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);   8         ImageCache imageCache = new ImageCache() {   9             @Override  10             public void putBitmap(String key, Bitmap value) {  11                 lruCache.put(key, value);  12             }  13   14             @Override  15             public Bitmap getBitmap(String key) {  16                 return lruCache.get(key);  17             }  18         };  19         ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);   20         mNetworkImageView.setImageUrl(imageUrl,imageLoader);  21     }  
View Code

 

初识Volley