当前位置: 移动技术网 > IT编程>移动开发>Android > Android中Glide库的使用小技巧总结

Android中Glide库的使用小技巧总结

2019年07月24日  | 移动技术网IT编程  | 我要评论

京v02####,妻货可居,马拉松的长度

简介

在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 glide 的图片加载库,作者是bumptech。这个库被广泛的运用在google的开源项目中,包括2014年google i/o大会上发布的官方app。

简单使用 

dependencies { 
 compile 'com.github.bumptech.glide:glide:3.7.0'
} 

如何查看最新版本

 

详细的glide库配置、使用方法及简介看这里:

引言

所以大家都知道,在android项目中,图片加载是必备的功课。经历过多个第三方图片加载库后,用到了glide。感觉挺好用,记录下使用中总结的小技巧。

  • as导入glide库
  • glide方法介绍

as导入glide库

dependencies { 
compile ‘com.github.bumptech.glide:glide:3.5.2' 
compile ‘com.android.support:support-v4:22.0.0' 
}

glide使用

在需要加载图片的地方,直接调用方法。在with()方法中,参数可以是activity,fragment以及context,以activity和fragment作为参数的好处在于,可以根据activity和fragment的生命周期来加载图片。

基础使用:

glide.with(activity).load(url).into(view);

需要注意:

不要在非主线程里面使用glide加载图片。如果非要使用glide在非主线程中加载图片,那么请将context改成getapplicationcontext

glide扩展属性介绍

1、override(int width, int height)

使用此方法,自定义图片大小

2、fitcenter()/centercrop()/fitstart()/fitend()

设置imageview的setscaletype,控制glide在加载图片的时候,能根据imageview的尺寸或者overide()的尺寸加载图片。减少加载图片oom出现的可能性。

3、图片缓存

glide的图片缓存策略是根据imageview尺寸进行相应处理,缓存与imageview尺寸相同的图片。

使用方法:

.diskcachestrategy(diskcachestrategy.result) 

查看源码可得

  • diskcachestrategy.none caches nothing, as discussed 不缓存图片
  • diskcachestrategy.source caches only the original full-resolution image. in our example above that would be the 1000x1000 pixel one 仅缓存原图片
  • diskcachestrategy.result caches only the final image, after reducing the resolution (and possibly transformations) 缓存根据url加载到imageview后,与imageview相同尺寸的图片
  • diskcachestrategy.all caches all versions of the image (default behavior) 默认的缓存方式,会将url得到的图片各个尺寸都缓存一遍。

很明显可知,在使用过程中,一般会考虑diskcachestrategy.alldiskcachestrategy.result。其中使用all,会占用较多的内存,但是同一张图片,在不同地方显示不同尺寸,是一次网络请求而来;而使用result,则会相对少的占用内存,但是一张图片在不同地方显示不同尺寸,会根据尺寸不同多次请求网络。

4、占位图,错误图展示

placeholder() ,默认占位图

error() ,默认加载错误显示的图片

5、使用glide加载自定义imageview中图片

使用glide加载自定义view的时候,可能会出现如下情况:

glide填写了占位图,查看自定义view,自定义view第一次不会显示url加载的图片,而是显示占位图。需要取消再次查看自定义view,才会显示正确。

出现原因:glide加载自定义view的时候,需要使用glide库中的transformations方法转换自定义imageview或者在into()方法中使用 new simpletarget()方法来处理图片。

解决方法:

a、使用transformations方法转换

public class blurtransformation extends bitmaptransformation {

private renderscript rs;

public blurtransformation(context context) {
 super( context );

 rs = renderscript.create( context );
}

@override
protected bitmap transform(bitmappool pool, bitmap totransform, int outwidth, int outheight) {
 bitmap blurredbitmap = totransform.copy( bitmap.config.argb_8888, true );

 // allocate memory for renderscript to work with
 allocation input = allocation.createfrombitmap(
 rs, 
 blurredbitmap, 
 allocation.mipmapcontrol.mipmap_full, 
 allocation.usage_shared
 );
 allocation output = allocation.createtyped(rs, input.gettype());

 // load up an instance of the specific script that we want to use.
 scriptintrinsicblur script = scriptintrinsicblur.create(rs, element.u8_4(rs));
 script.setinput(input);

 // set the blur radius
 script.setradius(10);

 // start the scriptintrinisicblur
 script.foreach(output);

 // copy the output to the blurred bitmap
 output.copyto(blurredbitmap);

 totransform.recycle();

 return blurredbitmap;
}

@override
public string getid() {
 return "blur";
}

}
glide 
.with( context ) 
.load( eatfoodyimages[0] ) 
.transform( new blurtransformation( context ) ) 
//.bitmaptransform( new blurtransformation( context ) ) // this would work too! 
.into( imageview1 );

b、使用new simpletarget()

glide.with(activity).load(url).into(new simpletarget() { 
@override 
public void onresourceready(glidedrawable resource, glideanimation

如何修改glide bimmap格式

默认bitmap格式:

rgb_565,也可以使用rgb_8888,但是会相对耗内存,而且这两种格式在手机端看起来,效果相差并不大。

如何修改bitmap格式:

public class glideconfiguration implements glidemodule {

@override
public void applyoptions(context context, glidebuilder builder) {
 // apply options to the builder here.
 builder.setdecodeformat(decodeformat.prefer_argb_8888);
}

@override
public void registercomponents(context context, glide glide) {
 // register modelloaders here.
}

} 

同时在androidminifest.xml中,将glidemodul定义为meta-data

glide设置图片tag

在使用过程中,想要给imageview设置tag,然后使用glide加载,但是总会报错~如何为imageview设置tag呢?

方案一:使用settag(int,object)方法设置tag,具体用法如下:

glide.with(context).load(urls.get(i).geturl()).fitcenter().into(imageviewholder.image); 
imageviewholder.image.settag(r.id.image_tag, i); 
imageviewholder.image.setonclicklistener(new view.onclicklistener() { 
@override 
int position = (int) v.gettag(r.id.image_tag); 
toast.maketext(context, urls.get(position).getwho(), toast.length_short).show(); 
} 
}); 

同时在values文件夹下新建ids.xml,添加

方案二:从glide的3.6.0之后,新添加了全局设置的方法。具体方法如下:

先实现glidemoudle接口,全局设置viewtaget的tagid:

public class myglidemoudle implements glidemodule{ 
@override 
public void applyoptions(context context, glidebuilder builder) { 
viewtarget.settagid(r.id.glide_tag_id); 
}

@override
public void registercomponents(context context, glide glide) {

}

} 

同样,也需要在ids.xml下添加id

最后在androidmanifest.xml文件里面添加

一些实用技巧

1.glide.with(context).resumerequests()glide.with(context).pauserequests()

当列表在滑动的时候,调用pauserequests()取消请求,滑动停止时,调用resumerequests()恢复请求。这样是不是会好些呢?

2.glide.clear()

当你想清除掉所有的图片加载请求时,这个方法可以帮助到你。

3.listpreloader

如果你想让列表预加载的话,不妨试一下listpreloader这个类。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

参考链接

  • http://www.wtoutiao.com/p/y3eaf0.html
  • http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网