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