Glide ImageViewTarget 加载图片监听以及获取图片Path、Bitmap用法

来源:互联网 发布:淘宝店铺升天猫费用 编辑:程序博客网 时间:2024/05/22 14:18

使用 Glide 加载高清大图的时候,在加载过程中,显示一个加载进度条,加载结束之后,取消进度条。

Glide ImageViewTarget API

相关方法介绍:

 void onLoadCleared(Drawable placeholder)  加载时调用生命周期回调,取消了和它的资源释放。一般情况不需要我们操作。
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
void onLoadFailed(Exception e, Drawable errorDrawable) 加载失败回调,根据需求,可在当前方法中进行图片加载失败的后续操作。
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
void onLoadStarted(Drawable placeholder) 开始加载图片,可在当前方法中进行加载图片的初始操作。比如,显示加载进度条,
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
void onResourceReady(Z resource, GlideAnimation<? super Z> glideAnimation) 当前方法表示图片资源加载完成。
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
Drawable getCurrentDrawable()获取当前显示的 Drawable 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
void setDrawable(Drawable drawable) 设置 drawable 显示在当前 ImageView 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
protected abstract void setResource(Z resource) 该方法暂时不知道用途 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

方法调用说明:

如果你只是想监听,不想修改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)在图片下载缓存好之后获取

[java] view plain copy
  1. Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {  
  2.                 @Override  
  3.                 public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {  
  4.                     image.setImageBitmap(resource);  
  5.                 }  
  6.             }); //方法中设置<span style="font-family: Arial, Helvetica, sans-serif;">asBitmap可以设置回调类型</span>  
上面是简单方法,下面有全面的方法,可以完美控制:

[java] view plain copy
  1. Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>() {  
  2.                 @Override  
  3.                 public void onLoadStarted(Drawable placeholder) {  
  4.                       
  5.                 }  
  6.   
  7.                 @Override  
  8.                 public void onLoadFailed(Exception e, Drawable errorDrawable) {  
  9.   
  10.                 }  
  11.   
  12.                 @Override  
  13.                 public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {  
  14.                      //TODO set bitmap  
  15.                 }  
  16.   
  17.                 @Override  
  18.                 public void onLoadCleared(Drawable placeholder) {  
  19.   
  20.                 }  
  21.   
  22.                 @Override  
  23.                 public void getSize(SizeReadyCallback cb) {  
  24.   
  25.                 }  
  26.   
  27.                 @Override  
  28.                 public void setRequest(Request request) {  
  29.   
  30.                 }  
  31.   
  32.                 @Override  
  33.                 public Request getRequest() {  
  34.                     return null;  
  35.                 }  
  36.   
  37.                 @Override  
  38.                 public void onStart() {  
  39.   
  40.                 }  
  41.   
  42.                 @Override  
  43.                 public void onStop() {  
  44.   
  45.                 }  
  46.   
  47.                 @Override  
  48.                 public void onDestroy() {  
  49.   
  50.                 }  
  51.             });  


2)通过url获取

[java] view plain copy
  1. Bitmap myBitmap = Glide.with(applicationContext)  
  2.     .load(yourUrl)  
  3.     .asBitmap() //必须  
  4.     .centerCrop()  
  5.     .into(500500)  
  6.     .get()  

2. 获取图片缓存路径

[java] view plain copy
  1. FutureTarget<File> future = Glide.with(mContext)  
  2.                     .load("url")  
  3.                     .downloadOnly(500500);  
  4.             try {  
  5.                 File cacheFile = future.get();  
  6.                 String path = cacheFile.getAbsolutePath();  
  7.             } catch (InterruptedException e) {  
  8.                 e.printStackTrace();  
  9.             } catch (ExecutionException e) {  
  10.                 e.printStackTrace();  
  11.             }  


阅读全文
0 0