使用 Glide 加载高清大图的时候,在加载过程中,显示一个加载进度条,加载结束之后,取消进度条。
Glide ImageViewTarget API
相关方法介绍:
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);
}
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
这样我们就可以在 Glide 加载图片的过程中,进行添加自己的需求逻辑了。
今天主要研究了Glide获取图片Path、Bitmap用法,相信也困扰了大家很久,我在网上也找了很久,基本没有,后来研究了下,也参考了下api文档,总结了以下几个方式:
1. 获取Bitmap:
1)在图片下载缓存好之后获取
- Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
- @Override
- public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
- image.setImageBitmap(resource);
- }
- });
上面是简单方法,下面有全面的方法,可以完美控制:
- Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>() {
- @Override
- public void onLoadStarted(Drawable placeholder) {
-
- }
-
- @Override
- public void onLoadFailed(Exception e, Drawable errorDrawable) {
-
- }
-
- @Override
- public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
-
- }
-
- @Override
- public void onLoadCleared(Drawable placeholder) {
-
- }
-
- @Override
- public void getSize(SizeReadyCallback cb) {
-
- }
-
- @Override
- public void setRequest(Request request) {
-
- }
-
- @Override
- public Request getRequest() {
- return null;
- }
-
- @Override
- public void onStart() {
-
- }
-
- @Override
- public void onStop() {
-
- }
-
- @Override
- public void onDestroy() {
-
- }
- });
2)通过url获取
- Bitmap myBitmap = Glide.with(applicationContext)
- .load(yourUrl)
- .asBitmap()
- .centerCrop()
- .into(500, 500)
- .get()
2. 获取图片缓存路径
- FutureTarget<File> future = Glide.with(mContext)
- .load("url")
- .downloadOnly(500, 500);
- try {
- File cacheFile = future.get();
- String path = cacheFile.getAbsolutePath();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }