Reprint Please specify source: http://blog.csdn.net/guolin_blog/article/details/17482165 Guo Lin
It's all a way to request pictures online.
A JSON string that can be practiced hand: http://api.androidhive.info/volley/person_object.jso
A picture that can be practiced hand http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg
1. Usage of imagerequest (also inherit request and same as them)
We have already learned the usage of stringrequest and jsonrequest, and have summed up their usage in a very similar way, basically the following three steps:
1. Create a Requestqueue object.
2. Create a Request object.
3. Add the Request object to the Requestqueue.
Of these,Stringrequest and Jsonrequest are inherited from request, so their usage will be so similar
Requestqueue Mqueue = volley.newrequestqueue (context);
The next step is to go to new Imagerequest (to accept 6 arguments) and the code is as follows:
Imagerequest imagerequest =NewImagerequest ("Http://developer.android.com/images/home/aw_dac.png",NewResponse.listener<bitmap>() {@Override Public voidOnresponse (Bitmap response) {ImageView. Setimagebitmap(response); } }, 0,0, config.rgb_565,NewResponse.errorlistener () {///Two 0 specifies the maximum width and height allowed for the picture. If the picture is larger than the maximum value here, the picture is compressed, and the specified 0 means no matter how large it is, it is not compressed. The fifth parameter to make a picture's color properties @Override Public voidonerrorresponse (volleyerror error) {Imageview.setimageresource (r.drawable.default_image); } });
As you can see, the Imagerequest constructor receives six parameters, the first parameter is the URL address of the picture, and there is nothing to explain. The second parameter is a successful callback for the picture request, where we set the returned bitmap parameter to ImageView. The third and fourth parameters are used to specify the maximum allowable width and height of the picture, if the specified network picture width or height is greater than the maximum value here, the picture will be compressed, specified as 0, no matter how large the picture, will not be compressed. The fifth parameter is used to specify the color properties of the picture, and several constants under Bitmap.config can be used here, where argb_8888 can show the best color properties , each pixel occupies a size of 4 bytes, and the RGB _565 indicates that each picture pixel occupies 2 byte size. The sixth parameter is a callback for the failed picture request, where we display a default picture in ImageView when the request fails.
Mqueue.add (imagerequest);
2. Imageloader usage (internal is also imagerequest, more funny, not only can cache the picture, but also can filter out the duplicate links, to avoid repeated sending requests)
since Imageloader is not inherited from request , its usage is different from what we have learned before, which can be summed up roughly in the following four steps:
1. Create a Requestqueue object.
2. Create a Imageloader object.
3. Get a Imagelistener object.
4. Call Imageloader's Get() method to load the picture on the network.
The second step is to start learning, create a new Imageloader object, and the code looks like this:
New New Imagecache() { //Picture caching @Overridepublicvoid Putbitmap ( String URL, Bitmap Bitmap) { } @Override public Bitmap getbitmap (string url) { return NULL ; } });
As you can see, the Imageloader constructor receives two parameters, the first parameter is the Requestqueue object, the second argument is a Imagecache object, and here we first new an empty Imagecache implementation.
Next you need to get a Imagelistener object, the code looks like this:
Imagelistener listener = Imageloader.getimagelistener (imageview,r.drawable.default_image, R.drawable.failed_ image);
We can get to a Imagelistener object by calling Imageloader's getimagelistener() method,Getimagelistener( Show picture of the ImageView control, load the picture displayed during the loading process, the picture displayed when the load fails ) method receives three parameters
Finally, call Imageloader's Get () method to load the picture, as shown in the following code:
Imageloader. Get ("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", listener);
The Get () method receives two parameters, the first parameter is the URL address of the picture, and the second parameter is the Imagelistener object that was just fetched. of course, if you want to limit the size of a picture, you can also use the Get () method's overload to specify the maximum allowable width and height for the picture, as shown below:
Imageloader. Get ("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg"200 );
Now run the program and start loading the image, you will see a default picture displayed in ImageView, and when the picture on the network is loaded, ImageView will automatically display the graph, as shown in the results.
Stage Two. Use the Imagecache cache (which is also a more powerful place for Imageloader)
In fact, writing a imagecache is also very simple, but if you want to write a very good performance Imagecache, it is best to use the LruCache features provided by Android, if you do not understand LruCache, can refer to one of my previous blog android efficient loading large map, multi-figure solution, effectively avoid the program Oom.
Here we create a new Bitmapcache and implement the Imagecache interface as follows:
Public classBitmapcache implements Imagecache {PrivateLrucache<string, bitmap>Mcache; Public Bitmapcache() {intMaxSize =Ten*1024x768*1024x768; Cache picture size set to 10M Mcache=NewLrucache<string, bitmap>(maxSize) {@Overrideprotected intsizeOf (String key, Bitmap Bitmap) {returnBitmap.getrowbytes () *bitmap.getheight (); } }; } @Override PublicBitmap getbitmap (String url) {returnMcache.Get(URL); } @Override Public voidputbitmap (String URL, Bitmap Bitmap) {mcache.put (URL, Bitmap); } }
As you can see, here we set the size of the cached image to 10M. Then modify the code that creates the Imageloader instance, and the second parameter passes to the instance of Bitmapcache as follows:
New New Bitmapcache ());
in this way we will make full use of the functional advantages of Imageloader.
3. Networkimageview usage (custom control, inheritance ImageView)
In addition to the above two methods, volley also provides a third way to load the network picture, that is, the use of Networkimageview. Unlike both of these, Networkimageview is a custom control that inherits from ImageView , has all the functions of the ImageView control , and joins the native base The ability to load network pictures . Networkimageview controls are easier to use than the first two, and can be broadly divided into the following five steps:
1. Create a Requestqueue object.
2. Create a Imageloader object.
3. Add a Networkimageview control to the layout file.
4. Get an instance of the control in your code.
5. Set the image address to be loaded.
The first and second steps are exactly the same as Imageloader, so here we start with the third step. First modify the code in the layout file and add the Networkimageview control inside it, as shown below:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send Request" /> <Com.android.volley.toolbox.NetworkImageViewAndroid:id= "@+id/network_image_view"Android:layout_width= "200DP"Android:layout_height= "200DP"android:layout_gravity= "Center_horizontal" /> </LinearLayout>
Then the activity gets to the instance of the control, which is very simple, and the code looks like this:
Networkimageview = (Networkimageview) Findviewbyid (R.id.network_image_view);
Once an instance of the Networkimageview control is obtained,
Networkimageview. Setdefaultimageresid (r.drawable.default_image); Sets the picture Networkimageview displayed in the load. Seterrorimageresid (r.drawable.failed_image); Load failed to display picture Networkimageview. Setimageurl ("Http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", Imageloader); URL address of the destination picture
where the Setimageurl () method receives two parameters, the first parameter specifies the URL address of the picture, and the second parameter is the Imageloader object created earlier. over
Then some friends may ask, using imagerequest and Imageloader both ways to load the network picture, you can pass in a maximum width and height of the parameters to compress the picture, The Networkimageview does not provide a way to set the maximum width and height, then is not the use of Networkimageview to load the picture will not be compressed it?
In fact, this is not the case, Networkimageview does not need to provide any method of setting the maximum width and height can also compress the loaded picture. This is because Networkimageview is a control that automatically acquires its own width when loading a picture, then compares the width of the network image and then decides whether or not to compress the image. In other words, the compression process is fully automated in the internal, do not need our concern, Networkimageview will always present to us a size just good network pictures, will not occupy any bit of memory, this is networkimageview the most simple and useful
Networkimageview Layout_width and Layout_height are set to wrap_content on the layout file, This networkimageview will show the original size of the image without any compression.
Android--volley Framework (ii)