Android Open Source Framework Android-universal-image-loader
Source: Internet
Author: User
<span id="Label3"></p><p><p>Android Open source Framework Universal-image-loader is like a picture loader keeper, providing us with a wealth of features:<br>(1) multi-threaded loading images (asynchronous or synchronous);<br>(2) highly customizable imageloader configuration (thread pool, picture downloader, decoder, Memory and disk cache, display image options, etc.);<br>(3) Each display image has many custom options (stub picture, cache switch, decode option, bitmap processing and display, etc.);<br>(4) support memory and image cache on disk (device's file system and SD card);<br>(5) Monitor the loading process (including download progress);</p></p><p><p></p></p><p><p>Down we explain how to configure the use of Universal-image-loader to load network pictures</p></p>1. Download Universal-image-loader<p><p>GitHub Downloads:<br>Https://github.com/nostra13/Android-Universal-Image-Loader<br>Maven Downloads:<br>http://central.maven.org/maven2/com/nostra13/universalimageloader/universal-image-loader/</p></p>2. Using Universal-image-loader<p><p>(1) First associate the Universal-image-loader library File. Then initialize the Imageloader in the application OnCreate</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">myapplication</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">application</span>{</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (); Initimageloader (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Initimageloader</span></span>(context Context) {<span class="hljs-comment"><span class="hljs-comment">//this configuration tuning is Custom. You can tune every option, your may tune some of them,</span></span> <span class="hljs-comment"><span class="hljs-comment">//or You can create the default configuration</span> by</span>Imageloaderconfiguration.builder config =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Imageloaderconfiguration.builder (context); Config.threadpriority (thread.norm_priority-<span class="hljs-number"><span class="hljs-number">2</span></span>); Config.denycacheimagemultiplesizesinmemory ();<span class="hljs-comment"><span class="hljs-comment">//do not cache multiple-size pictures in memory</span></span>Config.diskcachefilenamegenerator (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Md5filenamegenerator ());<span class="hljs-comment"><span class="hljs-comment">//to Ensure that the image name is unique</span></span>Config.diskcachesize (<span class="hljs-number"><span class="hljs-number"></span> -</span>*<span class="hljs-number"><span class="hljs-number">1024x768</span></span>*<span class="hljs-number"><span class="hljs-number">1024x768</span></span>);<span class="hljs-comment"><span class="hljs-comment">//MiB</span></span> <span class="hljs-comment"><span class="hljs-comment">The default size of the memory cache is: 1/8 of the App's available memory</span></span>Config.tasksprocessingorder (queueprocessingtype.lifo); Config.writedebuglogs ();<span class="hljs-comment"><span class="hljs-comment">//Remove for release app</span></span> <span class="hljs-comment"><span class="hljs-comment">//Initialize Imageloader with Configuration.</span></span>Imageloader.getinstance (). Init (config.build ());<span class="hljs-comment"><span class="hljs-comment">//imageloader.getinstance (). init (imageloaderconfiguration.createdefault (this));</span></span>}}</code></pre><p><p>(2) Configuring Application in Androidmanifest</p></p><p><p>(3) loading the image in myimageapadter, initializing the displayimageoptions (for ease of use, We extract it into a class)</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-javadoc"><span class="hljs-javadoc">/** * universal-image-loader-master Options Parameters</span> */</span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">interface</span> <span class="hljs-title">imageloaderoptions</span> {</span></span>Displayimageoptions options =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Displayimageoptions.builder (). showimageonloading (r.drawable.ic_default)<span class="hljs-comment"><span class="hljs-comment">//what picture is displayed during the loading of the picture</span></span>. Showimageforemptyuri (r.drawable.ic_default)<span class="hljs-comment"><span class="hljs-comment">if the URL is empty, which image is displayed</span></span>. Showimageonfail (r.drawable.ic_default)<span class="hljs-comment"><span class="hljs-comment">//load picture failed to show which picture</span></span>. Cacheinmemory (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>)<span class="hljs-comment"><span class="hljs-comment">//cache the picture in memory</span></span>. Cacheondisk (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>)<span class="hljs-comment"><span class="hljs-comment">//cache the picture on your hard disk</span></span>. Imagescaletype (imagescaletype.exactly)<span class="hljs-comment"><span class="hljs-comment">//the image will be further scaled to the extent of the zoom reference</span> to the width of the ImageView</span>. Bitmapconfig (Bitmap.Config.RGB_565)<span class="hljs-comment"><span class="hljs-comment">//this type of rendering mode is also more memory-saving</span></span>. Considerexifparams (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>)<span class="hljs-comment"><span class="hljs-comment">//will Identify the orientation information of the picture</span></span> <span class="hljs-comment"><span class="hljs-comment">//. displayer (new fadeinbitmapdisplayer). build ();//animation effect that gradually appears</span></span>. Displayer (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Roundedbitmapdisplayer (<span class="hljs-number"><span class="hljs-number"></span> -</span>). Build ();<span class="hljs-comment"><span class="hljs-comment">//fillet Effect</span></span> <span class="hljs-comment"><span class="hljs-comment">//display the options of a large image</span></span>Displayimageoptions pager_options =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Displayimageoptions.builder (). showimageonloading (r.drawable.ic_default)<span class="hljs-comment"><span class="hljs-comment">//what picture is displayed during the loading of the picture</span></span>. Showimageforemptyuri (r.drawable.ic_default)<span class="hljs-comment"><span class="hljs-comment">if the URL is empty, which image is displayed</span></span>. Showimageonfail (r.drawable.ic_default)<span class="hljs-comment"><span class="hljs-comment">//load picture failed to show which picture</span></span>. Cacheinmemory (<span class="hljs-keyword"><span class="hljs-keyword">false</span></span>)<span class="hljs-comment"><span class="hljs-comment">//do not cache the picture in memory</span></span>. Cacheondisk (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>)<span class="hljs-comment"><span class="hljs-comment">//cache the picture on your hard disk</span></span>. Imagescaletype (imagescaletype.exactly)<span class="hljs-comment"><span class="hljs-comment">//the image will be further scaled to the extent of the zoom reference</span> to the width of the ImageView</span>. Bitmapconfig (Bitmap.Config.RGB_565)<span class="hljs-comment"><span class="hljs-comment">//this type of rendering mode is also more memory-saving</span></span>. Considerexifparams (<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>)<span class="hljs-comment"><span class="hljs-comment">//will Identify the orientation information of the picture</span></span>. Displayer (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Fadeinbitmapdisplayer (<span class="hljs-number"><span class="hljs-number"></span> -</span>). Build ();<span class="hljs-comment"><span class="hljs-comment">///animation effects are gradually displayed</span></span> <span class="hljs-comment"><span class="hljs-comment">//. displayer (new roundedbitmapdisplayer). build ();//fillet effect</span></span>}</code></pre><p><p>(4) call the load image in the GetView method</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-javadoc">/** * displayImage(uri, imageView, options, listener, progressListener); * 参数解析: * imageUrl : 图片的URL地址 * imageView : 显示图片的ImageView控件 * options : DisplayImageOptions配置信息 * listener : 图片下载情况的监听 * progressListener : 图片下载进度的监听 */</span><span class="hljs-comment">//加载图片</span>ImageLoader.getInstance().displayImage(Url.IMAGE, holder.iv_icon, ImageLoaderOptions.options);</code></pre></pre><p><p>–> expansion<br>Gradually show the effect of animation and rounded corners can not be set at the same time, the solution: we set the fillet effect in (3), and then discard (4) the method to load the picture, and use the following method, to this, we have the cake and eat both.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-comment">//加载图片</span>ImageLoader.getInstance().displayImage(Url.IMAGE,holder.iv_icon, options ,<span class="hljs-keyword">new</span> SimpleImageLoadingListener(){ <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onLoadingComplete</span>(String imageUri, View view, Bitmap loadedImage) { <span class="hljs-keyword">super</span>.onLoadingComplete(imageUri, view, loadedImage); <span class="hljs-number">800</span>);<span class="hljs-comment">//渐渐显示的效果</span> }});</code></pre></pre>3. Common problems and solutions in use<p><p>(1) the problem that the picture is not displayed on the Low-profile mobile phone or some mobile phone when encountering a large horizontal screen image (width>height)</p></p><p><p>Workaround: Configure the Imageloaderconfiguration parameter in the app, It is best to set up and configure in application (as in the Myapplication.java above), only once, such as multiple configurations, the system defaults to the first configuration Parameter.</p></p><pre class="prettyprint"><code class=" hljs avrasm">imageloaderconfiguration config = new imageloaderconfiguration<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Builder</span></span>(getapplicationcontext ())<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Memorycacheextraoptions</span></span>(<span class="hljs-number"><span class="hljs-number">480</span></span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>)//save the maximum length and width of each cached picture<span class="hljs-preprocessor"><span class="hljs-preprocessor">. ThreadPoolSize</span></span>(<span class="hljs-number"><span class="hljs-number">3</span></span>)//thread Pool Size This is actually the default<span class="hljs-number"><span class="hljs-number">3</span></span> <span class="hljs-preprocessor"><span class="hljs-preprocessor">. ThreadPriority</span></span>(Thread. norm_priority-<span class="hljs-number"><span class="hljs-number">2</span></span>)//set Thread Priority<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Denycacheimagemultiplesizesinmemory</span></span>()//when the same URI gets a different size picture, cache to memory only ONE. By default, multiple different sizes of the same picture are cached<span class="hljs-preprocessor"><span class="hljs-preprocessor">. MemoryCache</span></span>(new Usingfreqlimitedmemorycache (<span class="hljs-number"><span class="hljs-number">2</span></span>*<span class="hljs-number"><span class="hljs-number">1024x768</span></span>*<span class="hljs-number"><span class="hljs-number">1024x768</span></span>))<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Memorycachesize</span></span>(<span class="hljs-number"><span class="hljs-number">2</span></span>*<span class="hljs-number"><span class="hljs-number">1024x768</span></span>*<span class="hljs-number"><span class="hljs-number">1024x768</span></span>)//set the maximum bytes for the cache<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Tasksprocessingorder</span></span>(queueprocessingtype. LIFO)//set up picture download and display of work queue sorting<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Defaultdisplayimageoptions</span></span>(displayimageoptions. Createsimple ())<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Imagedownloader</span></span>(new Baseimagedownloader (getapplicationcontext (),<span class="hljs-number"><span class="hljs-number">5</span></span>*<span class="hljs-number"><span class="hljs-number"></span> +</span>,<span class="hljs-number"><span class="hljs-number"></span> -</span>*<span class="hljs-number"><span class="hljs-number"></span> +</span>)//connecttimeout: Timeout time<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Writedebuglogs</span></span>()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Build</span></span>()<span class="hljs-comment"><span class="hljs-comment">;//start Building</span></span>Imageloader. GetInstance ()<span class="hljs-preprocessor"><span class="hljs-preprocessor">. Init</span></span>(config)<span class="hljs-comment"><span class="hljs-comment">;//global Initialization of this configuration</span></span></code></pre><p><p>According to the above settings and configuration to resolve the screen large image on some phones do not display the problem, it is recommended to set up and configure in application, the disadvantage of the code configuration: use one time to Match.</p></p><p><p>(2) the problem of oom often occurs when a large number of images in a low-profile phone</p></p><p><p>The number of pictures in the project is large and large, and the crash information collected in the background sees a lot of oom Phenomenon.</p></p><p><p>Workaround:<br>1) Disable In-memory cache cacheinmemory (false)<br>2) reduce the size of the configured thread pool (. threadpoolsize (...)), recommended<br>3) use. bitmapconfig (Bitmap.Config.RGB_565) in Display options because rgb_565 mode consumes twice times less memory than argb_8888 mode<br>4) use. memorycache (newweakmemorycache ()) in configuration or completely disable in-memory caching<br>5) use The. imagescaletype (imagescaletype.exactly) or. imagescaletype (imagescaletype.in_sample_int) in the display options.<br>6) Avoid using Roundedbitmapdisplayer. Called when it uses the ARGB-8888 mode to create a new bitmap object to display</p></p><p><p>The above conditions, not necessarily all set, may change some of these options to set up can solve the problem, to be flexible to use</p></p>4. Related Use Tips<p><p>(1) you need to configure the Maximagewidthformemorycache () and maximageheightformemorycache () parameters only if you need to make the image larger than the size of the current device. For example, when zooming a picture. In other cases, these configurations are not required because the default configuration handles bitmap in the most memory-efficient manner based on the screen size.</p></p><p><p>(2) Configure the size of the thread pool in settings. A large thread pool allows multiple threads to work at the same time, but it can also significantly affect the speed of the UI Thread. however, You can fix this by setting a lower priority: when Imageloader is in use, you can lower its priority so that the UI thread is Smoother. The UI thread is often less fluid when using the listview, so it's best to set ThreadPoolSize () and ThreadPriority () in your program to optimize your app.</p></p><p><p>(3) the two parameters of MemoryCache () and memorycachesize () are covered with each other, so one can be used in the Imageloaderconfiguration.</p></p><p><p>(4) the three parameters of Diskcachesize (), diskcache () and Diskcachefilecount () will cover each other, using only One.</p></p><p><p>Special note: do not use the three parameters of Disccachesize (), disccache (), and Disccachefilecount () because they are deprecated.</p></p> <p><p>Android Open Source Framework Android-universal-image-loader</p></p></span>
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