Android image download and caching framework

Source: Internet
Author: User
Tags webp

Framework for image download and caching in real-world development

Introduce the use of common picture loading frameworks in development and compare the pros and cons.

1. Picasso Frame

Development in Android, often need to get pictures from the remote and display on the client, of course, we can use native httpurlconnection and Asynctask and other operations to complete, but not recommended, because this will not only require us to write a lot of code, Also need to deal with cache and download management, etc., it is best to encapsulate into a library or use a third-party library;

Use HttpURLConnection and asynctask for remote picture downloads:

Using HttpURLConnection and Asynctask to get a remote picture requires the following steps:

1, httpurlconnection connection = Url.openconnection ();

2, InputStream in = Connection.getinputstream ();

3, Bitmap Bitmap = Bitmapfactory.decodestream (in);

4, Imageview.setbitmap (bitmap);

We know that it is not possible to perform networking operations in the main thread, so we need asynctask to run the time-consuming operation in the background thread.

Basic use of Picasso:

After adding Picasso to the project, it's very easy to use it, just one line of code to get it done:

Picasso.with (context). Load (IMAGEURL). into (ImageView);

1, the image is automatically cached locally;

2, through the image compression conversion to reduce memory consumption;

3, Automatic processing of imageview recovery, that is, the automatic cancellation of the ImageView view in the field of views of the resource load;

Adapter:

The adapter automatically discovers and reuses previously canceled downloads:

Image format conversion:

There are many times when you need to format or cut a picture to save memory or achieve my layout:

Crop Size Method: Picasso.with (context). Load (IMAGEURL). Resize (+). Centercrop (). info (ImageView);

Custom Format conversions:

To achieve more of the effect you want the image to convert, you can implement a class that implements the transformation interface yourself, and then pass its objects to the transform () method:

Placeholder Picture:

The so-called placeholder image, which is the default picture when the picture is not displayed properly, is set by placeholder (), and Picasso also supports setting the default picture displayed when the picture is displayed incorrectly, using error () to set the Picasso.with (context). Load ( IMAGEURL). Placeholder (R.drawable.image_placeholder). Error (R.drawable.image_error_placeholder). into (ImageView);

Load Local resources:

In addition to downloading images over the Web, Picasso can also load local image resources:

Picasso.with (context). Load (R.drawable.icon). into (ImageView);

Picasso.with (context). Load ("File:///android_asset/Android.png"). into (ImageView);

Picasso.with (context). Load (new File (...)). Into (ImageView);

Debugging:

For ease of debugging, you can debug Picasso's setindicatiorenabled (true), allowing images from different sources to display a different color tag

2. Android Universal Image Loader

In the actual project development Imageloader usage is very high, the usage diversity is very big, because of the space question, the details please poke the following link:

Http://note.youdao.com/share/?id=7601c6850df29dea288734677fd5a56c&type=note

3. Glide Most fire picture loading open source frame loading GIF Resource Graph into Android ImageView

Normally Android ImageView can't load GIF images, and if you don't do any processing, the GIF loaded into ImageView shows only the first frame. There are many solutions available on the web, and there are many open source frameworks that customize the GIF view to load GIFs, mostly with the help of Android movie, the GIF image resource as a stream, parsing into Android movie display, The basic idea of these customizations is to first detect if the image resource is a GIF, and if so, parse it according to Android movie.
There is a androidglide open source framework, itself in the picture loading and caching is very good, at the same time, the Android glide loading GIF pictures is also very convenient, the need to load the GIF map into the drawable directory, and then the ordinary andriod ImageView setting a picture resource r.drawanle.xxx exactly the same, very simple. or from the Web URL to load a picture, the developer does not care whether the picture resource is GIF or other JPG or PNG format, directly as a normal picture loading can, as to the image of the resource format of the judgment processing and drawing, there are Android glide all do.

Now for example:

Where r.drawable.loading is the Loading.gif diagram loaded in the drawable directory:

4, fresco to let the image of the progressive presentation of the powerful framework

Fresco is a powerful picture loading component.

The Fresco is designed with a module called Image pipeline. It is responsible for loading images from the network, from the local file system, local resources. To maximize space and CPU time, it contains a level 3 cache design (level 2 memory, Level 1 files).

The Fresco design has a drawees module that conveniently displays the loading diagram, freeing up memory and space consumption when the picture is no longer displayed on the screen.

Fresco supports Android2.3 (API level 9) and above systems.

Characteristics

Memory management

The extracted images, which are bitmap in Android, occupy a lot of memory. Large memory usage is bound to cause more frequent GC. Under 5.0, the GC will significantly cause the interface to stutter.

In the system below 5.0, Fresco puts the picture in a special memory area. Of course, when the picture is not displayed, the memory that is occupied is automatically released. This will make the app smoother and reduce the oom caused by image memory usage.

Fresco is performing as well on low-end machines, and you no longer have to after thinking the memory footprint of your images.

Progressive rendering of images

Progressive JPEG image format has been popular for several years, progressive picture format first presents a rough outline of the picture, and then as the picture continues to download, showing a gradually clear picture, which for mobile devices, especially slow network has great positive, can bring a better user experience.

The image Library of Android itself does not support this format, but fresco supports it. When used, as always, just need to provide a picture of the URI, the rest of the matter, fresco will handle.

GIF graphs and WEBP formats

Yes, support for loading GIF graph, support WEBP format.

Rendering of images

Fresco's drawees design brings some useful features:

Customize Center focus (very helpful for displaying pictures such as faces)

Fillet chart, of course the circle is OK.

After the download fails, click reproduce download

Custom placeholder bitmap, custom overlay, or progress bar

Specify the overlay when the user presses

Loading of images

Fresco's image pipeline design allows users to control the loading of images in multiple ways:

Specify a different remote path for the same picture, or use a picture that already exists in the local cache

Show a low-resolution image first, and then display high-definition image after download

Load completion callback Notification

For this map, if you have an EXIF thumbnail, you can display the thumbnail before the large map loading is complete.

Zoom or rotate a picture

Process a downloaded picture

WebP Support

5, Picasso, Imageloader, fresco,glide merits

First look at the fresco, which has the advantage that several other frameworks do not have, or are several other frames of the short board.

Fresco:

Advantages:

    • The image is stored in the anonymous shared memory of the Android system, not in the VM's heap memory, the middle buffer data of the picture is also stored in the local heap memory, so the application has more memory usage, does not cause oom due to picture loading, and also reduces the interface lag caused by the garbage collector's frequent recall bitmap. Higher performance.
    • Progressive loading of JPEG images to support images from Blur to clear loading
    • The image can be displayed at any center point in the ImageView, not just the center of the image.
    • JPEG image Resizing is also done in native, not in VM heap memory, likewise reducing oom
    • Very good support for the display of GIF images

Disadvantages:

    • Large frame, impact apk volume
    • Use more cumbersome

Imageloader, Picasso, Glide: These three implementations are similar.

Imageloader:

Compared to the old frame, stable, loading speed is moderate, the disadvantage is not support GIF image loading, use a little cumbersome, and the caching mechanism does not have a good combination with the HTTP cache, is entirely their own set of caching mechanism.

Picasso:

Easy to use, a line of code to complete loading pictures and display, the frame size is small,

The downside is that GIF is not supported, and it may be that you want the server to handle the scaling of the image, the cached picture is not scaled, and the image is cached by default using the argb_8888 format, and the cache size is large.

Glide:

Can be said to be an upgrade version of Picasso, with the advantages of Picasso, and support GIF image loading display, the image cache will automatically scale, the default use of rgb_565 format cache picture, is the Picasso cache volume half.

Android image download and caching framework

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.