Explain the latest version of glide V4 User Guide

Glide is an Android image loading and caching library. It mainly focuses on the smooth loading of a large number of images. Glide can be competent for almost any scene where you need to use images to pull, compress and display from the network.

This article mainly introduces its basic usage based on glide version 4.0.

GitHub address: https://github.com/bumptech/glide

Add dependency to build.gradle file at app or lib level:

Android studio 3.0 uses:

Whether to use implementation or API depends on the situation. Implementation can only be used for the current module. If dependencies are set in this way in the library, the module in the app cannot be referenced, but the API can. The API is equivalent to compile.

Add confusion in proguard.pro/proguard.cfg:

In most cases, loading a picture requires only one line of code:

Canceling the load is also simple:

In fact, you don't need to cancel loading...

Because when the activity or fragment you passed in the with method is destroyed, glide will automatically cancel the loading and recycle all the resources used in the loading process.

Glide uses the annotation processor to generate APIs, allowing applications to modify requestbuilder, requestoptions and any methods contained in a single streaming API library. This is a feature of V4, which is more convenient to use after annotation:

After glide. With(). Load() in glidev4, there are no methods such as fitcenter and placeholder in previous versions, but glideapp has and can be used directly in the builder. Glideapp can replace the beginning of glide in previous versions.

The purpose of this is:

1. For library projects, you can use custom methods to inherit glide's API. 2. For applications, after inheriting glide's API, you can add custom methods.

Although you can also manually inherit requestoptions, it is obviously more troublesome and destroys the streaming API feature.

3.1 implement appglidemodule in the project:

This class implementation must have @ glidemodule annotation. If the method you added fails, check here.

If it is a library, implement the libraryglidemodule. Take okhttp as an example:

Okhttpurlloader is a class in glide's okhttp extension library. If you need to use glide's implementation, you can add it to the dependency:

Android Studio 3.0

After adding dependencies, you don't need to implement the okhttplibraryglidemodule class yourself. Okhttp will be used automatically because it is already included in the library.

Then, when compiling the project, you can find that four classes are generated in build:

3.2 GlideExtension

In order to add new methods, modify existing methods, or add support for other types of formats, you need to use annotated static methods in the extension.

Glideoption is used to add custom methods, and glidetype is used to support new formats.

3.2.1 GlideOption

First create a customglideextension class:

Compile the project and open glideoptions in the build directory. You can see that two methods are automatically generated:

Now you can use your custom method:

3.2.2 GlideType

Taking the addition of support for GIF format as an example, it is only an example. In fact, it is already supported in the API.

Add the following to the customglideextension class just now:

Compile the project and open gliderequests in the build directory. You can see that a method is automatically generated:

Now you can use the type you added:

Placeholders are the default images displayed when the requested image is not loaded.

Glide supports placeholders in three different situations:

Set placeholder:

After that, I drew a flow chart.

5.1 RequestOptions

Most of the request parameters in glide can be set through the requestoptions class and the apply () method.

The request parameters in glide mainly include:

For example, to set the display mode of pictures to centercrop, you can do this:

But in fact, you can set the ImageView to Android: scaletype = "centercrop" in the layout file. Glide will automatically set the display mode of the image according to this attribute.

The apply method can be called multiple times, but if there are conflicting settings between the two applications, the last one will prevail.

5.2 TransitionOptions

The transition options determine how to transition from a placeholder picture (or previous picture) after the picture is loaded.

be careful

Transition options are bound to the type of resource you want to load, that is, if you request a bitmap, you need to use bitmaptransition options instead of drawabletransition options. Therefore, for the bitmap you requested, you need to use simple fade in instead of cross fade (drawabletransitionoptions. Withcrossfade()).

If it is neither bitmap nor drawable, you can use generictransitionoptions

5.3 RequestBuilder

effect:

So how to get requestbuilder?

A drawable requestbuilder is obtained by default. If you want to specify the type as bitmap, you can write as follows:

Apply requestoptions

Requestbuilder can also be reused:

Glide will automatically read the zoom type of ImageView, so it is generally enough to specify the scaletype in the layout file.

CenterCrop,CenterInside,CircleCrop,FitCenter,RoundedCorners

Glide supports setting these scaling types in Java code:

There are three uses:

1 using requestoptions

2 use the transform method in requestoptions

3 V4 characteristics

The third method is the simplest and recommended.

Multiple transformations

Ordinary animation

Transition animation in glide refers to the animation from placeholder to requested picture or thumbnail to full size requested picture. Transition animation can only be executed for a single request and cannot be executed across requests.

Transition animation execution time:

1. Pictures are cached on disk 2. Pictures are local 3. Pictures are remote

If the picture is in the memory cache, the transition animation will not be executed. If you need to load animation on the memory cache, you can do this:

The common usage is as follows:

Introduction to transition options: transition options. There are three transitionoptions:

If you want to use custom animation, you can use generictransitionoptions.with (int viewanimationid) or bitmaptransitionoptions.withcrossfade (int animationid, int duration) or drawabletransitionoptions.withcrossfade (int animationid, int duration).

For performance reasons, it's better not to use transition animation in listview, GridView and recycleview. You can use transitionoptions. Donttransition() without loading animation or dontanimate without loading animation

Custom transition animation

1. Implement transitionfactory 2. Rewrite build ()

You can control whether a picture performs animation on the memory cache.

Refer to DrawableCrossFadeFactory in detail, and then call TransitionOptions's with (TransitionFactory transitionFactory) to load.

8.1 configuring memory cache

Glide will automatically allocate the memory cache reasonably, but it can also allocate it manually.

Method 1

Set via memorysizecalculator

Setmemorycachescreens sets the number of device screens with pixel values that the memorycache should be able to accommodate. In other words, it is how many pictures are cached. The default value is 2.

Method 2

Method 3

Implement the memorycache interface yourself.

Clean up the memory cache and call in the main thread:

When using, you can skip the memory cache:

8.2 disk cache

Glide uses disklrucachewrapper as the default disk cache. The default size is 250m. The cache files are placed in the cache folder of app.

As above, you can specify that the cache is stored internally or externally, or you can specify the cache size and folder.

Custom disk cache

Implement the diskcache interface yourself.

Clean up the disk cache and call in the child thread:

Set disk cache policy when loading pictures:

The default policy is diskcachestrategy.automatic

Diskcachestrategy has five constants:

8.3 it is forbidden to parse manifest files

It is mainly aimed at users upgrading from V3 to v4. It can improve the initialization speed and avoid some potential errors.

8.4 dimensions

Glide resolves the width and height attributes of ImageView as follows:

Glide vs wrap_ The support of content is not good, so try not to use it.

So how to modify the ImageView size at run time?

Method 1 inherits imageviewtarget

The type of the view I specify here is ImageView, and the resource type is bitmap, which can be modified as needed. In the onresourceready (Bitmap bitmap, transition <? Super bitmap > transition) method, the size of the picture can be obtained through bitmap.

use:

Method 2: use override ()

Get bitmap

If you only want to use glide to parse the URL, get a bitmap, and then process it yourself, you can use simpletarget < z >,

Simpletarget can also specify width and height. Usage examples:

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
分享
二维码
< <上一篇
下一篇>>