Android uses volley framework to load network images

Source: Internet
Author: User

The volley framework was released on Google I/O 2013 volley to enable network communication libraries on the Android platform to make network communications faster, simpler, and more robust. We also have a simple understanding ofvolley introduced in the background, before volley appeared,we may face problems such as loading a larger number of images in a ListView or a GridView: Firststart the image reading in Listadapter#getview (), thenusing HttpURLConnection to go from the server to the picture resource through asynctask and other mechanisms, and thenset the properties of the corresponding ImageView in Asynctask#onpostexecute (). This is not only cumbersome steps, and not thoughtful, but also may appear oom and other problems. Now, with volley, weonly one function is required to load the real network picture. Simply speaking, volley features are:

  • Asynchronous loading of JSON, images, etc.;
  • Ordering of network requests;
  • Priority processing of network requests;
  • Picture Cache;
  • Multi-level cancellation requests;
  • networkimageview:
  • First Way steps:
  • Create a Requestqueue object, in one sentence:requestqueue mqueue = volley.newrequestqueue (context);
  • Create a Imageloader object, Imageloader mimageloader= new Imageloader (Mrequestqueue, New Bitmaplrucache (cacheSize)); public class Bitmaplrucache extends Lrucache<string, bitmap> implements Imageloader.imagecache {
    Public Bitmaplrucache (int maxSize) {
    Super (MaxSize);
    }
    protected int sizeOf (String key, Bitmap Bitmap) {
    Return (Bitmap.getrowbytes () * bitmap.getheight ());
    }
    Public Bitmap getbitmap (String URL) {
    Return ((Bitmap) get (URL));
    }
    public void Putbitmap (String url, Bitmap Bitmap) {
    Put (URL, bitmap);
    }
    }*** creating parameters in the ImageloaderCacheSize is the maximum number of cache files we have set, such as the ability to setint cacheSize=Ten * 1024x768 * 1024x768;
  • create Imagelistener listener = Imageloader.getimagelistener (ImageView, r.drawable.default_image, r.drawable.failed_image); The//imageview parameter is the control we want to show the picture to
  • then you can load the picture: imageloader.get ( " http://www.xxxxxxx.jpg " , listener);//the incoming image URL and the listener created above will be OK.
  • The second way is to use volley encapsulated networkimageview loading pictures.
  • This way is simpler, networkimageview as our usual imageview put in the layout XML we want to load the picture, in the corresponding code with networkimageview imageview= Findviewbyid (); Find control,
  • Networkimageview.setdefaultimageresid (R.drawable.default_icon);// load the picture shown in
    Networkimageview.seterrorimageresid (R.drawable.failed_icon);// the picture displayed when loading fails
    Networkimageview.setimageurl ("Http://xxxxxxx.jpeg", imageloader);// URL address of the target picture (the first parameter is used to specify the URL address of the picture, The second parameter is the Imageloader object created above ).

Android uses volley framework to load network images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.