Kjframeforandroid Framework Learning----setting up network images efficiently

Source: Internet
Author: User

Kjframeforandroid Framework Project Address: https://github.com/kymjs/KJFrameForAndroid

or alternate address http://git.oschina.net/kymjs/KJFrameForAndroid

Kjframeforandroid Development Group: 257053751

As we all know, when the computer reads the data: The memory read speed is the fastest, then the file reads the speed, finally is the network resource reads.

It's too expensive to get the same picture every time you load it from the Web. So the same picture is enough to get once from the network, then cached locally and then loaded from the cache when the same picture is loaded.
Reading pictures from the memory cache is the fastest, but because Android has a limit on the amount of memory each app can use, it's a good idea to add a file cache. The file cache space is not infinite, the larger the capacity, the lower the reading efficiency, this is a good understanding, from the desert to find a missing needle and a needle from the plate, which is easy to know. So we often set a limited size such as 10M.

So, the process of loading the picture should be:
1, first from the memory cache, take the return, not to take the next step;
2, from the file cache, take the return and update to the memory cache, the next step is not taken;
3, download pictures from the network, and update to the memory cache and file cache.

If you only want to know that the file cache is common to the memory cache, check the next blog post.

In the past, we often used an implementation of a very popular memory caching technique, either soft or weak (SoftReference or weakreference). But according to Google's description: This is no longer recommended because, starting with Android 2.3 (API Level 9), the garbage collector is more inclined to reclaim objects holding soft or weak references, which makes soft and weak references less reliable. In addition, in Android 3.0 (API level 11), image data is stored in local memory, so it cannot be released in a predictable way, which poses a potential risk of memory overflow and crash of the application.

Therefore, we are more to use the LRU algorithm (Least recently used least recently used algorithm) originally this algorithm is used in the operating system scheduling. His principle is to store data through a linear table, and record the number of times each call to the data, the more commonly used to rank more forward, less use of the ranking is more up, if it is a newly added data, will put it in the first place, and then remove the last data. Here is how the memory LRU algorithm is implemented in the Kjframeforandroid framework Lrumemorycache

Since it is the load network picture, then of course need to load the control and network picture address as parameters, as shown below

