Glide源码阅读之建造者(builder)模式2【RequestBuilder】
Glide多种组合使用方式记录–没有全部亲测,大家可以根据实际需要选用
Glide设计模式之建造者(builder)模式1【GlideBuilder】
Glide设计模式之建造者(builder)模式2【RequestBuilder】
Glide设计模式之建造者(builder)模式3【RequestOptions】【BaseRequestOptions】
Glide设计模式之建造者(builder)模式4总结【MemorySizeCalculator】【GlideExecutor】【PreFillType】【LazyHeaders】
官方定义
Glide设计模式之builder模式1【GlideBuilder】已经有过builder模式的相关介绍和说明这里就不重复了。没有看过的可以看看之前的GlideBuilder篇!
使用形式
//第一种 官方形式
Glide.with(fragment).load(url).into(imageView);
//第二种 单元测试形式
Glide.with(app)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.skipMemoryCache(true)
.override(targetSize)
.load(resourceId)
.listener(
new RequestListener<Bitmap>() {
。。。)
.submit();
//第三种形式
Bitmap bitmap =
concurrency.get(
Glide.with(context).asBitmap().load(getDataUri(CompressFormat.JPEG)).submit())
从源码的角度即可看出调用的顺序和规律。前面涉及GlideBuilder构造,后面就是RequestBuilder接着就是into(imageView)待加载的目标了。
RequestBuilder
com.bumptech.glide.RequestBuilder
下面列出返回RequestBuilder的公共调用设置
@NonNull
/**
* Applies the given options to the request.
*
* <p>As with {@link RequestOptions#apply(BaseRequestOptions)}, {@code #apply} only replaces those
* values that are explicitly set in the given {@link RequestOptions} object. If you need to
* completely reset all previously set options, create a new {@code RequestBuilder} instead of
* using this method.
*
* @see RequestOptions#apply(BaseRequestOptions)
* @return This request builder.
*/
将给定的选项应用于请求。
与BaseRequestOptions.apply(BaseRequestOptions)一样,#apply只替换给定RequestOptions对象中显式设置的值。如果你需要完全重置所有之前设置的选项,创建一个新的RequestBuilder而不是使用这个方法。
覆盖:
应用在类BaseRequestOptions<RequestBuilder<TranscodeType>>
返回:
这个请求构建器。
参见:
BaseRequestOptions.apply (BaseRequestOptions)
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> apply(@NonNull BaseRequestOptions<?> requestOptions) {
。。。
/**
* Sets the {@link TransitionOptions} to use to transition from the placeholder or thumbnail when
* this load completes.
*
* <p>The given {@link TransitionOptions} will replace any {@link TransitionOptions} set
* previously.
*
* @return This request builder.
*/
设置TransitionOptions,以便在加载完成时从占位符或缩略图进行转换。
给定的TransitionOptions将替换之前设置的任何TransitionOptions。
返回:
这个请求构建器。
@NonNull
@CheckResult
public RequestBuilder<TranscodeType> transition(
@NonNull TransitionOptions<?, ? super TranscodeType> transitionOptions) {
...
/**
* Sets a {@link RequestListener} to monitor the resource load and removes all previously set
* listeners (either via this method or from {@link #addListener(RequestListener)} .
*
* <p>Calls to this method will replace previously set listeners. To set multiple listeners, use
* {@link #addListener} instead.
*
* @param requestListener The request listener to use.
* @return This request builder.
*/
设置一个RequestListener来监视资源加载,并删除所有以前设置的侦听器(通过这个方法或从addListener(RequestListener)。要设置多个监听器,请使用
addListener代替。
*
* @param requestListener要使用的请求监听器。
* @return此请求生成器。
@NonNull
@CheckResult
@SuppressWarnings("unchecked")
public RequestBuilder<TranscodeType> listener(
@Nullable RequestListener<TranscodeType> requestListener) {
...
/**
* Adds a {@link RequestListener} to the list that will be called in the order they were added
* when the request ends.
*
* <p>Multiple calls to this method append additional listeners. Previous listeners are not
* removed. If you want to replace any previously added listeners, use {@link
* #listener(RequestListener)}.
*
* <p>Listeners track the state of the request started by this particular {@code builder}. When
* used with the thumbnail APIs ({@link #thumbnail(RequestBuilder)}) this can start to seem
* confusing because multiple requests are running and each may succeed or fail, independent of
* each other. As a rule, Glide does not add {@link RequestListener}s to thumbnail requests
* automatically. That means that {@link RequestListener}s track the state of exactly one request
* in the chain. For example, if you start a primary request with a single nested thumbnail and
* you add a {@link RequestListener} only to the primary request, then the {@link RequestListener}
* will only be notified when the primary request succeeds or fails. If the thumbnail succeeds,
* but the primary request fails, the {@link RequestListener} added to the primary request will
* still be called with {@link RequestListener#onLoadFailed(GlideException, Object, Target,
* boolean)}. In the same scenario, the {@link RequestListener} added only to the primary request
* will not have {@link RequestListener#onResourceReady(Object, Object, Target, DataSource,
* boolean)} called when the thumbnail request finishes successfully. Similarly, if you add a
* {@link RequestListener} only to a thumbnail request, but not the primary request, that {@code
* listener} will only be called for changes related to the thumbnail request. If the thumbnail
* request fails, the {@code listener} added to the thumbnail request will be immediately called
* via {@link RequestListener#onLoadFailed(GlideException, Object, Target, boolean)}, even though
* the primary request may eventually succeed. It is perfectly possible to add a {@link
* RequestListener} to both the primary and a thumbnail request. If you do so, the {@link
* RequestListener} will be called independently for each request when it finishes. Keep in mind
* that if any parent request finishes before its thumbnail request(s), it will attempt to cancel
* those requests. As a result there's no guarantee that a {@link RequestListener} added to a
* thumbnail request will actually be called with either success or failure. These same patterns
* hold for arbitrarily nested thumbnails. The {@code listener} is only called for the requests it
* is added to and may not be called for every thumbnail request if those requests are cancelled
* due to the completion of a parent request.
*
* <p>The one exception to the rules about thumbnails is {@link #thumbnail(float)}. In this case
* we appear to be passing {@link RequestListener}s added to the parent request to the generated
* thumbnail requests. To try to reduce confusion, the {@link #thumbnail(float)} method has been
* deprecated. It can be easily replicated using {@link #thumbnail(RequestBuilder)} and {@link
* BaseRequestOptions#sizeMultiplier(float)}.
*
* <p>Often in UIs it's desirable to try to track the overall status of a request, including the
* thumbnails. For example, you might want to load an image, start an animation if the
* asynchronous image load succeeds and perform some fallback action if it fails. If you're using
* a single primary request, {@link RequestListener} will work for this. However, if you then
* decide to try to make things more performant by adding a thumbnail (or multiple thumbnails),
* {@link RequestListener} is awkward because either you only add it to the main request and it's
* not called when the thumbnails complete (which defeats the purpose) or it's called for every
* request and it's hard to keep track of when the overall request has failed. A better option
* than using {@link RequestListener} to track the state of the UI then is to use {@link Target}
* instead. {@link Target#onResourceReady(Object, Transition)} will be called when any thumbnail
* finishes, which you can use to trigger your animation starting. {@link
* Target#onLoadFailed(Drawable)} will only be called if every request in the chain, including the
* primary request, fails, which you can use to trigger your fallback behavior. Be sure to pick an
* appropriate {@link Target} subclass when possible, like {@link
* com.bumptech.glide.request.target.BitmapImageViewTarget} or {@link
* com.bumptech.glide.request.target.DrawableImageViewTarget} when loading into {@link ImageView}
* or {@link com.bumptech.glide.request.target.CustomTarget} when using custom rendering. Don't
* forget to call {@code super()} in the {@code ImageViewTarget}s.
*
* <p>It's best to create a single instance of an exception handler per type of request (usually
* activity/fragment) rather than pass one in per request to avoid some redundant object
* allocation.
*
* @param requestListener The request listener to use. If {@code null}, this method is a noop.
* @return This request builder.
*/
将RequestListener添加到列表中,该列表将按照请求结束时添加的顺序调用。
<p>对该方法的多次调用会附加额外的监听器。以前的侦听器不会被删除。如果你想替换之前添加的监听器,使用#listener(RequestListener) <p>监听器跟踪由这个特定的生成器When启动的请求的状态
使用缩略图api (#thumbnail(RequestBuilder)),这可能开始看起来
之所以令人困惑,是因为有多个请求正在运行,每个请求都可能成功或失败,独立于
对方。作为规则,Glide不会将RequestListeners添加到缩略请求中
自动。这意味着RequestListeners只跟踪一个请求的状态
在链。例如,如果您用一个嵌套的缩略图和开始一个主请求
添加RequestListeneronly到主请求,然后添加RequestListener
仅在主请求成功或失败时才会得到通知。如果缩略图成功,
但是主请求失败,添加到主请求的requestlistener将失败
仍然被RequestListener#onLoadFailed(GlideException, Object, Target,
在相同的场景中,requestlistener只添加到主请求
将没有RequestListener#onResourceReady(对象,对象,目标,数据源,
当缩略图请求成功完成时调用。类似地,如果加上a
RequestListener只对缩略请求,而不是主请求
侦听器只会在与缩略图请求相关的更改时被调用。如果缩略图
请求失败,添加到缩略图请求的侦听器将立即被调用
通过RequestListener#onLoadFailed(GlideException, Object, Target, boolean),即使
主要请求最终可能会成功。完全有可能加一个
RequestListener同时用于主请求和缩略请求。如果你这样做,
RequestListener将在每个请求完成时被独立地调用。请记住
如果任何父请求在其缩略请求之前完成,它将尝试取消
这些请求。因此,不能保证将RequestListener添加到
缩略图请求将实际调用,成功或失败。这些相同的模式
保持为任意嵌套的缩略图。侦听器只对它的请求调用
添加到每个缩略图请求,如果这些请求被取消,这些请求可能不会被调用
由于父请求的完成。
目标#onLoadFailed(Drawable)将只被调用,如果链中的每个请求,包括
主请求失败,您可以使用它来触发您的回退行为。一定要选一个
适当的目标子类,如
com.bumptech.glide.request.target.BitmapImageViewTarget或
当加载到ImageView时,drawableimageviewtarget
或com.bumptech.glide.request.target.CustomTarget时使用自定义呈现。不
忘记在ImageViewTargets中调用super()。
最好为每一种请求类型(通常)创建一个异常处理程序实例
Activity /fragment)而不是在每个请求中传递一个,以避免一些冗余对象
分配。
@param requestListener要使用的请求监听器。如果为空,则此方法为noop。
这个请求生成器。
@NonNull
@CheckResult
public RequestBuilder<TranscodeType> addListener(
@Nullable RequestListener<TranscodeType> requestListener) {
...
/**
* Sets a {@link RequestBuilder} that is built and run if the load started by this {@link
* RequestBuilder} fails.
*
* <p>If this {@link RequestBuilder} uses a thumbnail that succeeds the given error {@link
* RequestBuilder} will be started anyway if the non-thumbnail request fails.
*
* <p>Recursive calls to this method as well as calls to {@link #thumbnail(float)} and {@link
* #thumbnail(RequestBuilder)} are supported for the given error {@link RequestBuilder}.
*
* <p>Unlike {@link #thumbnail(RequestBuilder)} and {@link #thumbnail(float)}, no options from
* this primary {@link RequestBuilder} are propagated to the given error {@link RequestBuilder}.
* Options like priority, override widths and heights and transitions must be applied
* independently to the error builder.
*
* <p>The given {@link RequestBuilder} will start and potentially override a fallback drawable if
* it's set on this {@link RequestBuilder} via {@link
* RequestOptions#fallback(android.graphics.drawable.Drawable)} or {@link
* RequestOptions#fallback(int)}.
*
* @return This {@link RequestBuilder}.
*/
设置一个RequestBuilder,如果由该RequestBuilder启动的加载失败,该RequestBuilder将被构建并运行。
如果这个RequestBuilder使用一个缩略图成功,给出的错误RequestBuilder将启动无论如何,如果非缩略图请求失败。
对于给定的错误RequestBuilder,此方法的递归调用以及对缩略图(float)和缩略图(RequestBuilder)的调用都被支持。
不像缩略图(RequestBuilder)和缩略图(浮动),没有选项从这个主要的RequestBuilder传播到给定的错误RequestBuilder。像优先级、覆盖宽度和高度以及转换等选项必须独立应用于错误构建器。
给定的RequestBuilder将启动并潜在覆盖一个回退绘制,如果它通过BaseRequestOptions.fallback(android.graphics.drawable.Drawable)或BaseRequestOptions.fallback(int)设置在这个RequestBuilder上。
返回:
这RequestBuilder。
@NonNull
public RequestBuilder<TranscodeType> error(@Nullable RequestBuilder<TranscodeType> errorBuilder) {
...
/**
* Identical to calling {@link #error(RequestBuilder)} where the {@code RequestBuilder} is the
* result of calling {@link #clone()} and removing any existing thumbnail and error {@code
* RequestBuilders}.
*
* <p>Other than thumbnail and error {@code RequestBuilder}s, which are removed, all other options
* are retained from the primary request. However, <b>order matters!</b> Any options applied after
* this method is called will not be applied to the error {@code RequestBuilder}.
*
* <p>WARNING: Calling this method with a {@code model} whose type does not match the type of the
* model passed to {@code load()} may be dangerous! Any options that were applied by the various
* type specific {@code load()} methods, like {@link #load(byte[])} will be copied to the error
* request here even if the {@code model} you pass to this method doesn't match. Similary, options
* that would be normally applied by type specific {@code load()} methods will <em>not</em> be
* applied to this request. If this behavior is confusing or unexpected, use {@link
* #error(RequestBuilder)} instead.
*/
相同的调用#错误(RequestBuilder)的RequestBuilder是
调用#clone()并删除任何现有缩略图的结果和错误
RequestBuilders。
<p>除了缩略图和错误RequestBuilders,这是删除的,所有其他选项
从主要请求中保留。然而,< b >订单事宜!</b>后应用的任何选项
调用此方法将不会应用于错误RequestBuilder。
<p>警告:调用该方法时使用的模型的类型与
传递给load()的模型可能是危险的!各种各样应用的任何选项
特定类型的load()方法,如#load(byte[])将被复制到错误中
在这里请求,即使你传递给这个方法的模型不匹配。同样,选择
通常由特定类型的load()方法应用的将是<em>而不是</em> be
适用于此请求。如果这种行为令人困惑或出乎意料,请使用
# (RequestBuilder)而不是错误。
@NonNull
@CheckResult
public RequestBuilder<TranscodeType> error(Object model) {
...
/**
* Loads and displays the resource retrieved by the given thumbnail request if it finishes before
* this request. Best used for loading thumbnail resources that are smaller and will be loaded
* more quickly than the full size resource. There are no guarantees about the order in which the
* requests will actually finish. However, if the thumb request completes after the full request,
* the thumb resource will never replace the full resource.
*
* <p>Recursive calls to thumbnail are supported.
*
* <p>Overrides any previous calls to this method, {@link #thumbnail(float)} and {@link
* #thumbnail(RequestBuilder[])}.
*
* @see #thumbnail(float)
* @see #thumbnail(RequestBuilder[])
* @param thumbnailRequest The request to use to load the thumbnail.
* @return This request builder.
*/
加载并显示由给定的缩略图请求检索的资源(如果它在此请求之前完成)。最好用于加载较小的缩略图资源,将比加载完整大小的资源更快。并不能保证请求实际完成的顺序。但是,如果thumb请求在完整请求之后完成,则thumb资源将永远不会替换完整资源。
支持对缩略图的递归调用。
覆盖之前对该方法的任何调用,缩略图(浮动)和缩略图(RequestBuilder[])。
参数:
thumbnailRequest—用来加载缩略图的请求。
返回:
这个请求构建器。
参见:
缩略图(浮动),缩略图(RequestBuilder [])
@NonNull
@CheckResult
@SuppressWarnings("unchecked")
public RequestBuilder<TranscodeType> thumbnail(
@Nullable RequestBuilder<TranscodeType> thumbnailRequest) {
...
/**
* Recursively applies {@link #thumbnail(RequestBuilder)} so that the {@link RequestBuilder}s are
* loaded as thumbnails in the given priority order.
*
* <p>{@link #thumbnail(RequestBuilder)} is applied in the order given so that the {@link
* RequestBuilder} at position 0 has the {@link RequestBuilder} at position 1 applied as using its
* thumbnail method, the {@link RequestBuilder} at position 1 has the {@link RequestBuilder} at
* position 2 applied using its thumbnail method and so on.
*
* <p>Calling this method with an {@code null} array of {@link RequestBuilder} thumbnails or an
* empty array of {@link RequestBuilder} thumbnails is equivalent to calling {@link
* #thumbnail(RequestBuilder)} with {@code null}.
*
* <p>Any individual {@link RequestBuilder} in the array of thumbnails provided here may be {@code
* null}. {@code null} {@link RequestBuilder}s are ignored and excluded from the recursive chain.
*
* <p>The {@link RequestBuilder} objects provided here may be mutated and have any previous calls
* to this method or {@link #thumbnail(RequestBuilder)} methods overridden.
*
* <p>Overrides any previous calls to {@link #thumbnail(RequestBuilder)}, {@link
* #thumbnail(float)} and this method.
*
* @see #thumbnail(float)
* @see #thumbnail(RequestBuilder)
* @return This request builder.
*/
递归应用缩略图(RequestBuilder),以便RequestBuilder加载的缩略图在给定的优先级顺序。
缩略图的顺序(RequestBuilder)是应用在位置0,这样RequestBuilder RequestBuilder在位置1应用使用其缩略图的方法,在位置1 RequestBuilder RequestBuilder位置2应用使用其缩略图方法等等。
用一个空的RequestBuilder缩略图数组或一个空的RequestBuilder缩略图数组调用这个方法等同于用null调用thumbnail(RequestBuilder)。
这里提供的缩略图数组中的任何单独的RequestBuilder都可能是空的。RequestBuilders被忽略,并被排除在递归链中。
这里提供的RequestBuilder对象可能会发生突变,并覆盖之前对该方法或缩略图(RequestBuilder)方法的任何调用。
覆盖任何先前对thumbnail(RequestBuilder), thumbnail(float)和这个方法的调用。
返回:
这个请求构建器。
参见:
缩略图(浮动),缩略图(RequestBuilder)
@SuppressWarnings({"CheckResult", "unchecked"})
@NonNull
@CheckResult
public RequestBuilder<TranscodeType> thumbnail(
@Nullable RequestBuilder<TranscodeType>... thumbnails) {
...
/**
* Recursively applies {@link #thumbnail(RequestBuilder)} so that the {@link RequestBuilder}s are
* loaded as thumbnails in the given priority order.
*
* <p>{@link #thumbnail(RequestBuilder)} is applied in the order given so that the {@link
* RequestBuilder} at position 0 has the {@link RequestBuilder} at position 1 applied as using its
* thumbnail method, the {@link RequestBuilder} at position 1 has the {@link RequestBuilder} at
* position 2 applied using its thumbnail method and so on.
*
* <p>Calling this method with a {@code null} list of {@link RequestBuilder} thumbnails or an
* empty list of {@link RequestBuilder} thumbnails is equivalent to calling {@link
* #thumbnail(RequestBuilder)} with {@code null}.
*
* <p>Any individual {@link RequestBuilder} in the list of thumbnails provided here may be {@code
* null}. {@code null} {@link RequestBuilder}s are ignored and excluded from the recursive chain.
*
* <p>The {@link RequestBuilder} objects provided here may be mutated and have any previous calls
* to this method or {@link #thumbnail(RequestBuilder)} methods overridden.
*
* <p>Overrides any previous calls to {@link #thumbnail(RequestBuilder)}, {@link
* #thumbnail(float)} and this method.
*
* @see #thumbnail(float)
* @see #thumbnail(RequestBuilder)
* @return This request builder.
*/
同上
@SuppressWarnings({"CheckResult", "unchecked"})
@NonNull
@CheckResult
public RequestBuilder<TranscodeType> thumbnail(
@Nullable List<RequestBuilder<TranscodeType>> thumbnails) {
...
/**
* Loads a resource in an identical manner to this request except with the dimensions of the
* target multiplied by the given size multiplier. If the thumbnail load completes before the full
* size load, the thumbnail will be shown. If the thumbnail load completes after the full size
* load, the thumbnail will not be shown.
*
* <p>Note - The thumbnail resource will be smaller than the size requested so the target (or
* {@link ImageView}) must be able to scale the thumbnail appropriately. See {@link
* android.widget.ImageView.ScaleType}.
*
* <p>Almost all options will be copied from the original load, including the {@link
* com.bumptech.glide.load.model.ModelLoader}, {@link com.bumptech.glide.load.ResourceDecoder},
* and {@link com.bumptech.glide.load.Transformation}s. However, {@link
* com.bumptech.glide.request.RequestOptions#placeholder(int)} and {@link
* com.bumptech.glide.request.RequestOptions#error(int)}, and {@link #listener(RequestListener)}
* will only be used on the full size load and will not be copied for the thumbnail load.
*
* <p>Recursive calls to thumbnail are supported.
*
* <p>Overrides any previous calls to this method, {@link #thumbnail(RequestBuilder[])}, and
* {@link #thumbnail(RequestBuilder)}.
*
* @see #thumbnail(RequestBuilder)
* @see #thumbnail(RequestBuilder[])
* @param sizeMultiplier The multiplier to apply to the {@link Target}'s dimensions when loading
* the thumbnail.
* @return This request builder.
* @deprecated The behavior differences between this method and {@link #thumbnail(RequestBuilder)}
* are subtle, hard to understand for users and hard to maintain for developers. See the
* javadoc on {@link #listener(RequestListener)} for one concrete example of the behavior
* differences and complexity introduced by this method. Better consistency and readability
* can be obtained by calling {@link #thumbnail(RequestBuilder)} with a duplicate {@code
* RequestBuilder} on which you have called {@link BaseRequestOptions#sizeMultiplier(float)}.
* In practice this method also isn't especially useful. It's much more common to want to
* specify a number of different attributes for thumbnails than just a simple percentage
* modifier on the target size, so there's little justification for keeping this method. This
* method will be removed in a future version of Glide.
*/
以与此请求相同的方式加载资源,只是目标的维度乘以给定的大小乘数。如果缩略图加载在完整尺寸加载之前完成,缩略图将显示出来。如果缩略图加载完成后,全尺寸加载,缩略图将不会显示。
注意:缩略图资源将小于请求的大小,所以目标(或ImageView)必须能够适当地缩放缩略图。看到ImageView.ScaleType。
几乎所有选项都将从原始加载中复制,包括ModelLoader、ResourceDecoder和transforms。然而,BaseRequestOptions.placeholder(int)和BaseRequestOptions.error(int),以及listener(RequestListener)将只用于完整大小加载,不会复制缩略图加载。
支持对缩略图的递归调用。
覆盖之前对该方法的任何调用,缩略图(RequestBuilder[])和缩略图(RequestBuilder)。
参数:
sizeMultiplier—加载缩略图时应用于目标尺寸的乘数。
返回:
这个请求构建器。
参见:
RequestBuilder缩略图,缩略图(RequestBuilder [])
@deprecated 这个方法和#thumbnail(RequestBuilder)之间的行为差异
都很微妙,用户很难理解,开发人员也很难维护。看到
javadoc上的#listener(RequestListener)的一个具体的行为例子
这种方法的差异和复杂性。更好的一致性和可读性
可以通过调用#thumbnail(RequestBuilder)与一个副本{@code
RequestBuilder上,你已经调用BaseRequestOptions#sizeMultiplier(float)。
在实践中,这种方法也不是特别有用。更常见的是想要
为缩略图指定多个不同的属性,而不仅仅是一个简单的百分比
修改器的目标大小,所以没有什么理由保留这个方法。这
方法将在未来版本的Glide中删除。
@NonNull
@CheckResult
@SuppressWarnings("unchecked")
@Deprecated
public RequestBuilder<TranscodeType> thumbnail(float sizeMultiplier) {
...
/**
* Sets the specific model to load data for.
*
* @param model The model to load data for, or null.
* @return This request builder.
*/
设置要加载数据的特定模型。
参数:
model—要加载数据的模型,或为空。
返回:
这个请求构建器。
@NonNull
@CheckResult
@SuppressWarnings("unchecked")
@Override
public RequestBuilder<TranscodeType> load(@Nullable Object model) {
...
/**
* Returns an object to load the given {@link Bitmap}.
*
* <p>It's almost always better to allow Glide to load {@link Bitmap}s than pass {@link Bitmap}s
* into Glide. If you have a custom way to obtain {@link Bitmap}s that is not supported by Glide
* by default, consider registering a custom {@link com.bumptech.glide.load.model.ModelLoader} or
* {@link com.bumptech.glide.load.ResourceDecoder} instead of using this method.
*
* <p>The {@link DiskCacheStrategy} is set to {@link DiskCacheStrategy#NONE}. Previous calls to
* {@link #apply(BaseRequestOptions)} or previously applied {@link DiskCacheStrategy}s will be
* overridden by this method. Applying an {@link DiskCacheStrategy} other than {@link
* DiskCacheStrategy#NONE} after calling this method may result in undefined behavior.
*
* <p>In memory caching relies on Object equality. The contents of the {@link Bitmap}s are not
* compared.
*
* @see #load(Object)
*/
返回一个对象来加载给定的位图。
允许Glide加载位图比将位图传递到Glide中要好得多。如果你有一个自定义的方法来获取bitmap,而这个方法是Glide默认不支持的,考虑注册一个自定义的ModelLoader或ResourceDecoder,而不是使用这个方法。
DiskCacheStrategy设置为DiskCacheStrategy. none。以前调用apply(BaseRequestOptions)或以前应用的DiskCacheStrategys将被此方法覆盖。应用非DiskCacheStrategy的DiskCacheStrategy。在调用此方法之后,NONE可能会导致未定义的行为。
在内存中缓存依赖于对象相等。位图的内容不进行比较。
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@Nullable Bitmap bitmap) {
...
/**
* Returns a request builder to load the given {@link Drawable}.
*
* <p>It's almost always better to allow Glide to load {@link Bitmap}s than to pass {@link
* Bitmap}s into Glide using this method . If you have a custom way to obtain {@link Bitmap}s that
* is not supported by Glide by default, consider registering a custom {@link
* com.bumptech.glide.load.model.ModelLoader} or {@link com.bumptech.glide.load.ResourceDecoder}
* instead of using this method.
*
* <p>The {@link DiskCacheStrategy} is set to {@link DiskCacheStrategy#NONE}. Previous calls to
* {@link #apply(BaseRequestOptions)} or previously applied {@link DiskCacheStrategy}s will be
* overridden by this method. Applying an {@link DiskCacheStrategy} other than {@link
* DiskCacheStrategy#NONE} after calling this method may result in undefined behavior.
*
* <p>In memory caching relies on Object equality. The contents of the {@link Drawable}s are not
* compared.
*
* @see #load(Object)
*/
返回一个请求构建器来加载给定的Drawable。
让Glide加载位图总是比用这种方法将位图传递给Glide要好。如果你有一个自定义的方法来获取bitmap,而这个方法是Glide默认不支持的,考虑注册一个自定义的ModelLoader或ResourceDecoder,而不是使用这个方法。
DiskCacheStrategy设置为DiskCacheStrategy. none。以前调用apply(BaseRequestOptions)或以前应用的DiskCacheStrategys将被此方法覆盖。应用非DiskCacheStrategy的DiskCacheStrategy。在调用此方法之后,NONE可能会导致未定义的行为。
在内存中缓存依赖于对象相等。可绘制表的内容不进行比较。
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@Nullable Drawable drawable) {
...
/**
* Returns a request builder to load the given {@link java.lang.String}.
*
* <p>Note - this method caches data using only the given String as the cache key. If the data is
* a Uri outside of your control, or you otherwise expect the data represented by the given String
* to change without the String identifier changing, Consider using {@link
* com.bumptech.glide.request.RequestOptions#signature(com.bumptech.glide.load.Key)} to mixin a
* signature you create that identifies the data currently at the given String that will
* invalidate the cache if that data changes. Alternatively, using {@link
* com.bumptech.glide.load.engine.DiskCacheStrategy#NONE} and/or {@link
* com.bumptech.glide.request.RequestOptions#skipMemoryCache(boolean)} may be appropriate.
*
* @see #load(Object)
* @param string A file path, or a uri or url handled by {@link
* com.bumptech.glide.load.model.UriLoader}.
*/
返回一个请求构建器来加载给定的String。
注意:这个方法只使用给定的String作为缓存键来缓存数据。如果数据是一个您无法控制的Uri,或者您希望给定String所表示的数据在不改变String标识符的情况下改变,考虑使用BaseRequestOptions.signature(com.bumptech.glide.load.Key)来混合您创建的一个签名,该签名识别当前给定String中的数据,如果数据更改,该签名将使缓存无效。另外,使用DiskCacheStrategy。NONE和/或BaseRequestOptions.skipMemoryCache(boolean)可能是合适的。
参数:
一个文件路径,或者UriLoader处理的uri或url。
@NonNull
@Override
@CheckResult
public RequestBuilder<TranscodeType> load(@Nullable String string) {
...
/**
* Returns a request builder to load the given {@link Uri}.
*
* <p>Note - this method caches data at Uris using only the Uri itself as the cache key. The data
* represented by Uris from some content providers may change without the Uri changing, which
* means using this method can lead to displaying stale data. Consider using {@link
* com.bumptech.glide.request.RequestOptions#signature(com.bumptech.glide.load.Key)} to mixin a
* signature you create based on the data at the given Uri that will invalidate the cache if that
* data changes. Alternatively, using {@link
* com.bumptech.glide.load.engine.DiskCacheStrategy#NONE} and/or {@link
* com.bumptech.glide.request.RequestOptions#skipMemoryCache(boolean)} may be appropriate.
*
* @see #load(Object)
* @param uri The Uri representing the image. Must be of a type handled by {@link
* com.bumptech.glide.load.model.UriLoader}.
*/
返回一个请求构建器来加载给定的Uri。
注意:这个方法只使用Uri本身作为缓存键来在Uri中缓存数据。来自某些内容提供者的Uri表示的数据可能会发生变化,而Uri不会发生变化,这意味着使用这种方法可能会导致显示陈旧的数据。考虑使用BaseRequestOptions.signature(com.bumptech.glide.load.Key)来混合一个基于给定Uri上的数据创建的签名,如果数据改变,该签名将使缓存无效。另外,使用DiskCacheStrategy。NONE和/或BaseRequestOptions.skipMemoryCache(boolean)可能是合适的。
参数:
uri -表示图像的uri。必须是UriLoader处理的类型。
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@Nullable Uri uri) {
...
/**
* Returns a request builder to load the given {@link File}.
*
* <p>Note - this method caches data for Files using only the file path itself as the cache key.
* The data in the File can change so using this method can lead to displaying stale data. If you
* expect the data in the File to change, Consider using {@link
* com.bumptech.glide.request.RequestOptions#signature(com.bumptech.glide.load.Key)} to mixin a
* signature you create that identifies the data currently in the File that will invalidate the
* cache if that data changes. Alternatively, using {@link
* com.bumptech.glide.load.engine.DiskCacheStrategy#NONE} and/or {@link
* com.bumptech.glide.request.RequestOptions#skipMemoryCache(boolean)} may be appropriate.
*
* @see #load(Object)
* @param file The File containing the image
*/
返回一个请求构建器来加载给定的File。
注意:这个方法只使用文件路径本身作为缓存键来缓存文件数据。File中的数据可能会更改,因此使用此方法可能会导致显示陈旧的数据。如果你希望文件中的数据发生改变,考虑使用BaseRequestOptions.signature(com.bumptech.glide.load.Key)来混合一个你创建的签名来识别当前文件中的数据,如果数据发生改变,缓存将会失效。另外,使用DiskCacheStrategy。NONE和/或BaseRequestOptions.skipMemoryCache(boolean)可能是合适的。
参数:
file—包含图像的文件
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@Nullable File file) {
...
/**
* Returns a request builder that uses the {@link
* com.bumptech.glide.load.model.ModelLoaderFactory} currently registered or {@link Integer} to
* load the image represented by the given {@link Integer} resource id. Defaults to {@link
* com.bumptech.glide.load.model.ResourceLoader} to load resource id models.
*
* <p>By default this method adds a version code and night mode based signature to the cache key
* used to cache this resource in Glide. This signature is sufficient to guarantee that end users
* will see the most up to date versions of your Drawables, but during development if you do not
* increment your version code before each install and you replace a Drawable with different data
* without changing the Drawable name, you may see inconsistent cached data. To get around this,
* consider using {@link com.bumptech.glide.load.engine.DiskCacheStrategy#NONE} via {@link
* RequestOptions#diskCacheStrategy(com.bumptech.glide.load.engine.DiskCacheStrategy)} during
* development, and re-enabling the default {@link
* com.bumptech.glide.load.engine.DiskCacheStrategy#RESOURCE} for release builds.
*
* <p>This method will load non-{@link android.graphics.Bitmap} resources like {@link
* android.graphics.drawable.VectorDrawable}s. Although Glide makes a best effort to apply {@link
* com.bumptech.glide.load.Transformation}s to these {@link Drawable}s by either extracting the
* underlying {@link Bitmap} or by converting the {@link Drawable} to a {@link Bitmap}, Glide is
* still not able to transform all types of resources. Animated {@link Drawable}s cannot be
* transformed (other than {@link com.bumptech.glide.load.resource.gif.GifDrawable}). To avoid
* load failures if a {@link Drawable} can't be transformed, use the optional transformation
* methods like {@link RequestOptions#optionalTransform(Class, Transformation)}.
*
* <p>In some cases converting {@link Drawable}s to {@link Bitmap}s may be inefficient. Use this
* method, especially in conjunction with {@link com.bumptech.glide.load.Transformation}s with
* caution for non-{@link Bitmap} {@link Drawable}s.
*
* @see #load(Integer)
* @see com.bumptech.glide.signature.AndroidResourceSignature
*/
返回一个请求生成器,该生成器使用当前注册的ModelLoaderFactory或Integer加载由给定的Integer资源id表示的图像。默认为ResourceLoader来加载资源id模型。
默认情况下,该方法会向用于在Glide中缓存该资源的缓存键添加版本代码和基于夜间模式的签名。这个签名足以保证终端用户将看到最新版本的画板,但在开发过程中如果你不增加您的版本代码之前你们每个安装和替换可拉的有不同的数据没有改变可拉的名字,你会看到不一致的缓存数据。要解决这个问题,可以考虑使用DiskCacheStrategy。在开发过程中,通过BaseRequestOptions.diskCacheStrategy(com.bumptech.glide.load.engine.DiskCacheStrategy),重新启用默认的DiskCacheStrategy。发布构建的资源。
该方法将加载非位图资源,如VectorDrawables。虽然Glide尽了最大的努力,通过提取底层的位图或者将绘制图转换为位图,来将转换应用到这些可绘制图上,但是Glide仍然不能转换所有类型的资源。动画Drawables不能被转换(GifDrawable除外)。为了避免加载失败,如果一个Drawable不能被转换,使用可选的转换方法,如BaseRequestOptions。optionalTransform(类、转换)。
在某些情况下,将可绘制图转换为位图可能是低效的。使用这种方法,特别是与转换结合使用时,要注意非位图绘制。
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@RawRes @DrawableRes @Nullable Integer resourceId) {
...
/**
* Returns a request builder to load the given {@link URL}.
*
* @param url The URL representing the image.
* @see #load(Object)
* @deprecated The {@link java.net.URL} class has <a href="http://goo.gl/c4hHNu">a number of
* performance problems</a> and should generally be avoided when possible. Prefer {@link
* #load(android.net.Uri)} or {@link #load(String)}.
*/
弃用。URL类有许多性能问题,通常应该尽可能避免使用。最好是load(android.net.Uri)或load(String)。
返回一个请求构建器来加载给定的URL。
@Deprecated
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@Nullable URL url) {
...
/**
* Returns a request to load the given byte array.
*
* <p>Note - by default loads for bytes are not cached in either the memory or the disk cache.
*
* @param model the data to load.
* @see #load(Object)
*/
返回加载给定字节数组的请求。
注意:默认情况下,字节的加载不会缓存在内存或磁盘缓存中。
参数:
模型——要加载的数据。
@NonNull
@CheckResult
@Override
public RequestBuilder<TranscodeType> load(@Nullable byte[] model) {
....
/**
* Returns a copy of this request builder with all of the options put so far on this builder.
*
* <p>This method returns a "deep" copy in that all non-immutable arguments are copied such that
* changes to one builder will not affect the other builder. However, in addition to immutable
* arguments, the current model is not copied so changes to the model will affect both builders.
*/
@SuppressWarnings({
// we don't want to throw to be user friendly
"PMD.CloneThrowsCloneNotSupportedException"
})
返回该请求构建器的副本,其中包含到目前为止放置在该构建器上的所有选项。
这个方法返回一个“深”复制,因为所有不可变参数都被复制,这样一个构建器的更改将不会影响另一个构建器。然而,除了不可变参数外,当前模型不会被复制复制,因此对模型的更改将影响两个构建器。
覆盖:
克隆类BaseRequestOptions<RequestBuilder<TranscodeType>>
@CheckResult
@Override
public RequestBuilder<TranscodeType> clone() {
...
补充说明
/**
* Set the target the resource will be loaded into.
*
* @param target The target to load the resource into.
* @return The given target.
* @see RequestManager#clear(Target)
*/
设置加载资源的目标。
参数:
target—加载资源的目标。
返回:
给定的目标。
@NonNull
public <Y extends Target<TranscodeType>> Y into(@NonNull Y target) {
...
/**
* Returns a future that can be used to do a blocking get on a background thread.
*
* @param width The desired width in pixels, or {@link Target#SIZE_ORIGINAL}. This will be
* overridden by {@link com.bumptech.glide.request.RequestOptions#override(int, int)} if
* previously called.
* @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be
* overridden by {@link com.bumptech.glide.request.RequestOptions#override(int, int)}} if
* previously called).
*/
返回一个future,该future可用于在后台线程上执行阻塞获取。
参数:
width -期望的宽度,以像素为单位,或者Target.SIZE_ORIGINAL。这将被BaseRequestOptions覆盖。如果先前调用,重写(int, int)。
height -期望的高度,以像素为单位,或Target.SIZE_ORIGINAL。这将被BaseRequestOptions覆盖。如果先前调用,重写(int, int)})。
@NonNull
public FutureTarget<TranscodeType> submit(int width, int height) {
...
/**
* Preloads the resource into the cache using the given width and height.
*
* <p>Pre-loading is useful for making sure that resources you are going to to want in the near
* future are available quickly.
*
* <p>Note - Any thumbnail request that does not complete before the primary request will be
* cancelled and may not be preloaded successfully. Cancellation of outstanding thumbnails after
* the primary request succeeds is a common behavior of all Glide requests. We do not try to
* prevent that behavior here. If you absolutely need all thumbnails to be preloaded individually,
* make separate preload() requests for each thumbnail (you can still combine them into one call
* when loading the image(s) into the UI in a subsequent request).
*
* @param width The desired width in pixels, or {@link Target#SIZE_ORIGINAL}. This will be
* overridden by {@link com.bumptech.glide.request.RequestOptions#override(int, int)} if
* previously called.
* @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be
* overridden by {@link com.bumptech.glide.request.RequestOptions#override(int, int)}} if
* previously called).
* @return A {@link Target} that can be used to cancel the load via {@link
* RequestManager#clear(Target)}.
* @see com.bumptech.glide.ListPreloader
*/
使用给定的宽度和高度将资源预加载到缓存中。
预加载对于确保您在不久的将来能够快速获得所需的资源非常有用。
参数:
width -期望的宽度,以像素为单位,或者Target.SIZE_ORIGINAL。这将被BaseRequestOptions覆盖。如果先前调用,重写(int, int)。
height -期望的高度,以像素为单位,或Target.SIZE_ORIGINAL。这将被BaseRequestOptions覆盖。如果先前调用,重写(int, int)})。
注意:任何在主请求之前未完成的缩略图请求将被
已取消,可能无法成功预加载。取消后的突出缩略图
主请求成功是所有Glide请求的常见行为。我们并不想这么做
在这里阻止这种行为。如果你绝对需要将所有缩略图单独预加载,
为每个缩略图创建单独的preload()请求(您仍然可以将它们合并到一个调用中)
当在后续请求中将图像加载到UI中时)。
@param width期望的像素宽度,或者目标#SIZE_ORIGINAL。这将是
由com.bumptech.glide.request覆盖。RequestOptions #覆盖(int, int)
之前调用。
@param height期望的高度(像素),或者目标#SIZE_ORIGINAL。这将是
由com.bumptech.glide.request覆盖。RequestOptions #覆盖(int, int)
以前叫)。
一个可以用来取消加载的目标
RequestManager #清晰(目标)。
@see com.bumptech.glide.ListPreloader
@NonNull
public Target<TranscodeType> preload(int width, int height) {
...
/**
* Preloads the resource into the cache using {@link Target#SIZE_ORIGINAL} as the target width and
* height. Equivalent to calling {@link #preload(int, int)} with {@link Target#SIZE_ORIGINAL} as
* the width and height.
*
* @return A {@link Target} that can be used to cancel the load via {@link
* RequestManager#clear(Target)}
* @see #preload(int, int)
*/
使用Target将资源预加载到缓存中。SIZE_ORIGINAL作为目标宽度和高度。相当于用Target调用preload(int, int)。SIZE_ORIGINAL作为宽度和高度。
返回:
一个可以通过RequestManager.clear(Target)来取消加载的目标
@NonNull
public Target<TranscodeType> preload() {
...
/**
* Loads the original unmodified data into the cache and calls the given Target with the cache
* File.
*
* @param target The Target that will receive the cache File when the load completes
* @param <Y> The type of Target.
* @return The given Target.
* @deprecated Use {@link RequestManager#downloadOnly()} and {@link #into(Target)}.
*/
弃用。使用RequestManager.downloadOnly()和到(目标)。
将原始未修改的数据加载到缓存中,并使用缓存文件调用给定的Target。
类型参数:
Y -目标的类型。
参数:
target—加载完成时将接收缓存文件的目标
返回:
给定的目标。
@Deprecated
@CheckResult
public <Y extends Target<File>> Y downloadOnly(@NonNull Y target) {
...
/**
* Loads the original unmodified data into the cache and returns a {@link
* java.util.concurrent.Future} that can be used to retrieve the cache File containing the data.
*
* @param width The width in pixels to use to fetch the data.
* @param height The height in pixels to use to fetch the data.
* @return A {@link java.util.concurrent.Future} that can be used to retrieve the cache File
* containing the data.
* @deprecated Use {@link RequestManager#downloadOnly()} and {@link #submit(int, int)}.
*/
弃用。使用RequestManager.downloadOnly()并提交(int, int)。
将原始未修改的数据加载到缓存中,并返回一个Future,该Future可用于检索包含该数据的缓存文件。
参数:
width -用来获取数据的像素宽度。
height—用于获取数据的像素高度。
返回:
一个Future,可以用来检索包含数据的缓存文件。
@Deprecated
@CheckResult
public FutureTarget<File> downloadOnly(int width, int height) {
...
小计
RequestBuilder 的起到了请求配置和应用目标的作用。在完成GlideBuilder的构建配置过程后接着进行RequestBuilder配置构建。接着进行into返回<Y extends Target>进行应用。这个风格和node.js里那种{}.then(…).then(…).catch(…)这样的链式比较相似。
Glide.with(fragment).load(url).into(imageView);
这样的代码风格集配置构建数据请求应用于一行的方式很简洁。类比需要new几个对象然后彼此设置,接着再拉出数据结果进行回调应用到UI上。这样的使用方式丝滑很多。
建议:
如果在工程中有这样配置构建环境、配置构建数据拉取请求、配置构建应用数据列表的结构。即可考虑使用这样的方式。作者本人在解析这个结构的时候发现其实它用在listview里也是个不错的选择:Activity构建环境、网络或者数据库或者数据文件的数据请求配置、接着应用于UI列表里。这个因为listview的子项自定义程度比较高,所以通用的抽象结构比较难推广的其他项目工程。但如果本工程子项相似度高还是可以试试这样的结构的。至少看完本篇完成这个练习。拿出来吹嘘逼格也高了不少哦!
附录1相关类说明
com.bumptech.glide.TransitionOptions
/**
* A base class for setting a transition to use on a resource when a load completes.
*
* @param <CHILD> The implementation of this class to return to chain methods.
* @param <TranscodeType> The type of resource that will be animated.
*/
一个基类,用于设置加载完成时在资源上使用的转换。
com.bumptech.glide.request.target.BitmapImageViewTarget
/**
- A {@link com.bumptech.glide.request.target.Target} that can display an {@link
- android.graphics.Bitmap} in an {@link android.widget.ImageView}.
*/
可以在ImageView中显示Bitmap的目标。
com.bumptech.glide.request.target.DrawableImageViewTarget
/** A target for display {@link Drawable} objects in {@link ImageView}s. */
在ImageViews中显示可绘制对象的目标。
com.bumptech.glide.request.target.CustomTarget
/**
* A base {@link Target} for loading resources ({@link android.graphics.Bitmap}, {@link Drawable}
* etc) that are used outside of {@link android.view.View}s.
*
* <p>If you're loading a resource into a {@link View}, use {@link
* com.bumptech.glide.RequestBuilder#into(ImageView)}, a subclass of {@link ImageViewTarget}, or
* {@link CustomViewTarget}. Using this class to load resources into {@link View}s can prevent Glide
* from correctly cancelling any previous loads, which may result in incorrect images appearing in
* the view, especially in scrolling views like {@link androidx.recyclerview.widget.RecyclerView}.
*
* <p>You <em>MUST</em> implement {@link #onLoadCleared(Drawable)} and ensure that all references to
* any resource passed into the target in {@link #onResourceReady(Object, Transition)} are removed
* before {@link #onLoadCleared(Drawable)} completes. Failing to do so can result in graphical
* corruption, crashes caused by recycled {@link Bitmap}s, and other undefined behavior. It is never
* safe to leave {@link #onLoadCleared(Drawable)} unimplemented or empty. Even if you do not
* manually clear this {@link Target}, Glide may do so automatically after certain lifecycle events
* in {@link androidx.fragment.app.Fragment}s and {@link android.app.Activity}s.
*
* <p>This class can only be used with {@link Target#SIZE_ORIGINAL} or when the desired resource
* dimensions are known when the {@link Target} is created. If you'd like to run some asynchronous
* process and make full use of {@link #getSize(SizeReadyCallback)} and {@link SizeReadyCallback},
* extend {@link Target} directly instead of using this class.
*
* @param <T> The type of resource that will be loaded (e.g. {@link Bitmap}).
*/
一个基本的目标加载资源(位图,绘制等),使用外视图。
如果你正在加载一个资源到一个视图,使用RequestBuilder.into(ImageView),一个子类的ImageViewTarget,或CustomViewTarget。使用这个类将资源加载到视图中可以防止Glide正确地取消以前的加载,这可能会导致错误的图像出现在视图中,特别是在RecyclerView这样的滚动视图中。
你必须实现target . onloadcleared (Drawable),并确保所有对任何资源的引用传递到target中的目标。在Target.onLoadCleared(Drawable)完成之前删除onResourceReady(Object, Transition)。如果做不到这一点,可能会导致图像损坏,bitmap回收导致的崩溃,以及其他未定义的行为。让Target.onLoadCleared(Drawable)未实现或为空永远都不安全。即使你不手动清除这个目标,Glide可能会在Fragments和activity中的某些生命周期事件后自动清除。
这个类只能与Target一起使用。SIZE_ORIGINAL或当创建目标时所需的资源维度已知时。如果你想运行一些异步进程并充分利用getSize(SizeReadyCallback)和SizeReadyCallback,直接扩展Target而不是使用这个类。
com.bumptech.glide.load.model.ModelLoader
/**
* A factory interface for translating an arbitrarily complex data model into a concrete data type
* that can be used by an {@link DataFetcher} to obtain the data for a resource represented by the
* model.
*
* <p>This interface has two objectives: 1. To translate a specific model into a data type that can
* be decoded into a resource.
*
* <p>2. To allow a model to be combined with the dimensions of the view to fetch a resource of a
* specific size.
*
* <p>This not only avoids having to duplicate dimensions in xml and in your code in order to
* determine the size of a view on devices with different densities, but also allows you to use
* layout weights or otherwise programmatically put the dimensions of the view without forcing you
* to fetch a generic resource size.
*
* <p>The smaller the resource you fetch, the less bandwidth and battery life you use, and the lower
* your memory footprint per resource.
*
* @param <Model> The type of the model.
* @param <Data> The type of the data that can be used by a {@link
* com.bumptech.glide.load.ResourceDecoder} to decode a resource.
*/
一种工厂接口,用于将任意复杂的数据模型转换为具体的数据类型,DataFetcher可以使用该数据类型为模型所表示的资源获取数据。
这个接口有两个目标:1。将特定模型转换为可以解码为资源的数据类型。
- 允许模型与视图的维度组合,以获取特定大小的资源。
这不仅避免了重复的维度在xml和代码中为了确定大小的设备上有不同的密度,而且还允许您使用布局权重或通过编程将视图的尺寸没有强迫你去拿一个通用的资源大小。
获取的资源越小,占用的带宽和电池寿命就越短,每个资源占用的内存就越少。
com.bumptech.glide.load.ResourceDecoder
/**
* An interface for decoding resources.
*
* @param <T> The type the resource will be decoded from (File, InputStream etc).
* @param <Z> The type of the decoded resource (Bitmap, Drawable etc).
*/
解码资源的接口。
com.bumptech.glide.load.Transformation
/**
* A class for performing an arbitrary transformation on a resource that implements {@link
* #equals(Object)} and {@link #hashCode()}} to identify the transformation in the memory cache and
* {@link #updateDiskCacheKey(java.security.MessageDigest)}} to identify the transformation in disk
* caches.
*
* <p>Using the fully qualified class name as a static final {@link String} (not {@link
* Class#getName()} to avoid proguard obfuscation) is an easy way to implement {@link
* #updateDiskCacheKey(java.security.MessageDigest)}} correctly. If additional arguments are
* required they can be passed in to the constructor of the {@code Transformation} and then used to
* update the {@link java.security.MessageDigest} passed in to {@link
* #updateDiskCacheKey(MessageDigest)}. If arguments are primitive types, they can typically easily
* be serialized using {@link java.nio.ByteBuffer}. {@link String} types can be serialized with
* {@link String#getBytes(Charset)} using the constant {@link #CHARSET}.
*
* <p>Implementations <em>must</em> implement {@link #equals(Object)} and {@link #hashCode()} for
* memory caching to work correctly.
*
* @param <T> The type of the resource being transformed.
*/
一个用于在实现Key.equals(Object)和Key.hashCode()}的资源上执行任意转换的类,以识别内存缓存中的转换,以及Key.updateDiskCacheKey(java.security.MessageDigest)}来识别磁盘缓存中的转换。
使用完全限定的类名作为静态最终字符串(而不是class . getname()以避免proguard混淆)是正确实现Key.updateDiskCacheKey(java.security.MessageDigest)}的一种简单方法。如果需要额外的参数,可以将它们传递给Transformation的构造函数,然后使用它们更新传递给Key.updateDiskCacheKey(MessageDigest)的MessageDigest。如果参数是基本类型,通常可以使用ByteBuffer轻松地序列化它们。String类型可以使用String. getbytes (Charset)使用常量Key.CHARSET序列化。
实现必须实现Key.equals(Object)和Key.hashCode()以使内存缓存正常工作。
com.bumptech.glide.load.engine.DiskCacheStrategy
/** Set of available caching strategies for media. */
一组可用的媒体缓存策略。(目测用到了策略模式【Strategy Pattern】)
com.bumptech.glide.load.model.ModelLoaderFactory
/**
* An interface for creating a {@link ModelLoader} for a given model type.
*
* <p>The application {@link android.content.Context} can be passed in to the constructor of the
* factory when necessary. It's unsafe to retain {@link android.app.Activity} {@link
* android.content.Context}s in factories. The {@link android.content.Context} can be obtained from
* {@link com.bumptech.glide.module.LibraryGlideModule#registerComponents(Context, Glide, Registry)}
* in most cases.
*
* @param <T> The type of the model the {@link com.bumptech.glide.load.model.ModelLoader}s built by
* this factory can handle
* @param <Y> The type of data the {@link com.bumptech.glide.load.model.ModelLoader}s built by this
* factory can load.
*/
用于为给定模型类型创建ModelLoader的接口。
必要时,可以将应用程序上下文传递给工厂的构造函数。在工厂中保留活动上下文是不安全的。在大多数情况下Context可以从LibraryGlideModule.registerComponents(Context, Glide, Registry)获取。。
com.bumptech.glide.signature.AndroidResourceSignature
/** Includes information about the package as well as whether or not the device is in night mode. */
包括有关包的信息以及设备是否处于夜间模式
更多设计模式解读
单例模式
Glide设计模式之单例模式
空对象模式
Glide设计模式之空对象模式【EmptyModelLoader】【EmptyList<E>
建造者模式
Glide多种组合使用方式记录–没有全部亲测,大家可以根据实际需要选用
Glide设计模式之建造者(builder)模式1【GlideBuilder】
Glide设计模式之建造者(builder)模式2【RequestBuilder】
Glide设计模式之建造者(builder)模式3【RequestOptions】【BaseRequestOptions】
Glide设计模式之建造者(builder)模式4总结【MemorySizeCalculator】【GlideExecutor】【PreFillType】【LazyHeaders】
工厂模式
Glide设计模式之工厂模式1【ModelLoaderFactory】
Glide设计模式之工厂模式2【DiskCache.Factory】
Glide工厂模式3【TransitionFactory】【Transition】
Glide设计模式之工厂模式4总结
自研产品推荐
历时一年半多开发终于smartApi-v1.0.0版本在2023-09-15晚十点正式上线
smartApi是一款对标国外的postman的api调试开发工具,由于开发人力就作者一个所以人力有限,因此v1.0.0版本功能进行精简,大功能项有:
- api参数填写
- api请求响应数据展示
- PDF形式的分享文档
- Mock本地化解决方案
- api列表数据本地化处理
- 再加上UI方面的打磨
为了更好服务大家把之前的公众号和软件激活结合,如有疑问请大家反馈到公众号即可,下个版本30%以上的更新会来自公众号的反馈。
嗯!先解释不上服务端原因,API调试工具的绝大多数时候就是一个数据模型、数据处理、数据模型理解共识的问题解决工具,所以作者结合自己十多年开发使用的一些痛点来打造的,再加上服务端开发一般是面向企业的,作者目前没有精力和时间去打造企业服务。再加上没有资金投入所以服务端开发会滞后,至于什么时候会进行开发,这个要看募资情况和用户反馈综合考虑。虽然目前国内有些比较知名的api工具了,但作者使用后还是觉得和实际使用场景不符。如果有相关吐槽也可以在作者的公众号里反馈蛤!
下面是一段smartApi使用介绍: