MainActivity例如以下:
package cc.y;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.util.LruCache;
import android.widget.ImageView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
/**
 * Demo描写叙述:
 * 去年一个项目中简单的使用了Volley,參见:
 * http://blog.csdn.net/lfdfhl/article/details/12223345
 * 近期guolin写了几篇文章具体介绍Volley,所以在此比較系统地学习一下.
 * 在本演示样例中,主要包含:
 * 1 利用ImageRequest为ImageView载入网络图片
 * 2 利用ImageLoader为ImageView载入网络图片
 * 3 利用NetworkImageView为ImageView载入网络图片

 * 
 * 注意事项:
 * 1 引入jar包volley.jar
 * 2 网络权限<uses-permission android:name="android.permission.INTERNET"/>
 * 3 Volley源代码分析,请參见:
 *   http://blog.csdn.net/guolin_blog/article/details/17656437
 * 4 自己定义的XMLRequest和GsonRequest,请參见:
 *   http://blog.csdn.net/guolin_blog/article/details/17612763
 * 
 * 学习资料:
 * http://blog.csdn.net/guolin_blog/article/details/17482165
 * Thank you very much
 *
 */
public class MainActivity extends Activity {
	private Context mContext;
	private RequestQueue mRequestQueue;
	private String mUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
	private ImageView mImageView;
	private ImageRequest mImageRequest;
	private ImageLoader mImageLoader;
	private ImageListener mImageListener;
	private NetworkImageView mNetworkImageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        //volley_ImageRequest();
        //volley_ImageLoader();
        volley_NetworkImageView();
    }
    
    private void init(){
    	mContext=this;
    	mImageView=(ImageView) findViewById(R.id.imageView);
    	mNetworkImageView=(NetworkImageView) findViewById(R.id.networkImageView);
    }
    
    //利用ImageRequest为ImageView载入网络图片
	private void volley_ImageRequest() {
		mRequestQueue = Volley.newRequestQueue(mContext);
		mImageRequest = new ImageRequest(
			mUrl, 
			new Response.Listener<Bitmap>() {
			  @Override
			  public void onResponse(Bitmap response) {
				mImageView.setImageBitmap(response);
			}}, 
		   0, 
		   0, 
		   Config.ARGB_8888, 
		   new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				System.out.println("获取图片失败,所以载入一个默认图片");
				mImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
			}
		});
		mRequestQueue.add(mImageRequest);
	}
	
	
	
	
	
	//利用ImageLoader为ImageView载入网络图片
	private void volley_ImageLoader(){
		mRequestQueue = Volley.newRequestQueue(mContext);
		mImageLoader=new ImageLoader(mRequestQueue, new ImageCacheImpl());
		mImageListener=ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher, R.drawable.ic_launcher);
		mImageLoader.get(mUrl, mImageListener);
		//mImageLoader.get(mUrl, mImageListener,250,250);
	}
	
	//利用LruCache实现图片缓存机制
	//请參见:
	//http://blog.csdn.net/lfdfhl/article/details/18350601
	private class ImageCacheImpl implements ImageCache {
		private LruCache<String, Bitmap> mLruCache;
		public ImageCacheImpl() {
			int maxSize = 15 * 1024 * 1024;
			mLruCache = new LruCache<String, Bitmap>(maxSize) {
				@Override
				protected int sizeOf(String key, Bitmap bitmap) {
					return bitmap.getRowBytes() * bitmap.getHeight();
				}
			};
		}

		@Override
		public Bitmap getBitmap(String url) {
			return mLruCache.get(url);
		}
		@Override
		public void putBitmap(String url, Bitmap bitmap) {
			mLruCache.put(url, bitmap);
		}
	}
	
	
	
	
	
	//利用NetworkImageView为ImageView载入网络图片
	private void volley_NetworkImageView(){
		mRequestQueue = Volley.newRequestQueue(mContext);
		mImageLoader=new ImageLoader(mRequestQueue, new ImageCacheImpl());
		mNetworkImageView.setDefaultImageResId(R.drawable.ic_launcher);
		mNetworkImageView.setErrorImageResId(R.drawable.ic_launcher);
		mNetworkImageView.setImageUrl(mUrl, mImageLoader);
	}
	
}

main.xml例如以下:

<?xml version="1.0" encoding="utf-8"?

> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学习Volley(二)" android:layout_centerHorizontal="true" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dip" /> <com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="120dip" android:layout_height="120dip" android:layout_centerHorizontal="true" android:layout_marginTop="150dip" /> </RelativeLayout>



posted on 2017-07-10 12:02  yutingliuyl  阅读(169)  评论(0编辑  收藏  举报