usage
- Basic Usage
Glide.with(fragment)
.load(url)
.into(imageView);
Glide.with(fragment).clear(imageView);
- Module Definition
package com.example.myapp;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}
The default name is generated API GlideApp
, and AppGlideModule
the same sub-class package name. Application in a module Glide.with()
is replaced GlideApp.with()
, the API can be used to complete the loading work.
GlideApp.with(fragment)
.load(myUrl)
.placeholder(placeholder)
.fitCenter()
.into(imageView);
- RecyclerView uses
Glide View has been processed and multiplexed request will be automatically canceled
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String url = urls.get(position);
Glide.with(fragment)
.load(url)
.into(holder.imageView);
}
- Non-View
Glide.with(context
.load(url)
.into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<Drawable> transition) {
// Do something with the Drawable here.
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
// Remove the Drawable provided in onResourceReady from any Views and ensure
// no references to it remain.
}
});
- Use a background thread
submit
FutureTarget<Bitmap> futureTarget =
Glide.with(context)
.asBitmap()
.load(url)
.submit(width, height);
Bitmap bitmap = futureTarget.get();
...
Glide.with(context).clear(futureTarget);
The same can also be loaded asynchronously in the background thread to start, even if you do not need a background thread or Bitmap Drawable
Glide.with(context)
.asBitmap()
.load(url)
.into(new Target<Bitmap>() {
...
});
- Placeholder
Glide allows the user to specify three different types of placeholders, were used in three different scenes
- placeholder
placeholder is being performed when Drawable request being presented. When a request is successfully completed, replace the placeholder character resources to be requested. - error
error Drawable show when requesting permanent failure - fallback
fallback Drawable show When requested url / model is null.
GlideApp.with(fragment)
.load(url)
.placeholder(R.drawable.placeholder) // error(R.drawable.error) | fallback(new ColorDrawable(Color.GREY))
.into(view);
- Options
- RequestBuilder
RequestBuilder () returns by Glide.with, can be applied placeHolder, Transformations, cacheStrategies, Component options
RequestBuilder<Drawable> requestBuilder = Glide.with(fragment).asDrawable();
RequestBuilder<Drawable> requestBuilder = Glide.with(fragment).load(url);
- RequestOptions
RequestOptions cropOptions = new RequestOptions().centerCrop(context);
...
Glide.with(fragment)
.load(url)
.apply(cropOptions)
.into(imageView);
Here is the main explanation of apply()
the method, it can be called many times, if the call will be repeated, subject to final
- TransitionOptions
It is mainly used in the request after loading is complete, we can use it to fade in, fade into the placeholder, no
Glide.with(fragment)
.load(url)
.transition(withCrossFade())
.into(view);
- Thumbnail Request
loads the thumbnail
Glide.with(fragment)
.load(url)
.thumbnail(Glide.with(fragment) // thumbnail(/*sizeMultiplier=*/ 0.25f) 原图的百分比
.load(thumbnailUrl))
.into(imageView);
- Component Options
is used mainly to components, such asModelLoaders
,ResourceDecoders
,ResourceEncoders
,Encoders
etc.
Glide.with(context)
.load(url)
.apply(option(MyCustomModelLoader.TIMEOUT_MS, 1000L))
.into(imageView);
RequestOptions options = new RequestOptions()
.set(MyCustomModelLoader.TIMEOUT_MS, 1000L);
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
- Transformation
is access to resources and modify it, and then return resources after being modified. Transform operation is typically used to complete the cut or filter, may also be used to convert an animated GIF, or even a custom resource types.
- Built-in conversion
CenterCrop
,FitCenter
,CircleCrop
RequestOptions options = new RequestOptions();
options.centerCrop();
Glide.with(fragment)
.load(url)
.apply(options)
.into(imageView);
- Multiple transformation
Glide.with(fragment)
.load(url)
.transform(new MultiTransformation(new FitCenter(), new YourCustomTransformation())
.into(imageView);
- Custom conversion
is mainly inheritedBitmapTransformation
, then rewritetransform
methods, andequals()
,hashCode()
,updateDiskCacheKey()
- Target
Target<Drawable> target =
Glide.with(fragment)
.load(url)
.into(new Target<Drawable>() {
...
});
...
// Some time in the future:
Glide.with(fragment)
.load(newUrl)
.into(target);
- Transition
in Glide, the image may be loaded from any one of four places in:- Glide memory cache
- Glide disk cache
- A source device locally available files or Uri
- Url only one source or available remotely Uri
- Configuration
- Add components
- Add to
AppGlideModule
- (Optional) Add
LibraryGlideModule
one or more - module needs to add
@GlideModule
comment - Add gradle dependence
- In proguard, add to
AppGlideModule
the keep
- Add to
- Cache
// 内存缓存
@GlideModule
public class YourAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
.setMemoryCacheScreens(2)
.build();
builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
}
}
// Bitmap池
@GlideModule
public class YourAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
.setBitmapPoolScreens(3)
.build();
builder.setBitmapPool(new LruBitmapPool(calculator.getBitmapPoolSize()));
}
}
// 磁盘缓存
@GlideModule
public class YourAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDiskCache(new ExternalCacheDiskCacheFactory(context));
}
}
- The default option
@GlideModule
public class YourAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDefaultRequestOptions(
new RequestOptions()
.format(DecodeFormat.RGB_565)
.disallowHardwareBitmaps());
// 在加载图片时假如发生了一个异常 (例如, OOM),
//Glide 将会使用一个 GlideExecutor.UncaughtThrowableStrategy
final UncaughtThrowableStrategy myUncaughtThrowableStrategy = new ...
builder.setDiskCacheExecutor(newDiskCacheExecutor(myUncaughtThrowableStrategy));
builder.setResizeExecutor(newSourceExecutor(myUncaughtThrowableStrategy));
}
}
Once the new request is created, these options will be on GlideBuilder by the setDefaultRequestOptions be applied. Therefore, any request for a separate option in the application will override option GlideBuilder in conflict settings.
- Cache
GlideApp.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL) // 本地和远程
// .onlyRetrieveFromCache(true) 仅从缓存加载图片
// .skipMemoryCache(true) 跳过缓存
// .signature(new ObjectKey(yourVersionMetadata)) 定制签名用于定制缓存
.into(imageView);
- Resource reuse
each callinto()
to load a resource, the resource reference count is incremented by one. If the same resources are loaded into two differentTarget
, then after two loading completed, will be its reference count is two. We canclear()
reduce the count and request new resources. When the count is 0, the resource is released to Glide to reuse.
We can also use the configuration BitmapPool mentioned
Reproduced in: https: //www.jianshu.com/p/8a993d66dcb2