ImageViewmImageView;Stringurl="http://i.imgur.com/7spzG.png";mImageView=(ImageView)findViewById(R.id.myImage);...// Retrieves an image specified by the URL, displays it in the UI.ImageRequestrequest=newImageRequest(url,newResponse.Listener(){@OverridepublicvoidonResponse(Bitmapbitmap){mImageView.setImageBitmap(bitmap);}},0,0,null,newResponse.ErrorListener(){publicvoidonErrorResponse(VolleyErrorerror){mImageView.setImageResource(R.drawable.image_load_error);}});// Access the RequestQueue through your singleton class.MySingleton.getInstance(this).addToRequestQueue(request);
ImageLoadermImageLoader;ImageViewmImageView;// The URL for the image that is being loaded.privatestaticfinalStringIMAGE_URL="http://developer.android.com/images/training/system-ui.png";...mImageView=(ImageView)findViewById(R.id.regularImageView);// Get the ImageLoader through your singleton class.mImageLoader=MySingleton.getInstance(this).getImageLoader();mImageLoader.get(IMAGE_URL,ImageLoader.getImageListener(mImageView,R.drawable.def_image,R.drawable.err_image));
ImageLoadermImageLoader;NetworkImageViewmNetworkImageView;privatestaticfinalStringIMAGE_URL="http://developer.android.com/images/training/system-ui.png";...// Get the NetworkImageView that will display the image.mNetworkImageView=(NetworkImageView)findViewById(R.id.networkImageView);// Get the ImageLoader through your singleton class.mImageLoader=MySingleton.getInstance(this).getImageLoader();// Set the URL of the image that should be loaded into this view, and// specify the ImageLoader that will be used to make the request.mNetworkImageView.setImageUrl(IMAGE_URL,mImageLoader);
Volley工具箱中提供了通过DiskBasedCache实现的一种标准缓存。这个类能够缓存文件到磁盘的制定目录。但是为了使用ImageLoader,你应该提供一个自定义的内存LRC缓存,这个缓存需要实现ImageLoader.ImageCache的接口。你可能想把你的缓存设置成一个单例。关于更多的有关内容,请参考建立请求队列Setting Up a RequestQueue.
importandroid.graphics.Bitmap;importandroid.support.v4.util.LruCache;importandroid.util.DisplayMetrics;importcom.android.volley.toolbox.ImageLoader.ImageCache;publicclassLruBitmapCacheextendsLruCache<String,Bitmap>implementsImageCache{publicLruBitmapCache(intmaxSize){super(maxSize);}publicLruBitmapCache(Contextctx){this(getCacheSize(ctx));}@OverrideprotectedintsizeOf(Stringkey,Bitmapvalue){returnvalue.getRowBytes()*value.getHeight();}@OverridepublicBitmapgetBitmap(Stringurl){returnget(url);}@OverridepublicvoidputBitmap(Stringurl,Bitmapbitmap){put(url,bitmap);}// Returns a cache size equal to approximately three screens worth of images.publicstaticintgetCacheSize(Contextctx){finalDisplayMetricsdisplayMetrics=ctx.getResources().getDisplayMetrics();finalintscreenWidth=displayMetrics.widthPixels;finalintscreenHeight=displayMetrics.heightPixels;// 4 bytes per pixelfinalintscreenBytes=screenWidth*screenHeight*4;returnscreenBytes*3;}}
下面是如何初始化ImageLoader并使用cache的实例:
12
RequestQueuemRequestQueue;// assume this exists.ImageLoadermImageLoader=newImageLoader(mRequestQueue,newLruBitmapCache(LruBitmapCache.getCacheSize()));
TextViewmTxtDisplay;ImageViewmImageView;mTxtDisplay=(TextView)findViewById(R.id.txtDisplay);Stringurl="http://my-json-feed";JsonObjectRequestjsObjRequest=newJsonObjectRequest(Request.Method.GET,url,null,newResponse.Listener(){@OverridepublicvoidonResponse(JSONObjectresponse){mTxtDisplay.setText("Response: "+response.toString());}},newResponse.ErrorListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){// TODO Auto-generated method stub}});// Access the RequestQueue through your singleton class.MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);