When using Volley to retrieve images from the network, we introduced the use of ImageRequest. In fact, Volley also provides a NetworkImageView class. Using this class, we can get images from the network more efficiently, because it helps us set up another cache and help us to process the Request queue by ourselves.
The NetworkImageView class is used as follows:
1) define in XML:
<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>
This is equivalent to using a custom View. Here is com. android. volley. toolbox. NetworkImageView.
2) then use:
networkImageView = (NetworkImageView) findViewById(R.id.nivTestView);mQueue = Volley.newRequestQueue(this);LruImageCache lruImageCache = LruImageCache.instance();ImageLoader imageLoader = new ImageLoader(mQueue,lruImageCache);networkImageView.setDefaultImageResId(R.drawable.ic_launcher);networkImageView.setErrorImageResId(R.drawable.ic_launcher);networkImageView.setImageUrl(URLS[1], imageLoader);
Step 1: Create a RequestQueue.
Step 2: Create an ImageLoader.
ImageLoader is a class that really deals with networks and obtains images in the NetworkImageView class. In its constructor, we find that a class of the ImageCache interface needs to be implemented, that is, the above LruImageCache class is used as the memory cache class of ImageLoader, that is, level 1 cache (L1 ).
ImageCache is the internal interface of ImageLoader. Its definition is as follows:
public interface ImageCache { public Bitmap getBitmap(String url); public void putBitmap(String url, Bitmap bitmap); }
Therefore, before using NetworkImageView, we need to implement this interface first, and Volley recommends LruCache. The implementation of LruImageCache in 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.getRuntime().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);}}}
The LruCache is used as the cache, and then the getBitmap and putBitmap methods of ImageCache are implemented.
Then, the mQueue and LruImageCache created above are passed to the constructor as parameters, so that the ImageLoader object is created.
Step 3: Call the setImageUrl method of NetworkImageView. Here, pass the ImageLoader to it, and then click OK. But Volley recommends that you call the following two methods before setting the url:
/** * Sets the default image resource ID to be used for this view until the attempt to load it * completes. */ public void setDefaultImageResId(int defaultImage) { mDefaultImageId = defaultImage; } /** * Sets the error image resource ID to be used for this view in the event that the image * requested fails to load. */ public void setErrorImageResId(int errorImage) { mErrorImageId = errorImage; }
Therefore, before calling setImageUrl, we also call these two methods for two purposes:
1) Call setDefaultImageResId to set a default image display until the image load on the network is complete.
2) Call setErrorImageResId to set an incorrect image and display it when an error occurs during network loading.
Let's take a look:
In the first article, we used ImageRequest to obtain the network image. The figure below is a slightly larger one, which we used NetworkImageView to obtain.
Besides being defined in XML, we can also dynamically create them directly in Java and set their url addresses in Java. In fact, I think this is the main purpose of Java, it should be used to display a large number of network images, and the effect will be better.
For details about how to use ImageRequest to load images, refer to: Using Volley in Android (1) Loading Images