Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android利用Volley異步加載數據完整詳細示例(二)

Android利用Volley異步加載數據完整詳細示例(二)

編輯:關於Android編程

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 網絡權限
 * 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() {
			  @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 mLruCache;
		public ImageCacheImpl() {
			int maxSize = 15 * 1024 * 1024;
			mLruCache = new LruCache(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如下:




    
    
    
    
    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved