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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-04-03 我要评论

简介

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

https://github.com/bumptech/glide

简单使用 

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

如何查看最新版本

http://search.maven.org/ 

详细的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
您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们