MasterofProject

[UIL] Universal-Image-Loader (a complete analytical framework) the introduction and use of detailed

Label UILImageloaderPicture asynchronous loadingUniversal-Image-Load
2821 people read comment(4) Collection Report
Classification:

Reproduced please indicate the source:

Http://prog3.com/sbdm/blog/developer_jiangqq/article/details/50439814

This article from:[clear] Jiangqing blog


(One).Preface:

           Has not been updated for half a month, on the one hand, the recent work is busy, on the other hand, I also choose the theme of the blog. This time I mainly explain the normal use of the image loading frame ImageLoader. Although the author has stopped updating the frame shortly before, I have been using this image for a long time. I know that the framework is simple and easy to use, but if we can not only use but also be aware of the principle of which is the most important. So I putImageLoaderProject all the code read it again and give each class and related methods to add a Chinese version of the notes.

         This case as wellUILAll Chinese versions of the framework have been updated toGitHub, you can go to the interestClone, start orFork.

         addressHttps://github.com/jiangqqlmj/Android-Universal-Image-Loader-Modify

[By the way,Can scan the left side of the two-dimensional code is concerned about the micro channel pay attention to subscribe number,Get the latest articleNext week

          The topic of this article will mainly involve the following partsImageLoaderIntroduction and use of,Part of the important module analysis and related source code. Today, we mainly carry out the first part of the explanation(ImageLoaderBased on the introduction and use of).

(Two).ImageLoaderBasic introduction:

           We are developingAPPWhen you meet the need to load the image, especially the list needs to load a large number of pictures, then we will need to download the image and display. But due to various aspects of memory problems will have some problems more or less;for example:List loading pictures, pictures of the distribution of confusion. Or the OOM problem.~ ~At this time we need to develop a picture of asynchronous loading and can be properly compressed frame. for example(: UILUniversal-Image-Loader, GitHubProject addressUILProject address)Other than that, there are a lot of other frameworksGlide,Fresco, PicassoSo many. Behind several now used more and still more. Behind the article I will also share this side of the image loading framework.

          UIL(Universal-Image-Loader)Designed to provide a powerful, flexible and highly customizable tool for image loading, caching, and display functions. And provides a number of configuration options and good control of the process of image loading and caching. So in a long period of time, the framework of the use of a very wide and very convenient oh.

           UILFrame features:

  • Support multi thread image loading(Synchronous or asynchronous)
  • Support custom configurationImageLoader, such as the thread pool, image download, memory cache, hard disk cache, picture display options, and other configurations
  • Support for custom configuration picture display, for example:Cache switch selection,Interface options,Image loading progress and display
  • Support image memory cache or file system,Device SD cache
  • Support image loading progress monitor(Including picture download progress monitor)
  • Support for the extension of the framework for more tools
  • It can also be supported for exampleListView, GridVIewTo slide the time to suspend or resume image loading display and other functions.
  • Of course, there are other very characteristic,Specific, we read the source will know it... .

(Three).ImageLoaderRapid installation:

          If we want to useImageLoaderYou can download a copy of the program code which will haveImageLoaderTheJarThe package is introduced into the project. The contents folder in the overall project is as follows:


3.1.We downloadImageloaderTheJarPackage to our projectLIBSFolder, and then related to(Universal-image-loader-1.9.5.jar):

3.2.Now we useAndroidStudioDevelopment projects, we only needBuild.gradleAdd the following dependency can be:

                 Compile'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

3.3.Project configuration files need to add relevant permissions(Mainly for access to network permissions, if the picture needs SDCardCache also need to add one more)

<manifest>
Following permission if <! -- Include you load images from Internet -- >
<uses-permission android:name= "android.permission.INTERNET" / >
Following permission if <! -- Include you want to cache images on SD card -- >
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" / >
...
</manifest>

3.4.Finally we need to use the first timeImageLoaderbefore,Do itImageLoaderBasic configuration,The configuration can beApplicationperhapsActivityCan be done in,As follows:

Config ImageLoaderConfiguration = ImageLoaderConfiguration.Builder new (this)
...
.build ();
ImageLoader.getInstance ().Init (config);

3.5.We can use the followingImageLoaderTo load and display pictures.

(Four).ImageLoaderGlobal configuration item(Configuration)

         AboveThree point fourWe're talking about it.ImageLoaderYou need to configure the global configuration before you use it. OK,ImageLoaderGive us aImageLoaderConfigurationTo make global settingsImageLoader. But the setting is global, we only need to set up once.

          SeeImageLoaderDocuments andImageLoaderConfigurationSource discovery, which provides us with all the configuration items are optional. Unless you need to customize the settings, the template we can call the default construction method is as follows:

        Configuration=ImageLoaderConfiguration.createDefault ImageLoaderConfiguration (this);

ImageLoader.getInstance ().Init (configuration);

       AboveCreateDefaultMethod will give us the default initialization of a number of configuration items, the specific configuration can be all options as follows:


Above includes setting the cache size, task execution, and the number of thread pool threads,Thread weight,Task execution algorithm,Disk cache size, cache file number, cache file naming rules, image download, decoder, picture display configuration items, etc.. You can freely according to the actual situation of the project can be configured.

(Five).ImageLoaderpictureDisPlayConfiguration item(DisPlayOptions)

          We mentioned above.ImageLoaderThe global configuration, in the time of the picture display(ImageLoader.displayImage (... )At the time of)We can also set the parameters of the image display(Main useDisplayImageOptions). DisOptions play (Display options)For each picture display task(ImageLoader.displayImage ()The method is called).

         [note] if we do not set the image display configuration item, then I will display the picture when the default call picture default display mode. Specific optional configuration items are as follows:


 Above, including image loading process, pictures address is empty and failed to load the picture picture, delay load, open the cache memory and disk cache, picture zoom type, picture coding types and picture display parameters of the loader, and so on. Specific according to the actual project to choose the configuration can be.

(Six).ImageLoaderLoading pictures to explain

            Now we're starting to explain the use ofImageLoaderLoad pictures, before this we must be in accordance with the contents of the third points ImageLoader to install and do the basic permissions as well as global configuration oh~Below we first look at the ImageLoader to show the image of several methods:


  View aboveImageLoaderView of the method graph:Mainly divided into the following three ways to load the picture it~We generally use the first two in the project oh(displayImageandLoadImage),Because the two are asynchronous, and the otherLoadImageSyncIt is in sync.

6.1.dispalyImage ()Method use explanation:

6.1.1In this example IImageLoaderThe global default configuration is used as follows:

Class UILApplication extends Application public {
@Override
Void onCreate public () {
Super.onCreate ();
InitImageLoader ();
}
Void initImageLoader private () {
Configuration=ImageLoaderConfiguration.createDefault ImageLoaderConfiguration (this);
imageloader。getinstance() init(配置);
}
}

 实例布局文件如下

<?xml version=“1”encoding=“utf-8”?>
< LinearLayout xmlns:Android =“http://schemas.android.com/apk/res/android”
androidlayout_width =“match_parent”
安卓:layout_height =“match_parent”
安卓:方向=“垂直”
安卓:重力=“中心”>
<图片
安卓:ID =“”+身份证/ test_img_one”
安卓:layout_width =“wrap_content”
安卓:layout_height =“wrap_content”
安卓:src=“@冲/ ic_data_loading”/>
< /元素>

然后活动实例化控件以及imageloader,调用显示图象方法即可

这个test_img_one =(ImageView)这findViewById(r.id.test_img_one);
imageloader。getinstance()。DisplayImage(imagedatautils。imagesutils [ 0 ],test_img_one);

6.1.2。上边我们采用显示图象是两个参数的方法,我们同时可以在这边加入图片显示参数配置如下即可

这个test_img_one =(ImageView)这findViewById(r.id.test_img_one);
displayimageoptions选项=新displayimageoptions。builder()
。showimageonloading(r.drawable。ic_data_loading)
。showimageonfail(r.drawable。ic_data_error)
cacheinmemory(真的)。
cacheondisk(真的)。
build();
imageloader。getinstance()。DisplayImage(imagedatautils。imagesutils [ 1 ],test_img_one,选项);

上面我们配置加载过程中和加载失败显示的图片以及开启内存和磁盘缓存功能运行效果如下


6.1.3。在显示的时候我们还可以加入图片的目标尺寸,如下

图像大小尺寸=新图像大小(100,50);
imageloader。getinstance()。DisplayImage(imagedatautils。imagesutils [ 1 ],test_img_one,大小);

这边构造一个图像大小对象来定义目标图片的宽度和高度,传入宽100,50,运行效果如下


6.1.4。除了上面的三个显示重载方法之外,我们还可以传入一个图片加载进度监听器进入,具体接口回调我们放入到下面负荷image()方法中讲解。

6.2。loadimage()方法使用讲解

        上面六点一我们已经对displayimage()方法做了一些讲解,现在我们对于另外一个异步加载图片方法LoadImage做一下讲解。本例中我这边采用twoactivity以及一个简单布局该布局中就放入一个ImageView即可。全局配置和上面一样。

6.2.1。LoadImage(stringuri,imageloadinglistener听众)

         该传入两个参数第一个为图片下载地址,第二个为图片加载监听器首先采用默认的接口如下设置

这个test_img_two =(ImageView)这findViewById(r.id.test_img_two);
imageloader。getinstance()。LoadImage(imagedatautils。imagesutils [ 3 ],新imageloadinglistener() {
“重写”
公共无效onloadingstarted(字符串imageuri,视图){
/ /图片开始加载的时候调用
日志。D(“zttjiangqq”、“onloadingstarted…”);
}
“重写”
公共无效onloadingfailed(字符串imageuri观,失败的原因,失败的原因){
/ /图片加载失败调用
日志。D(“zttjiangqq”、“onloadingfailed…”);
}
“重写”
公共无效onloadingcomplete(字符串imageuri、视图、位图loadedimage){
/ /图片加载完成调用
日志。D(“zttjiangqq”、“onloadingcomplete…”);
test_img_two setimagebitmap(loadedimage);
}
“重写”
公共无效onloadingcancelled(字符串imageuri,视图){
/ /图片加载取消调用
日志。D(“zttjiangqq”、“onloadingcancelled…”);
}
});

具体运行打印的日志和运行结果如下


我们看到使用imageloadinglistener接口必须要实现接口中四个方法,但是我们平时实际情况下可能不需要回调那么多方法,不错的是UIL给我们提供了一个类simpleimageloadinglistener类,该类实现了imageloadinglistenerInterface We introduced the class can be implemented, so we need to rewrite the actual method is much more convenient. Specific examples are as follows         

ImageLoader.getInstance ().LoadImage (ImageDataUtils.ImagesUtils[4], SimpleImageLoadingListener new () {
@Override
Void onLoadingComplete public (imageUri String, view View, loadedImage Bitmap) {
Super.onLoadingComplete (imageUri, view, loadedImage);
/ / image loading is complete when the call
Log.d ("zttjiangqq", "onLoadingComplete...");
Test_img_two.setImageBitmap (loadedImage);
}

@Override
Void onLoadingStarted public (imageUri String, view View) {
Super.onLoadingStarted (imageUri, view);
The picture began to call the load / /
Log.d ("zttjiangqq", "onLoadingStarted...");
}
});

I just have to rewrite this way.OnLoadingCompleteandOnLoadingStartedThe two method, the amount of code on the top is reduced by a lot of.

6.2.2.LoadImage (Stringuri, targetImageSize ImageSize, options DisplayImageOptions,ImageLoadingListenerlistener)

   The loading picture methodSix point oneIn about the same, we can also get into the picture display configuration items, the specific examples are as follows:

Options=new DisplayImageOptions.Builder DisplayImageOptions ()
.showImageOnLoading (R.drawable.ic_data_loading)
.showImageOnFail (R.drawable.ic_data_error)
.cacheInMemory (true)
.cacheOnDisk (true)
.build ();
ImageLoader.getInstance ().LoadImage (ImageDataUtils.ImagesUtils[5], options, SimpleImageLoadingListener new () {
@Override
Void onLoadingComplete public (imageUri String, view View, loadedImage Bitmap) {
Super.onLoadingComplete (imageUri, view, loadedImage);
/ / image loading is complete when the call
Log.d ("zttjiangqq", "onLoadingComplete...");
Test_img_two.setImageBitmap (loadedImage);
}
});

6.2.3.LoadImage (Stringuri, targetImageSize ImageSize, listener ImageLoadingListener)

 We can load the image into the image of the size of the image, with a direct construction of aImageSizeInto it. Specific methods are as follows:

Size=new ImageSize ImageSize (100,50);
ImageLoader.getInstance ().LoadImage (ImageDataUtils.ImagesUtils[6], size, SimpleImageLoadingListener new () {
@Override
Void onLoadingComplete public (imageUri String, view View, loadedImage Bitmap) {
Super.onLoadingComplete (imageUri, view, loadedImage);
/ / image loading is complete when the call
Log.d ("zttjiangqq", "onLoadingComplete...");
Test_img_two.setImageBitmap (loadedImage);
}
});

6.2.4.Mainly in several ways, other image loading method and the above almost, just need to pass the corresponding parameters on it.

(Seven).Last summary

          Today we areUIL(Universal-Image-Loader)Introduction and basic use of the open source framework to do the relevant explanation, including the introduction, installation, configuration and basic use. The next lecture will focus on the open source components of several core classes, as well as related to the use of attention.

         This case as wellUILAll Chinese versions of the framework have been updated toGitHub, you can go to the interestClone, start orFork.

         addressHttps://github.com/jiangqqlmj/Android-Universal-Image-Loader-Modify

Respect the original, reproduced please indicate: Sky From Dian Qing (Http://prog3.com/sbdm/blog/developer_jiangqqRights reserved)!

Pay attention to my subscription number (codedev123), share the mobile development technology (Android/IOS), project management and blog post every day! (welcome attention, the first time push the wonderful article)


Focus on my micro blog, you can get more exciting content







top
Eight
step on
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit446900 times
    • Integral:Seven thousand two hundred and eighteen
    • Grade
    • Rank:1601st name
    • Original192
    • Reproduced:5
    • Translation:0
    • Comments:395
    Contact information
    Individual micro channel subscription number (codedev123)


  • Mailbox: jiangqqlmj@163.com
  • I QQ:781931404
  • Android rookie news group: 99787482
  • Native React exchange group: 282693535
  • Android development group 3:109244103
    Blog column