Android powerful picture loading cache-glide

Source: Internet
Author: User

in the picture loading library Bad Street today, choose a suitable for their own use of the image loading library has become a necessary step for every Android developer. Now the famous picture loading library in the market has UIL,Picasso, Volley Imageloader,Fresco and our protagonist today Glide . They are not the same, they can not judge who is better than who, can only say which is more suitable for you.


I understand.

Let me talk about the personal understanding of these images loading library, if there are errors, but also hope to advise.

Universal Image Loader: A powerful image loading library that contains a wide variety of configurations, oldest and most widely used.

Picasso: Square produced, must be a boutique. Match with OKHttp !

volley Imageloader: Google official production, but can not load local pictures ~

Fresco: Facebook, born proud! Not as strong as usual.

Glide: Google's recommended image loading library, focusing on smooth scrolling.

For more details, please see the question on StackOverflow.

Preliminary Glide

Below to enter today's theme, I believe many students have seen this introduction glide article, the Chinese version here. This paper introduces and compares the glide and Picasso from various aspects, in general, they are very similar, and have almost the same usage style of API. But glide is notch above in caching policies and loading GIFs. Finally, the author also strongly recommended this library.

And it is said that in Google's new photos application, traces of glide are visible everywhere. See here, you are not eager to try this library? Just as you make up your mind to try it out, you hear that the Yelp app, which is said to be a public review of the United States, is also using this vault for the bombing days. Your heart is very excited, hair four make sure to use this library. Just do it, open Android Studio, add in Builde.gradle

Compile ' com.github.bumptech.glide:glide:3.6.1 '

Then the global search for the picture loading place, all replaced by the following code:

Glide.with (mcontext).        load (URL).        placeholder (r.drawable.loading_spinner).        crossfade ()        . into ( Myimageview);

After a long process of compiling, once again open the app, see the image with the effect of the gradual appearance in front of you, you can not help calling: "Wocao, really TM handsome!" Why didn't I find it before? ”。

But after you've been using it for a few days, you'll find some questions:

Why do some pictures only display the bitmap when it is loaded for the first time, and then the normal picture is displayed the second time?

Why do I always get something like you cannot start a load for a destroyed activity such an exception?

Why can't I give the loaded picture Settag ()?

Why? Why? There are so many problems in the library so NB. Yes, that's the point I'm going to talk about today. How to avoid the above problems to happen.

Some solutions

1. If you happen to be using this circular ImageView library or some other custom round imageview, and you happen to have a placeholder, then you will encounter the first question. How to solve it?

Scenario One: Do not set a placeholder;
Scenario Two: Use the Glide transformation API to customize the conversion of the circular bitmap. Here is an existing example;
Scenario Three: Load the picture using the following code:

Glide.with (mcontext).    load (URL).     placeholder (r.drawable.loading_spinner)    . into (New simpletarget< Bitmap> (width, height) {        @Override public         void Onresourceready (Bitmap Bitmap, glideanimation anim) {            // Setimagebitmap (bitmap) on Circleimageview         }     };

2. As for the second question, please remember a word: do not use glide in the main thread to load pictures, if really used, please change the context parameter to Getapplicationcontext. For more details please refer to this issue.

3. Why can't I set tag because you are using the wrong posture? How do I set tag for ImageView? And listen to me carefully.

Scenario One: Use the Settag (Int,object) method to set the tag, as follows:

The Java code is Jiangzi:

Glide.with (context). Load (Urls.get (i). GETURL ()). Fitcenter (). into (imageviewholder.image);        ImageViewHolder.image.setTag (R.id.image_tag, i);        ImageViewHolder.image.setOnClickListener (New View.onclicklistener () {            @Override                int position = (int) V.gettag (R.id.image_tag);                Toast.maketext (context, urls.get (position). getwho (), Toast.length_short). Show ();            }        );

Also create a new ids.xml under the Values folder and add

<item name= "Image_tag" type= "id"/>

Done!

Scenario Two: The new global setting method is added after the 3.6.0 from glide. Here's how:

First implement the Glidemoudle interface, the global settings Viewtaget TagID:

public class Myglidemoudle implements glidemodule{    @Override public    void Applyoptions (context context, Glidebuilder builder) {        viewtarget.settagid (r.id.glide_tag_id);    }    @Override public    void registercomponents (context context, Glide Glide) {    }}

Also, you need to add an ID under Ids.xml

<item name= "glide_tag_id" type= "id"/>

Finally add in the Androidmanifest.xml file

<meta-data    android:name= "Com.yourpackagename.MyGlideMoudle"    android:value= "Glidemodule"/>

And can play happily, hehe ' (∩_∩).

Scenario Three: Write a class that inherits from Imageviewtaget, and make a copy of its Get/setrequest method.

Glide.with (context). Load (Urls.get (i). GETURL ()). Fitcenter (). into (New imageviewtarget<glidedrawable> (                Imageviewholder.image) {@Override protected void Setresource (Glidedrawable Resource) {            ImageViewHolder.image.setImageDrawable (Resource); } @Override public void Setrequest (Request request) {ImageViewHolder.image.setTag (i)                ;            ImageViewHolder.image.setTag (r.id.glide_tag_id,request); @Override public Request getrequest () {return (Request) ImageViewHolder.image.getT            AG (R.ID.GLIDE_TAG_ID);        }        }); ImageViewHolder.image.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (                View v) {int position = (int) v.gettag ();            Toast.maketext (context, urls.get (position). getwho (), Toast.length_short). Show (); }        });
Some tips for using

1.glide.with (context). Resumerequests () and Glide.with (context). Pauserequests ()

When the list is sliding, call pauserequests () to cancel the request, and when the slide stops, call Resumerequests () to resume the request. Would that be better?

2.glide.clear ()

This method can help you when you want to erase all the picture loading requests.

3.ListPreloader

If you want the list to be preloaded, try listpreloader this class.

Some excellent libraries based on glide

1.glide-transformations

A glide-based transformation library, with a variety of cropping, coloring, blur, filters and other conversion effects, like the Can't Do it ~ ~

2.GlidePalette

A library that makes it easy to use palette when loading glide.


Android powerful picture loading cache-glide

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.