Volley加载网络图片

今天介绍如何使用Volley加载网络图片:

理论知识介绍:

(1)缓存功能简单介绍:

LruCache,和ImageCache实现图片的缓存


(2)加载网络图片及监听

ImageRequest

ImageLoader

NatWorkImageView(显示网络图片)

我们从简单到复杂,先实现一个不带缓存的加载:

package com.example.volley;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class VolleyImageActivity extends Activity {

	private ImageView imageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_volley_image);
		imageView = (ImageView) findViewById(R.id.myImage);
		String url = "http://p2.so.qhimg.com/bdm/326_204_/t01b6d53e067d888f92.jpg";
		//参数依次代表:图片请求成功回调,图片加载允许的最大高度和宽度并压缩处理,如果设置为0原图显示不压缩,加载格式,请求失败的回调
		ImageRequest request = new ImageRequest(url, new Listener<Bitmap>() {

			@Override
			public void onResponse(Bitmap response) {
				// TODO Auto-generated method stub
				imageView.setImageBitmap(response);//加载成功显示网络图片
			}
		},0 , 0, Config.RGB_565, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				imageView.setBackgroundResource(R.drawable.ic_launcher);//加载失败,显示加载失败的图片
			}
		});

		request.setTag("mytag");//给请求设置标签,方便加入队列后查找
		MyApplication.getHttpRequestQueues().add(request);//添加到请求队列

	}
}

下面使用ImageLoader imageCache,LruCache实现带缓存的加载:

先创建一个图片缓存类:

package com.example.volley;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;
/**
 *实现图片 缓存
 * @author Administrator
 *
 */
public class BitmapCache implements ImageCache{
/**
 * ImageCache单独使用不能起到缓存效果需要使用LruCache配合
 */
	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) {
				// TODO Auto-generated method stub
				return value.getRowBytes()*value.getHeight();
			}
		};
	}
	@Override
	public Bitmap getBitmap(String url) {
		// TODO Auto-generated method stub
		return cache.get(url);
	}

	@Override
	public void putBitmap(String url, Bitmap bitmap) {
		// TODO Auto-generated method stub
		cache.put(url, bitmap);
	}

}


//方式二imageLoader加载方式
	private void imageLoaderImage(){
		String url = "http://p2.so.qhimg.com/bdm/326_204_/t01b6d53e067d888f92.jpg";
		ImageLoader loader = new ImageLoader(MyApplication.getHttpRequestQueues(), new BitmapCache());

		ImageListener listener = ImageLoader.getImageListener(imageView, R.drawable.ic_launcher, R.drawable.ic_launcher);//参数:控件,默认图片,出错图片
		loader.get(url, listener);
		//loader.get(url, listener, 0, 0);//后两个参数图片加载允许的最大高度和宽度并压缩处理,如果设置为0原图显示不压缩

	}
下面使用NetWorkImageVIew 显示图片
//方式三使用NetWorkImageView显示加载图片
	private void netWorkImageView(){
		String url = "http://p2.so.qhimg.com/bdm/326_204_/t01b6d53e067d888f92.jpg";
		ImageLoader loader = new ImageLoader(MyApplication.getHttpRequestQueues(), new BitmapCache());
		networkImageView.setDefaultImageResId(R.drawable.ic_launcher);//设置默认显示图片
		networkImageView.setErrorImageResId(R.drawable.ic_launcher);//设置加载失败显示图片
		networkImageView.setImageUrl(url, loader);
	}

下面给出三种方法的全部代码:(缓存类代码上面已经给出这里不再粘贴了)

package com.example.volley;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.NetworkImageView;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class VolleyImageActivity extends Activity {

	private ImageView imageView;
	private NetworkImageView networkImageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_volley_image);
		imageView = (ImageView) findViewById(R.id.myImage);
		networkImageView = (NetworkImageView) findViewById(R.id.networkImageview);

		//imageResponseImage();
		//imageLoaderImage();
		netWorkImageView();

	}
	//方式一:ImageResponseImage(不带缓存的加载方式)
	private  void imageResponseImage(){
		//参数依次代表:图片请求成功回调,图片加载允许的最大高度和宽度并压缩处理,如果设置为0原图显示不压缩,加载格式,请求失败的回调
		String url = "http://p2.so.qhimg.com/bdm/326_204_/t01b6d53e067d888f92.jpg";
		ImageRequest request = new ImageRequest(url, new Listener<Bitmap>() {

			@Override
			public void onResponse(Bitmap response) {
				// TODO Auto-generated method stub
				imageView.setImageBitmap(response);//加载成功显示网络图片
			}
		},0 , 0, Config.RGB_565, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				imageView.setBackgroundResource(R.drawable.ic_launcher);//加载失败,显示加载失败的图片
			}
		});

		request.setTag("mytag");//给请求设置标签,方便加入队列后查找
		MyApplication.getHttpRequestQueues().add(request);//添加到请求队列
	}
	//方式二imageLoader加载方式
	private void imageLoaderImage(){
		String url = "http://p2.so.qhimg.com/bdm/326_204_/t01b6d53e067d888f92.jpg";
		ImageLoader loader = new ImageLoader(MyApplication.getHttpRequestQueues(), new BitmapCache());

		ImageListener listener = ImageLoader.getImageListener(imageView, R.drawable.ic_launcher, R.drawable.ic_launcher);//参数:控件,默认图片,出错图片
		loader.get(url, listener);
		//loader.get(url, listener, 0, 0);//后两个参数图片加载允许的最大高度和宽度并压缩处理,如果设置为0原图显示不压缩

	}
	//方式三使用NetWorkImageView显示加载图片
	private void netWorkImageView(){
		String url = "http://p2.so.qhimg.com/bdm/326_204_/t01b6d53e067d888f92.jpg";
		ImageLoader loader = new ImageLoader(MyApplication.getHttpRequestQueues(), new BitmapCache());
		networkImageView.setDefaultImageResId(R.drawable.ic_launcher);//设置默认显示图片
		networkImageView.setErrorImageResId(R.drawable.ic_launcher);//设置加载失败显示图片
		networkImageView.setImageUrl(url, loader);
	}

}



你可能感兴趣的:(Volley加载网络图片)