private void LoadImage (ImageView ImageView, String imageUrl) {//First access the memory cache to determine if the picture already exists Bitmap Bitmap = mmemorycache.g    ET (STRINGUTILS.MD5 (IMAGEURL));        if (bitmap! = null) {Imageview.setimagebitmap (bitmap);            }else{//Otherwise go to network download bitmapworkertask task = new Bitmapworkertask (ImageView);    Task.execute (IMAGEURL); }}

     As for the actual download method, I will not explain in detail, I believe everyone can think of, is a network request, and then download the image, then to bitmap, and finally set as a control picture. However, there is an important point to note here, that is, when we download the picture successfully, remember to cache in the Mmemorycache.
Here is the code for a network picture load in the Kjfraemforandroid application Development framework:

    /**     *  load Image (Core method)      *       *  @param  imageView     *              control to display the picture (ImageView set src, normal view set BG)       *  @param  imageUrl     *      url     */     of         Pictures Private void loadimage (View imageview, string imageurl)  {         if  (config.callback != null)              config.callback.imgloading (ImageView);         bitmap bitmap = mmemorycache.get (STRINGUTILS.MD5 (IMAGEURL));         if  (bitMap != null)  {            if  ( Imageview instanceof imageview)  {                  ((ImageView)  imageview). Setimagebitmap (bitmap);             } else {                 imageview.setbackgrounddrawable (new  Bitmapdrawable (bitmap));            }             if  (config.callback != null)                   Config.callBack.imgLoadSuccess (ImageView);             if  (Config.isdebug) &NBSP;&NBSP;&NBSP;&NBSp;            kjloger.debuglog (GetClass (). GetName (),                          "download success, from memory cache\n"  +  imageurl);        } else {             if  (Imageview instanceof imageview)  {                  ((ImageView)  imageview). Setimagebitmap (Config.loadingbitmap);             } else {                 imageview.setbackgrounddrawable (New bitmapdrawable (           &nBsp;             config.loadingbitmap));             }             bitmapworkertask task = new bitmapworkertask (ImageView );             taskcollection.add (Task);             task.execute (IMAGEURL);         }    }    /*********************  Asynchronously gets bitmap and sets the task class for image  *********************/    private class  bitmapworkertask extends asynctask<string, void, bitmap> {         private View imageView;         public bitmapworkertask (view iMageview)  {            this.imageview =  imageview;        }          @Override         protected bitmap doinbackground ( String... params)  {            bitmap  bitmap = null;            byte[]  res = downloader.loadimage (Params[0]);             if  (res != null)  {                 bitmap = bitmapcreate.bitmapfrombytearray (Res, 0,  res.length,                     &nbSp;   config.width, config.height);             }            if  (bitmap  != null && config.openmemorycache)  {                 //  cached in Lrccache when the picture is loaded                  putbitmaptomemory (Params[0], &NBSP;BITMAP);                 if  (Config.isdebug)                      kjloger.debuglog (GetClass (). GetName (),                               "Put to memory cache\n " + params[0]);             }            return bitmap;         }         @Override         protected void onpostexecute (Bitmap bitmap)  {            super.onpostexecute (bitmap);             if  (imageview instanceof  imageview)  {                 if  (bitmap != null)  {                      ((ImageView)  imageview). Setimagebitmap ( bitmap);                 }             } else {                 imageview.setbackgrounddrawable (new bitmapdrawable (bitmap));             }             if  (config.callback != null)                  config.callback.imgloadsuccess (ImageView);             taskcollection.remove (This);         }    }

in-depth understanding of the application of image loading in real projects :

The above is just the basic operation of the network picture loading and caching, then we must consider the completeness and extensibility of the code if we use it in the actual project.

① such as we want to specify the size of the image, although we can set the fixed width of the view to force the image display size, but if it is a few megabytes of the picture, and we only need to 15*15 resolution size of the display area, which is obviously wasteful;

② and for example, we want the control to display a default image when the network is downloading pictures (such as a gray avatar) or when the picture is downloaded to show a circular progress bar, then the above code is no way;

③ again, for example, we want to download a variety of images, for different site sources have different ways to download ....


These special needs tell us that the above code is completely out of the way. So for the completeness and scalability of the control, we need a configurator, a monitor, a downloader. And so on special needs to add the plug-in development.

Therefore, we can see that under the Org.kymjs.aframe.bitmap package of the kjframeforandroid framework there are kjbitmapconfig, I_imageloder, I_ Display, and so on, with a final decorated class or protocol interface.

such as the Kjbitmapconfig class, is a final modified Configurator class, through this configurator, we can dynamically set the width of each downloaded image, as well as memory size. And I_imageloder, I_display is two protocol interface, respectively defines the method of the downloader and display, here is actually GOF design pattern in the factory method of application, but the factory here is not actually used to create objects, but to define the display method or download method, Regardless of the actual factory that implements the I_imageloder abstract factory, there must be a way to load the image. Then in the actual application of the project, it can be no matter what the actual factory of the downloader, only need to call the factory loading image method on the line.

/** * Image loading interface protocol, can be customized to implement this Protocol downloader * * @explain using the factory method mode design of the downloader, this class is also an abstract factory class for the production of byte[] products * @author KYMJS ([email protected]) * @v Ersion 1.0 * @created 2014-7-11 */public interface I_imageloder {public byte[] LoadImage (String imageUrl);}

Here we should be able to see the code snippet in the above code for a reason.
byte[] res = downloader.loadimage (params[0]);
Downloader is actually the abstract factory of the downloader.

As for the display logic and the downloader is the same, here I do not detail, you can view the source code of Kjframeforandroid or sample project.

Here is an implementation of the I_imageloader Downloader protocol Downloader.java, we can certainly according to their own needs to implement their own downloader, which is not any extension of the trial development, which for the image set code itself has no effect.



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.