How to use Android new Internet volley (2)

Source: Internet
Author: User

If using volley to get a network picture is best done with the Networkimageview class provided by volley, it is easy to do the work, and he can set cache, LRU and SD card cache. Some of them are packed.

The specific use of the Networkimageview class is as follows:

    <framelayout        android:id= "@+id/flimagecontainer"        android:layout_width= "Match_parent"        android: layout_height= "Wrap_content"        android:layout_below= "@+id/gvimages"        android:layout_margin= "30DP" >        <com.android.volley.toolbox.networkimageview            android:id= "@+id/nivtestview"            android:layout_ Width= "100DP"            android:layout_height= "100DP" >        </com.android.volley.toolbox.NetworkImageView>    </FrameLayout>

  

Networkimageview = (Networkimageview) Findviewbyid (R.id.nivtestview);  Find the corresponding Networkimageviewmqueue = Volley.newrequestqueue (this);     As usual, generate lruimagecache Lruimagecache = Lruimagecache.instance ();  Newly added, this is a cache class that implements the Imagecache interface, using the LRUimageloader imageloader = new Imageloader (mqueue,lruimagecache);  //Major Internet access to picture work was handed to him Networkimageview.setdefaultimageresid (r.drawable.ic_launcher);  The default load Picture Networkimageview.seterrorimageresid (r.drawable.ic_launcher);//Wrong picture Networkimageview.setimageurl (URLS[1 ], Imageloader);

So before using Networkimageview, we need to implement this interface first, and volley recommended is LRUCache, the Lruimagecache implementation of the code is as follows:

package Com.lms.volleydemo;import Android.graphics.bitmap;import Android.support.v4.util.lrucache;import Com.android.volley.toolbox.imageloader.imagecache;public Class Lruimagecache implements Imagecache{private static lrucache<string, bitmap> mmemorycache;private static Lruimagecache lruimagecache;private Lruimagecache () {//Get the Max available memoryint maxmemory = (int) runtime.getrunti Me (). MaxMemory (); int cacheSize = Maxmemory/8;mmemorycache = new lrucache<string, bitmap> (cacheSize) {@ overrideprotected int sizeOf (String key, Bitmap Bitmap) {return bitmap.getrowbytes () * Bitmap.getheight ();}};} public static Lruimagecache instance () {if (Lruimagecache = = null) {Lruimagecache = new Lruimagecache ();} return Lruimagecache;} @Overridepublic Bitmap Getbitmap (String arg0) {return mmemorycache.get (arg0);} @Overridepublic void Putbitmap (String arg0, Bitmap arg1) {if (Getbitmap (arg0) = = null) {Mmemorycache.put (arg0, arg1);}}} 

  

2 and then it's all set.

Imageloader Imageloader = new Imageloader (Mqueue,lruimagecache);

This is detailed http://blog.csdn.net/linmiansheng/article/details/21293699

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.