码迷,mamicode.com
首页 > 移动开发 > 详细

Android利用Volley异步加载数据完整详细示例(二)

时间:2014-05-18 03:54:44      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:android   blog   class   code   c   java   

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>


Android利用Volley异步加载数据完整详细示例(二),布布扣,bubuko.com

Android利用Volley异步加载数据完整详细示例(二)

标签:android   blog   class   code   c   java   

原文地址:http://blog.csdn.net/lfdfhl/article/details/26007551

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!