Android Image Caching First Glide (iii) _android

Source: Internet
Author: User
Tags memory usage


The previous summary learned the use of pictures and LRU algorithm, today to learn more excellent image caching open source framework. Technology itself will be constantly changing, from the initial use of their own softreference to achieve their own picture caching, to later do the electrical Business Project their own implementation can not meet the needs of the project to switch to Afinal, because afinal no longer maintain and chose the teacher out of the same gate xutils, Also contacted in the middle of other open source framework such as Picasso, the first impression of Picasso is not very good, the first contact is to get the company from the outsourcing company to take over the photo-type app, the memory footprint is too large, the direct feeling is to lead to ListView sliding a little cotton, Old image caching framework Universalimageloader heard that has not really used, before the project is very small, almost millions of other apps, has been using the xutils, recently feel that the project is big, In case Xutils does not maintain or ask to support the picture format more, may Xutils is not the best choice, this is to learn the fundamental motives of Gilde it. Actually would like to learn the Facebook fresco picture frame, but a simple look, you need to work with the custom control, although powerful, but for the maintenance of the project to modify the cost that is not generally high, later interested in learning it!

Glide Introduction:

Glide is a Google employee open source project that is recommended on Google I/O, an efficient, open source, and media management framework on Android devices that follows the BSD, MIT, and Apache 2.0 protocols release. Glide has the ability to capture, decode, and display video stills, pictures, animations, and so on, and it has a flexible API that allows developers to apply glide to almost any network protocol stack. The main purpose of creating glide is two, one is to achieve smooth Picture list scrolling effect, the other is to support remote image acquisition, sizing and display.
GitHub Address:

Glide features
• Easy to use
• High configurable degree and high adaptability
• Support Common picture format Jpg png gif webp
• Support a wide range of data source networks, local, resource, Assets, etc.
• Efficient caching policy supports memory and disk picture caching default bitmap format with rgb_565 memory usage at least half
• Lifecycle integration automates management of requests based on the Activity/fragment lifecycle
• Efficient handling of bitmap using bitmap pool for bitmap reuse, active call recycle recovery bitmap recovery, reduced system recovery pressure

Glide simple to use
1.) Add Reference build.gradle add configuration
compile ' com.github.bumptech.glide:glide:3.7.0 '
2.) Set the binding life cycle
We can bind more efficiently with the way glide provides, so that the lifecycle of the request to load the picture can be managed dynamically

 Glide.with (context context);//bind Context
 Glide.with (activity activity);//Bind Activity
 glide.with ( Fragmentactivity activity);/bind fragmentactivity
 glide.with (Fragment Fragment);//Bind Fragment 

3.) Simple loading Picture instance
Glide.with (this). Load (IMAGEURL). into (ImageView);
4.) Set load and load failed pictures
The API inside the placeholder (), error () function in the polymorphic implementation of the time can be specific to familiarize yourself with

Copy Code code as follows:
Glide.with (this). Load (IMAGEURL). Placeholder (r.mipmap.ic_launcher). Error (R.mipmap.ic_launcher). into (ImageView);

5.) Set Skip Memory cache
Glide.with (this). Load (IMAGEURL). Skipmemorycache (True). into (ImageView);
6.) Set Download priority
Glide.with (this). Load (IMAGEURL). Priority (Priority.normal). into (ImageView);
7.) Set caching policy
Glide.with (this). Load (IMAGEURL). Diskcachestrategy (Diskcachestrategy.all). into (ImageView);
Policy Commentary:
All: Cache source resources and converted Resources
None: Do not make any disk caching
Source: Caching Resource
Result: Cached converted Resources
8.) Set Load animation
The API also provides several common animations: such as Crossfade ()
Glide.with (this). Load (IMAGEURL). Animate (R.anim.item_alpha_in). into (ImageView); R.anim.item_alpha_in

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"" Android ">
    android:fromalpha= "0.0"
    android:toalpha= "1.0" >

9.) Set thumbnail support
This will load the thumbnail first and then load the entire diagram
Glide.with (this). Load (IMAGEURL). Thumbnail (0.1f). into (ImageView);
10.) Set Load Size
Glide.with (this). Load (IMAGEURL). override (a). into (ImageView);
11.) Setting up dynamic transformations
Glide.with (this). Load (IMAGEURL). Centercrop (). into (ImageView);

APIs such as: Centercrop (), Fitcenter () and other functions can also be customized transformation, for example: a person rounded converter

 public class Glideroundtransform extends Bitmaptransformation {private float radius = 0f;
  Public Glideroundtransform {This (context, 4);
   Public Glideroundtransform (context, int dp) {super (context);
  This.radius = Resources.getsystem (). Getdisplaymetrics (). density * DP; @Override protected Bitmap transform (Bitmappool pool, Bitmap totransform, int outwidth, int outheight) {return
  Roundcrop (pool, totransform);

   Private Bitmap Roundcrop (Bitmappool pool, Bitmap source) {if (Source = null) return null;
   Bitmap result = Pool.get (Source.getwidth (), Source.getheight (), Bitmap.Config.ARGB_8888);
   if (result = = null) {result = Bitmap.createbitmap (Source.getwidth (), Source.getheight (), Bitmap.Config.ARGB_8888);
   } Canvas Canvas = new Canvas (result);
   Paint Paint = new Paint ();
   Paint.setshader (new Bitmapshader (source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
   Paint.setantialias (TRUE);RECTF RECTF = new RECTF (0f, 0f, Source.getwidth (), Source.getheight ());
   Canvas.drawroundrect (RECTF, radius, radius, paint);
  return result;
  @Override public String getId () {return getclass (). GetName () + math.round (RADIUS);


Specific use
Glide.with (this). Load (IMAGEURL). Transform (The new Glideroundtransform (this)). into (ImageView);
12.) Set the content to be loaded
There are many projects in the need to download the pictures and then do some synthetic functions, such as the project appears in the graphic mix, how to achieve the goal under

 Glide.with (this). Load (IMAGEURL). Centercrop (). into (new simpletarget<glidedrawable> () {
   public void Onresourceready (glidedrawable resource, glideanimation<? Super Glidedrawable> Glideanimation) {
    Imageview.setimagedrawable (Resource);

13.) Set up the listener request interface

 Glide.with (this). Load (IMAGEURL). Listener (New requestlistener<string, glidedrawable> () {
   public boolean onexception (Exception E, String model, Target<glidedrawable> Target, Boolean isfirstresource) { return

   @Override Public
   Boolean Onresourceready (glidedrawable resource, String model, target<glidedrawable> Target , Boolean Isfrommemorycache, Boolean isfirstresource) {
    //imageview.setimagedrawable (Resource);
    return false;
  }). Into (ImageView);

Setting up the use of listening can be used to monitor the source of the error from which the request occurred, and whether the picture originated in memory or disk

14.) Set up dynamic GIF loading mode

Glide.with (this). Load (IMAGEURL). Asbitmap (). into (ImageView);//display gif static picture
 Glide.with (this). Load (IMAGEURL). Asgif (). into (ImageView);//display GIF animated picture 

15.) Cache Dynamic Cleanup

Glide.get (This). Cleardiskcache ()//cleaning the disk cache requires the execution of Glide.get (this) in a child thread
 . Clearmemory ()//clean memory cache can be done in the UI main thread 

The above is the general use of glide, basically meet the development needs, and then to learn about other relevant knowledge.

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: 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.