The author blog
http://blog.csdn.net/u013005791/
The article directories
-
An overview of the
-
integration
-
Basic usage
-
Comments on new V4 features and custom methods
-
Implement AppGlideModule in your project
-
GlideExtension
-
21 GlideOption
-
22 GlideType
-
A placeholder
-
Set placeholders
-
Options
-
RequestOptions
-
TransitionOptions
-
RequestBuilder
-
Transformations
-
Use the RequestOptions
-
Use the transform method in RequestOptions
-
V4 features
-
Multiple transformation
-
Animated Transitions
-
Common animation
-
Custom transition animation
-
The basic configuration
-
Configuring Memory Caching
-
Methods a
-
Method 2
-
Methods three
-
Disk cache
-
Disallow Manifest file parsing
-
Size of the View
-
Method one inherits ImageViewTarget
-
Method two uses Override
-
Load optimization for Recycle
0
An overview of the
Glide is an Android image loading and caching library that focuses on smooth loading of large numbers of images. Glide can handle almost any scenario you need to use to pull, compress, or display images from the web.
This paper introduces the basic usage of Glide4.0.
integration
Github:
https://github.com/bumptech/glide
Add dependencies to build. Gradle files at app or lib level:
In the proguard. Pro/proguard. CFG add confusion:
2
Basic usage
In most cases, loading an image requires only one line of code:
Unloading is also simple:
You don’t actually need to unload…
This is because Glide automatically cancels the load and reclaims all the resources used in the load when an Activity or Fragment you pass in the with method is destroyed.
3
Annotations (new in V4) and custom methods
Glide uses the Annotation Processor to generate apis that allow applications to modify RequestBuilder, RequestOptions, and any method contained in a single streaming API library. This is a V4 feature that makes it easier to use with annotations:
Glidev4 does not have fitCenter and placeholder methods after Glide.with().load(), but GlideApp does and can be used directly in Builder. GlideApp can replace the start of Glide in previous versions.
The purpose of this is:
-
Glide’s API can be inherited using custom methods for Library projects;
-
For applications, you can add custom methods after inheriting Glide’s API.
You can also inherit RequestOptions manually, but this is obviously more cumbersome and breaks the streaming API features.
Implement AppGlideModule in your project:
The class implementation must have @glidemodule annotations. If the methods you add fail, check here.
Library implements LibraryGlideModule, using OkHttp as an example:
OkHttpUrlLoader is a class in Glide’s OKHttp extension library. If you want to use Glide’s implementation, you can add it to a dependency:
The OkHttpLibraryGlideModule class does not need to be implemented. It is already included in the library and uses OKHttp automatically.
Then the compilation project can see that four classes are generated in the build:
GlideExtension
To add new methods, modify existing methods, or add support for other types of formats, you need to use annotated static methods in your extensions.
GlideOption is used to add custom methods and GlideType is used to support new formats.
GlideOption
Start by creating a new CustomGlideExtension class:
Open GlideOptions in the build directory and you can see that two methods are generated automatically:
Now you can use your own custom methods:
GlideType
Take adding support for the GIF format, for example, which is actually already supported in the API.
Add the following to the CustomGlideExtension class:
Create GlideRequests in the build directory and see that a method is automatically generated:
Now you can use the type you added:
4
A placeholder
The placeholder is the default image to display when the requested image is not loaded. Glide supports placeholders in three different situations:
-
Placeholder is requesting an image load
-
Error The image load Error is requested
-
The Fallback request URL /model is null
Set placeholders
I drew a flow chart for the subsequent display priorities.
5
Options
RequestOptions
Most of the request parameters in Glide can be set using the RequestOptions class and the Apply () method.
Request parameters in Glide mainly include:
-
Placeholders placeholder
-
Transformations transform
-
Caching Strategies Caching Strategies
-
Component-specific parameters: encoding quality, decoding parameters, etc.
For example, to set the image display to CenterCrop, you could do this:
It is perfectly possible to set the ImageView to Android :scaleType=”centerCrop” in the Layout file, and Glide will automatically set the image display based on this property.
The Apply method can be called more than once, but if two Apply Settings conflict, the last one will prevail.
TransitionOptions
TransitionOptions determines how to transition from a placeholder image (or the image before it) after the image is loaded.
-
Fade in
-
Cross fade in
-
No transition
Pay attention to
TransitionOptions is and you want to load the type of resource binding, that is to say, if you request a Bitmap (Bitmap), you will need to use the BitmapTransitionOptions, rather than DrawableTransitionOptions. This picture that you requested bitmap, therefore, you need to use simple fade in, but cannot use cross fade in (DrawableTransitionOptions. WithCrossFade ()). GenericTransitionOptions can be used if it is neither a Bitmap nor a Drawable
RequestBuilder
Function:
-
Specify the load type. AsBitmap (), asGif(), asDrawable(), asFile().
-
Specifies the URL /model to load.
-
Specifies which View to load into.
-
Specifies the RequestOption to apply
-
Specifies the TransitionOption to apply
-
Specify the thumbnail to load
So how do you get the RequestBuilder?
The default is a Drawable RequestBuilder. If you want to specify a type of Bitmap, you can write:
Application RequestOptions
RequestBuilder can also be reused:
6
Transformations
Glide automatically reads the scaleType of the ImageView, so specify scaleType in the layout file.
CenterCrop, CenterInside, CircleCrop, FitCenter, RoundedCorners
Glide supports setting these zoom types in Java code:
-
CenterCrop zoom width and height both reach the edge of the View, with one parameter on the edge and another parameter that may or may not be on the edge
-
CenterInside If the width and height are both inside the boundary of the View, then no scaling, otherwise the width and height are both inside the boundary of the View, one parameter is on the boundary, another parameter may be on the boundary or inside the boundary
-
CircleCrop is circular and incorporates the characteristics of CenterCrop
-
The FitCenter scale width and height both enter the View boundary, with one parameter on the boundary and another parameter that may be on or within the boundary
-
RoundedCorners rounded corners
There are three uses:
Use the RequestOptions
Use the transform method in RequestOptions
V4 features
The third method is the easiest and recommended.
Multiple transformation
7
Transitions (animation)
Common animation
Glide transition animation refers to animation from a placeholder to a request picture or thumbnail to a full-size request picture. Transition animations can only be performed for a single request, not across requests.
Transition animation execution timing:
-
Images are cached on disk
-
Image is local
-
Picture on remote
The transition animation will not be performed if the image is in memory cache. If you need to load an animation on an in-memory cache, you can do this:
Common usage is as follows:
Introduction to TransitionOptions: TransitionOptions. There are three TransitionOptions:
-
GenericTransitionOptions universal
-
DrawableTransitionOptions
-
BitmapTransitionOptions
If you want to use custom animations, Can use GenericTransitionOptions. With (int viewAnimationId) or BitmapTransitionOptions. WithCrossFade (int animationId, Int duration) or DrawableTransitionOptions. WithCrossFade (int animationId, int duration).
For performance reasons, had better not in the ListView, GridView, RecycleView used in transition animations, use TransitionOptions. DontTransition () can not loading animation, also can use dontAnimate loading animation
Custom transition animation
-
Implement TransitionFactory
-
Rewrite the build ()
You can control whether an image is animated on the memory cache.
For details, see DrawableCrossFadeFactory, and then call with(TransitionFactory TransitionFactory) of TransitionOptions.
8
The basic configuration
The basic configuration
Glide automatically allocates the memory cache properly, but it can also allocate it manually.
Methods a
Set by MemorySizeCalculator
SetMemoryCacheScreens Sets the number of device screens the MemoryCache should be able to hold pixel values, in other words, the number of screen images cached. The default value is 2.
Method 2
Methods three
Implement the MemoryCache interface yourself.
Clear memory cache, called in the main thread:
When used, the memory cache can be skipped:
Disk cache
Glide uses DiskLruCacheWrapper as the default disk cache. The default size is 250 MB. The cache files are placed in the cache folder of the APP.
Used as above, you can specify internal or external cache, as well as cache size and folder.
Custom disk cache
Implement the DiskCache interface by yourself.
Clear disk cache, called in the main thread:
Set disk cache policy when loading images:
The default strategy is DiskCacheStrategy. AUTOMATIC DiskCacheStrategy has five constants:
-
Diskcachestrategy. ALL uses DATA and RESOURCE to cache remote DATA and only RESOURCE to cache local DATA.
-
Diskcachestrategy. NONE Does not use disk caching
-
Diskcachestrategy. DATA writes the raw DATA to the disk cache before decoding the resource
-
DiskCacheStrategy. RESOURCE in RESOURCE after decoding the data to disk cache, namely after scaling transformation of image resources.
-
DiskCacheStrategy. AUTOMATIC coding strategy according to the original image data and resources to automatically select disk caching strategies.
Disallow Manifest file parsing
For users who upgrade from V3 to V4, the initialization speed is improved and potential errors are avoided.
Size of the View
Glide parses the width and height attributes of the ImageView like this:
-
If width and height are greater than 0, use the dimensions in Layout.
-
If width and height are WRAP_CONTENT, the screen size is used.
-
If at least one of width and height is <=0 and is not WRAP_CONTENT, an OnPreDrawListener is added at layout time to listen for the size of the ImageView
Glide does not support WRAP_CONTENT very well, so try not to use it.
So how do I change the ImageView size at run time?
Method one inherits ImageViewTarget
The View type is ImageView and the resource type is Bitmap, which can be modified as needed. OnResourceReady (Bitmap Bitmap, Transition<? Super Bitmap> Transition) you can use the Bitmap to get the size of the image.
Use:
Method 2: Override ()
Load optimization for Recycle
Load only when dragging and resting, not when auto sliding.
Today’s recommendation
Android Studio Practical tips
Like and forward is the biggest support for me!