The volley framework has also done a lot of work in requesting network images, providing several methods. This article describes how to use imageloader to load network images.
Imageloader is implemented using imagerequest internally. Its constructor can input an imagecache cache parameter to implement the image cache function. It can also filter duplicate links to avoid repeated requests.
The following is how imageloader loads images:
Public void displayimg (view) {imageview = (imageview) This. findviewbyid (R. id. image_view); requestqueue mqueue = volley. newrequestqueue (getapplicationcontext (); imageloader = new imageloader (mqueue, new bitmapcache (); imagelistener listener = imageloader. getimagelistener (imageview, R. drawable. default_image, R. drawable. default_image); imageloader. get ("http://developer.android.com/images/home/aw_dac.png", listener); // specifies the maximum width and height allowed by the image // imageloader. get ("http://developer.android.com/images/home/aw_dac.png", listener, 200,200 );}
Create an imagelistener instance using the imageloader. getimagelistener () method and add the listener and image URL to the imageloader. Get () method to load the network image.
The following is a cache class implemented using lrucache:
Public class bitmapcache implements imagecache {private lrucache <string, bitmap> cache; Public bitmapcache () {cache = new lrucache <string, bitmap> (8x1024*1024) {@ override protected int sizeof (string key, Bitmap bitmap) {return bitmap. getrowbytes () * bitmap. getheight () ;};}@ override public bitmap getbitmap (string URL) {return cache. get (URL) ;}@ override public void putbitmap (string URL, Bitmap bitmap) {cache. put (URL, bitmap );}}
Finally, do not forget to add the network access permission to the androidmanifest. xml file.
<Uses-Permission Android: Name = "android. Permission. Internet"/>
Sample source code:
Http://download.csdn.net/detail/gaolu/7815527
Use imageloader of Volley framework to load network images