Volley Network Framework complete analysis (actual combat article)

Source: Internet
Author: User

Well, today, with a waterfall streaming demo, use the volley framework to request a network image.

Objective:

We use Networkimageview to display images:
1, because the control can automatically manage the life cycle of the request, when the parent control detach or it moves out of the screen can automatically cancel the network request
2. The control will compress the image according to the Networkimageview width and height we set.
3, the control can set the default state of the picture displayed, you can set the request when the failure to display the picture
Also uses the Imageloader to load the picture, uses the Imagecache cache to prevent the program oom, because the volley automatically handles the hard disk cache, so it still can display the picture normally without the net condition

OK, let me take a look at the effect:

Because of the GIF upload size limit, I recorded the previous part, we can see from this part of the effect, when loading the image, by default, we will first display the default picture we set, and then load through the network, and then display the loaded image, because Imagecache automatically manages the memory cache, and Networkimageview automatically according to its own wide-height to compress the picture, so even if the number of pictures, picture size is relatively large can also be good to avoid oom.

Below we open the cache directory to look at the cached files:

Now we disconnect the network and look at the effect:

As you can see, in the absence of a network state, you can still load the picture, because it will be taken from the cache, namely:
In the absence of network conditions will automatically fetch the data from the cache, in the state of the network, the cache will be judged whether there is no such cache files, if any, no, then open the network request access, and then into the hard disk cache.

Well, the code will stick to the main one:
Recyclerview Adapter:

 Public classRecycleradapter extends Recyclerview.adapter<recyclerviewholder> {PrivateContext Mcontext;PrivateList<string> datas;PrivateImageloader Mimageloader;PrivateBitmapcache Mbitmapcache;PrivateList<integer> Heights; Public Recycleradapter(context context, list<string> datas) { This. Mcontext = Context; This. datas = datas;        Getrandomheights (); Mbitmapcache =NewBitmapcache (); Mimageloader =NewImageloader (Myapplication.getrequestqueue (), Mbitmapcache); }Private void getrandomheights() {Heights =NewArraylist<> (); for(inti =0; I < datas.size (); i++) {Heights.add (int) ( -+ math.random () * $)); }} @Override PublicRecyclerviewholderOncreateviewholder(ViewGroup parent,intViewType) {View view = Layoutinflater. from(Mcontext). Inflate (R.layout.layout_item, parent,false); Recyclerviewholder holder =NewRecyclerviewholder (view);returnHolder } @Override Public void Onbindviewholder(Recyclerviewholder holder,intPosition) {Viewgroup.layoutparamsparams= Holder.itemView.getLayoutParams ();params. Height = Heights.Get(position); Holder.itemView.setLayoutParams (params);        Holder.mNetworkImageView.setDefaultImageResId (R.MIPMAP.IC_CROP_ORIGINAL_BLACK_48DP);        Holder.mNetworkImageView.setErrorImageResId (R.MIPMAP.IC_FILTER_HDR_BLACK_48DP); HOLDER.MNETWORKIMAGEVIEW.SETIMAGEURL (datas.Get(position), mimageloader); } @Override Public int GetItemCount() {returnDatas.size (); }}

Bitmapcache Code:

 Public  class Bitmapcache implements Imageloader. Imagecache {    PrivateLrucache<string,bitmap> Mlrucache; Public Bitmapcache(){intMaxMemory = (int) Runtime.getruntime (). MaxMemory ();intMaxcachesize = maxmemory/8; Mlrucache =NewLrucache<string,bitmap> (maxcachesize) {@Override            protected int sizeOf(String key, Bitmap value) {returnValue.getbytecount ();    }        }; }@Override     PublicBitmapGetbitmap(String s) {returnMlrucache.get (s); }@Override     Public void Putbitmap(String s, Bitmap Bitmap)    {mlrucache.put (S,BITMAP); }}

Demo Please poke

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Reprint Annotated Source: http://blog.csdn.net/u010687392

Volley Network Framework complete analysis (actual combat article)

Related Article

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.