引发场景:
使用 Glide 加载高清大图的时候,在加载过程中,显示一个加载进度条,加载结束之后,取消进度条。
相关方法介绍:
void onLoadCleared(Drawable placeholder)
加载时调用生命周期回调,取消了和它的资源释放。一般情况不需要我们操作。
void onLoadFailed(Exception e, Drawable errorDrawable)
加载失败回调,根据需求,可在当前方法中进行图片加载失败的后续操作。
void onLoadStarted(Drawable placeholder)
开始加载图片,可在当前方法中进行加载图片的初始操作。比如,显示加载进度条,
void onResourceReady(Z resource, GlideAnimation<? super Z> glideAnimation)
当前方法表示图片资源加载完成。
Drawable getCurrentDrawable()
获取当前显示的 Drawable
void setDrawable(Drawable drawable)
设置 drawable 显示在当前 ImageView
protected abstract void setResource(Z resource)
该方法暂时不知道用途
方法调用说明:
如果你只是想监听,不想修改Glide的默认行为,你可以继承任何一个Glide对ImageViewTargets的默认实现:
- GlideDrawableImageViewTarget - 默认的Target,用于正常的加载和asGif()。(重写onResourceReady 方法后 .asGif() 可省略 )
- BitmapImageViewTarget - 当使用asBitmap()加载时,使用的默认Target。
开始( onLoadStarted ),完成( onResourceReady ),失败( onLoadFailed )。这三个方法可视情况重写。完成方法在重写的时候需要注意 super.onResourceReady(resource, glideAnimation)
是需要保留的,其他方法中的,则可有可无。
示例代码:
Glide.with(context).load(mImageUrl).placeholder(R.drawable.default_error).into(new GlideDrawableImageViewTarget(mImageView) {
@Override
public void onLoadStarted(Drawable placeholder) {
// 开始加载图片
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), "图片加载失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
super.onResourceReady(resource, glideAnimation);
// 图片加载完成
mImageView.setImageDrawable(resource);
progressBar.setVisibility(View.GONE);
}
});
这样我们就可以在 Glide 加载图片的过程中,进行添加自己的需求逻辑了。
以上属于个人测试结果,如果有什么错误的地方,还请各位指出,谢谢。