Android uses Imageloader for image caching (Android development Essentials)

Source: Internet
Author: User

I believe that everyone in the study and the actual development of the basic will be in contact with the network data, and this one of the user experience is a very impact on the image of the cache, if there is no picture cache, the user experience will be greatly reduced, there is always the situation in Dayton, This problem is especially easy to appear in the case where the item in the ListView has a picture.

Before sharing with you a network connection framework retrofit, there are similar images loaded Picasso, we can go to experience, through train: http://www.cnblogs.com/liushilin/p/5680135.html

And, of course, what I think is the best Picture cache loading framework fresco, through train: http://www.cnblogs.com/liushilin/p/5659301.html

Everyone may be puzzled, why have such a good framework fresco, landlord why do you want to share this imageloader? What the heck. Perhaps we have different views, in short, each has the meaning of the existence, specifically on the screening of the people themselves.

Project synced to: Https://github.com/nanchen2251/ImageLoaderDemo

1) First look at the features of the Imageloader framework.

① multi-threaded download pictures, pictures can be from the network, file system, project folder assets and drawable medium

② supports arbitrary configuration of imageloader such as thread pool, picture downloader, memory cache policy, hard disk cache policy, picture display options, and other configuration

③ supports image memory cache, file system cache or SD card cache

④ supports monitoring of the image download process

⑤ bitmap is clipped based on the size of the control (ImageView), reducing the memory consumption of bitmap

⑥ good control of the loading process of the picture, such as pause picture loading, restart loading pictures, generally used in the Listview,gridview, the sliding process to pause loading pictures, stop sliding when to load the picture

⑦ provides loading of pictures on slower networks

2) then to use it, must add this support package, in studio support direct Add, search Image-loader can be added to the network, need to see its source code can also go to GitHub online Gank.

3) First, a simple use, loading a network image, only two lines of code, it is easy to see.

1<?xml version= "1.0" encoding= "Utf-8"?>2<Relativelayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Xmlns:tools= "Http://schemas.android.com/tools"5Android:layout_width= "Match_parent"6android:layout_height= "Match_parent"7tools:context= "Com.example.nanchen.imageloaderdemo.MainActivity" >8 9<ImageViewTenAndroid:layout_width= "Wrap_content" Oneandroid:layout_height= "Wrap_content" Aandroid:src= "@mipmap/ic_launcher" -Android:id= "@+id/main_image" -Android:layout_centerinparent= "true"/> the</RelativeLayout>

And look at the activity.

 PackageCom.example.nanchen.imageloaderdemo;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.ImageView;ImportCom.nostra13.universalimageloader.core.ImageLoader;Importcom.nostra13.universalimageloader.core.ImageLoaderConfiguration; Public classMainactivityextendsappcompatactivity {PrivateString imageUrl = "Http://pic.cnblogs.com/face/845964/20160301162812.png"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); ImageView ImageView=(ImageView) Findviewbyid (r.id.main_image); Imageloader.getinstance (). Init (Imageloaderconfiguration.createdefault ( This));//initialization Completeimageloader.getinstance (). DisplayImage (Imageurl,imageview); }}

Do not forget to add network permissions because you are accessing the network pictures

<uses-permission android:name= "Android.permission.INTERNET"/>

  

Operation diagram:

Very simple has the wood has, does not have you to write the network access, does not need you to go asynchronously executes, as long as two sentences, as long as two words!!!

4) Of course, you can also indulge in setting it up, just use a simple default property, the actual development we may need to write it according to their own needs, here set the load failed to display the picture, as well as the management of the cache mechanism.

1 //management actions for picture display2Displayimageoptions options =NewDisplayimageoptions.builder ()3. Cacheinmemory (true)4. Cacheondisk (true)5. Displayer (NewRoundedbitmapdisplayer (20))6 . Showimageonfail (R.mipmap.ic_launcher)7 . Bitmapconfig (Bitmap.Config.RGB_565)8 . Build ();9 Ten  One         //This allows you to manage the level two cache and level three cache after customizing the settings. AImageloaderconfiguration configuration =NewImageloaderconfiguration.builder ( This) -. Memorycachesizepercentage (20)//set percentage of memory consumed -. Diskcachefilecount (100)//set the maximum number of downloaded pictures the. diskcachesize (5 * 1024 * 1024) - . Defaultdisplayimageoptions (Options) - . Build (); -  +Imageloader.getinstance (). init (configuration);//initialization Complete -Imageloader.getinstance (). DisplayImage (Imageurl,imageview);

There are a lot of other properties here.

Of course, the use of Imageloader load network pictures is not only displayimage this method, but I think this is its method of relatively simple one.

It actually has loadimage (), Loadimagesync () Two ways, Loadimagesync () method is synchronous, android4.0 has a feature, network operation cannot be in the main thread, so Loadimagesync () Method We are not going to use it. For another way, you can try it on your own, but I do recommend the method.

4) In addition Imageloader nature is also support loading SD card picture and content provider, etc., use it is relatively simple, just need to add two simple words can

1         String ImagePath = "/mnt/sdcard/image.png"; 2         String imageUrl = ImageDownloader.Scheme.FILE.wrap (imagePath); 3 4         Imageloader.getinstance (). init (configuration); // initialization Complete 5         Imageloader.getinstance (). DisplayImage (Imageurl,imageview);

5) Of course, I believe that to use the picture loading frame, more people are using the ListView and the GridView to display a large number of pictures, and when we quickly slide the scroll bar, if the network load at the same time, we will find that performance is greatly affected by the welcome, which will appear in the serious situation in Dayton. So we should not be allowed to load the picture when sliding, until the end of the slide to load, this framework naturally without this function, it provides pauseonscrolllistner.

Lv.setonscrolllistener (new  Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling));  Gv.setonscrolllistener (new Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling));  

The three parameters are very simple, the first one is our Imageloader instance object, the second is a Boolean parameter, need to stop the sliding when passing a true, the third parameter control very quickly when the picture is loaded.

Said so much, also almost, Android a lot of things, you only to try, you will know the mystery, while young frivolous, good to engage in a wave of madness.

Android uses Imageloader for image caching (Android development Essentials)

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.