源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-07-02 03:47 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android中Glide库的使用小技巧总结
[b]简介[/b] 在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech。这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会上发布的官方app。 [url=https://github.com/bumptech/glide]https://github.com/bumptech/glide[/url] [b]简单使用  [/b]
dependencies { 
 compile 'com.github.bumptech.glide:glide:3.7.0'
} 
如何查看最新版本 [url=http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22glide%22]http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22glide%22[/url]  详细的Glide库配置、使用方法及简介看这里:[url=http://www.1sucai.cn/article/83156.htm]http://www.1sucai.cn/article/83156.htm[/url] [b]引言[/b] 所以大家都知道,在Android项目中,图片加载是必备的功课。经历过多个第三方图片加载库后,用到了Glide。感觉挺好用,记录下使用中总结的小技巧。 [list] [*]AS导入Glide库[/*] [*]Glide方法介绍[/*] [/list] [b]AS导入Glide库[/b]
dependencies { 
compile ‘com.github.bumptech.glide:glide:3.5.2' 
compile ‘com.android.support:support-v4:22.0.0' 
}
[b]Glide使用[/b] 在需要加载图片的地方,直接调用方法。在[code]with()[/code]方法中,参数可以是activity,fragment以及context,以activity和fragment作为参数的好处在于,可以根据activity和fragment的生命周期来加载图片。 基础使用:
Glide.with(activity).load(url).into(view);
[b]需要注意: [/b] 不要在非主线程里面使用Glide加载图片。如果非要使用Glide在非主线程中加载图片,那么请将context改成getApplicationContext [b]Glide扩展属性介绍[/b] [b]1、override(int width, int height) [/b] 使用此方法,自定义图片大小 [b]2、fitCenter()/centerCrop()/fitStart()/fitEnd() [/b] 设置imageview的setScaleType,控制Glide在加载图片的时候,能根据imageview的尺寸或者[code]overide()[/code]的尺寸加载图片。减少加载图片OOM出现的可能性。 [b]3、图片缓存[/b] Glide的图片缓存策略是根据imageview尺寸进行相应处理,缓存与imageview尺寸相同的图片。 [b]使用方法: [/b]
.diskCacheStrategy(DiskCacheStrategy.RESULT) 
查看源码可得 [list] [*]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得到的图片各个尺寸都缓存一遍。[/*] [/list] 很明显可知,在使用过程中,一般会考虑[code]DiskCacheStrategy.ALL[/code]与[code]DiskCacheStrategy.RESULT[/code]。其中使用ALL,会占用较多的内存,但是同一张图片,在不同地方显示不同尺寸,是一次网络请求而来;而使用RESULT,则会相对少的占用内存,但是一张图片在不同地方显示不同尺寸,会根据尺寸不同多次请求网络。 [b]4、占位图,错误图展示 [/b] [code]placeholder()[/code] ,默认占位图 [code]error()[/code] ,默认加载错误显示的图片 [b]5、使用Glide加载自定义imageview中图片 [/b] 使用Glide加载自定义view的时候,可能会出现如下情况: Glide填写了占位图,查看自定义View,自定义View第一次不会显示URL加载的图片,而是显示占位图。需要取消再次查看自定义View,才会显示正确。 出现原因:Glide加载自定义View的时候,需要使用Glide库中的Transformations方法转换自定义imageview或者在into()方法中使用 [code]new simpleTarget()[/code]方法来处理图片。 [b]解决方法: [/b] [b]a、使用Transformations方法转换 [/b]
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]b、使用new simpleTarget() [/b]
Glide.with(activity).load(url).into(new SimpleTarget() { 
@Override 
public void onResourceReady(GlideDrawable resource, GlideAnimation
[b]如何修改Glide Bimmap格式[/b] 默认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 [b]Glide设置图片Tag[/b] 在使用过程中,想要给imageview设置tag,然后使用Glide加载,但是总会报错~如何为ImageView设置Tag呢? [b]方案一:使用setTag(int,object)方法设置tag,具体用法如下:[/b]
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,添加 [b]方案二:从Glide的3.6.0之后,新添加了全局设置的方法。具体方法如下: [/b] 先实现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文件里面添加 [b]一些实用技巧[/b] 1.[code]Glide.with(context).resumeRequests()[/code]和 [code]Glide.with(context).pauseRequests()[/code] 当列表在滑动的时候,调用[code]pauseRequests()[/code]取消请求,滑动停止时,调用[code]resumeRequests()[/code]恢复请求。这样是不是会好些呢? 2.[code]Glide.clear()[/code] 当你想清除掉所有的图片加载请求时,这个方法可以帮助到你。 3.ListPreloader 如果你想让列表预加载的话,不妨试一下ListPreloader这个类。 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。 参考链接 [list] [*]http://www.wtoutiao.com/p/y3eaF0.html[/*] [*]http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html[/*] [/list]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部