標籤:學習 roi view ati 預設 creat port url post
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() {@Overridepublic 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/18350601private class ImageCacheImpl implements ImageCache {private LruCache<String, Bitmap> mLruCache;public ImageCacheImpl() {int maxSize = 15 * 1024 * 1024;mLruCache = new LruCache<String, Bitmap>(maxSize) {@Overrideprotected int sizeOf(String key, Bitmap bitmap) {return bitmap.getRowBytes() * bitmap.getHeight();}};}@Overridepublic Bitmap getBitmap(String url) {return mLruCache.get(url);}@Overridepublic 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非同步載入資料完整具體示範範例(二)