首先做出全局的请求队列
1 package com.qg.lizhanqi.myvolleydemo; 2 3 import android.app.Application; 4 5 import com.android.volley.RequestQueue; 6 import com.android.volley.toolbox.HttpStack; 7 import com.android.volley.toolbox.Volley; 8 /** 9 * Created by lizhanqi on 2016-7-27-0027. 10 */ 11 12 /** 13 * 这是一个本应用全局的Volley请求队列,所以这里继承了Application 14 * 由于这是一个自定义的全局的application,所以在清单文件application中加入属性 15 * android:name=".MyApplication" 16 */ 17 public class MyApplication extends Application { 18 public static RequestQueue queues; 19 20 @Override 21 public void onCreate() { 22 super.onCreate(); 23 //实例化全局的请求队列 24 queues = Volley.newRequestQueue(getApplicationContext(), (HttpStack) null); 25 } 26 public static RequestQueue getHttpQueues() { 27 return queues; 28 } 29 }
接着做出Lru缓存图片的类
1 package com.qg.lizhanqi.myvolleydemo; 2 3 import android.graphics.Bitmap; 4 import android.util.LruCache; 5 6 import com.android.volley.toolbox.ImageLoader; 7 8 /** 9 * Created by lizhanqi on 2016-7-27-0027. 10 */ 11 public class BitmapCache implements ImageLoader.ImageCache {//这里实现它的主要目的是volley需要一个这样类型的缓存方式,所以是继承了它然后搭配Lru一起实现缓存 12 public LruCache<String,Bitmap> lruCache; 13 int maxMomory = 10*1024*1024;//最大内存超过10M,启动内存回收或者使用 Runtime.getRuntime().maxMemory()/4代替; 14 15 16 public BitmapCache() { 17 lruCache=new LruCache<String,Bitmap>(maxMomory){// maxMomory可以使用Runtime.getRuntime().maxMemory()/4代替; 18 @Override 19 protected int sizeOf(String key, Bitmap value) { 20 21 //这里应该返回的是一个图片的大小 22 return value.getRowBytes()*value.getHeight();//或者value.getByteCount(); 23 } 24 }; 25 } 26 @Override 27 public Bitmap getBitmap(String s) { 28 return lruCache.get(s); 29 } 30 31 @Override 32 public void putBitmap(String s, Bitmap bitmap) { 33 lruCache.put(s,bitmap); 34 } 35 }
Main使用的方式
1 package com.qg.lizhanqi.myvolleydemo; 2 import android.graphics.Bitmap; 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.ImageView; 8 import com.android.volley.Response; 9 import com.android.volley.VolleyError; 10 import com.android.volley.toolbox.ImageLoader; 11 import com.android.volley.toolbox.ImageRequest; 12 import com.android.volley.toolbox.NetworkImageView; 13 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 14 private static final String TAG = "MainActivity"; 15 private Button VolleyGetString; 16 private Button VolleyPostString; 17 private Button VolleyGetJsonObject; 18 private Button VolleyGetImage; 19 private Button VolleyLruCacheGetImage; 20 private NetworkImageView networkImageView; 21 private ImageView imageView; 22 private Button loadnetworkimageview; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 imageView = (ImageView) findViewById(R.id.imageView); 29 VolleyGetImage = (Button) findViewById(R.id.VolleyGetImage); 30 VolleyLruCacheGetImage = (Button) findViewById(R.id.VolleyLruCacheGetImage); 31 networkImageView = (NetworkImageView) findViewById(R.id.networkimageview); 32 loadnetworkimageview = (Button) findViewById(R.id.loadnetworkimageview); 33 34 loadnetworkimageview.setOnClickListener(this); 35 VolleyGetImage.setOnClickListener(this); 36 VolleyLruCacheGetImage.setOnClickListener(this); 37 } 38 39 @Override 40 public void onClick(View v) { 41 switch (v.getId()) { 42 case R.id.VolleyGetImage: 43 volleyNormalGetImage("http://p1.qhimg.com/t0151320b1d0fc50be8.png"); 44 break; 45 case R.id.VolleyLruCacheGetImage: 46 voleyLruCacheGetImage("http://p1.qhimg.com/t0151320b1d0fc50be8.png"); 47 break; 48 case R.id.loadnetworkimageview: 49 voleyLoadNetWorkImage("http://p1.qhimg.com/t0151320b1d0fc50be8.png"); 50 break; 51 } 52 } 53 54 //一般的加载 55 private void volleyNormalGetImage(String url) { 56 /** 57 * ImageRequest(String url, Listener<Bitmap> listener, int maxWidth, int maxHeight, Config decodeConfig, ErrorListener errorListener) 58 * String图片请求网址 59 * Listener<Bitmap> 请求成功回调的监听 60 * int maxWidth int maxHeight对于图片压缩的大小,填写数字,如果0,代表原图大小 61 * Config decodeConfig 图片格式的设置 62 * ErrorListener errorListener 请求错误的回调 63 */ 64 ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() { 65 @Override 66 public void onResponse(Bitmap bitmap) { 67 imageView.setImageBitmap(bitmap); 68 } 69 }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() { 70 @Override 71 public void onErrorResponse(VolleyError volleyError) { 72 } 73 }); 74 imageRequest.setTag("volleyNormalGetImage"); 75 MyApplication.getHttpQueues().add(imageRequest); 76 } 77 //带有缓存的加载 78 private void voleyLruCacheGetImage(String url) { 79 //(RequestQueue queue, ImageLoader.ImageCache imageCache),请求的队列,以及缓存 80 ImageLoader imageLoader = new ImageLoader(MyApplication.getHttpQueues(), new BitmapCache()); 81 //getImageListener(Imageview,defaultimage,errorimage) 82 ImageLoader.ImageListener imageListener = imageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher); 83 imageLoader.get(url, imageListener); 84 } 85 //volleyNetWorkImageView控件的加载 86 private void voleyLoadNetWorkImage(String url) { 87 //(RequestQueue queue, ImageLoader.ImageCache imageCache),请求的队列,以及缓存 88 ImageLoader imageLoader = new ImageLoader(MyApplication.getHttpQueues(), new BitmapCache()); 89 networkImageView.setDefaultImageResId(R.mipmap.ic_launcher); 90 networkImageView.setErrorImageResId(R.mipmap.ic_launcher); 91 networkImageView.setImageUrl(url,imageLoader); 92 } 93 94 95 }
//XML布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.qg.lizhanqi.myvolleydemo.MainActivity"> <Button android:text="普通图片请求" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/VolleyGetImage" android:layout_gravity="center_vertical" /> <Button android:text="LRU缓存图片请求" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/VolleyLruCacheGetImage" android:layout_gravity="center_vertical" /> <Button android:text="加载networkimagview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/loadnetworkimageview" android:layout_gravity="center_vertical" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:id="@+id/imageView" android:layout_gravity="center_vertical" /> <com.android.volley.toolbox.NetworkImageView android:src="@mipmap/ic_launcher" android:id="@+id/networkimageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>