Android glide picture loading (loading monitor, loading animation)

This example shares the specific code of Android glide image loading for your reference. The specific contents are as follows

1. Common usage

Context, activity and fragment can be placed in with..; When activity and fragment are placed, glide will load pictures according to the life cycle. Activity is recommended.

2. Set loading and loading failed pictures

3. Add the effect of picture fade in loading

.crossFade()

4. Customize the animation with animate()

Animation from asset:

Back to the code, the first option is to pass an Android resource ID, that is, the animation resource. A simple example is provided by every Android system: slide in left animation, android.r.anim.slide_ in_ left 。 The following code is an XML description of the animation:

Of course, you can create your own XML animation. For example, for a small zoom animation, the picture starts small, and then gradually increases to its original size.

Both animations can be used by glide Builders:

After the picture is loaded and ready from the network, it will slide in from the left.

Animation through custom classes

This is very simple. You only need to implement the void animate (view) method. This view object is the entire target view. If it is a custom view, you need to find the child elements of your view and do some necessary animation.

Let's take a simple example. Suppose you want to implement a fade in animation, you need to create such an animation object:

Next, you need to set this animation in the glide request:

Of course, in your animation object method in animate (view view), you can do anything you want to do with the view. Free to use your animation to create it.

If you want to implement it in your custom view, you just need to create the view object and then create your custom method in your custom view.

5. Add load completion listening

6. Picture caching mechanism

Glide cache policy

Glide enables disk cache and memory cache by default. Of course, you can also set specific cache policies for a single picture. Set the picture not to be added to the memory cache

Set pictures not to be added to disk cache

Glide supports a variety of disk caching strategies:

Diskcachestrategy.none: do not cache pictures diskcachestrategy.source: cache picture source file diskcachestrategy.result: cache modified pictures diskcachestrategy.all: cache all pictures, default

Picture loading priority

Glide supports setting priority for image loading. The higher priority is loaded first, and the lower priority is loaded later:

7. Load fillet image

**Then, just add this sentence when using. Transform (New glidecircle transform (context))**

matters needing attention:

You cannot directly set the tag for the ImageView to use glide;

Because glide uses a tag when loading pictures, it will cause conflicts and report errors;

When you want to use tag to write logic code, you can do this

.setTag(R.string.xxx,xxx); And. Gettag (r.string. XXX);

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>