项目地址:html
https://github.com/bumptech/glideandroid
Glide做为安卓开发经常使用的图片加载库,有许多实用并且强大的功能,那么,今天就来总结一番,此次把比较常见的都写出来,但并非所有哦。git
在介绍以前,先来讲说什么是Glide吧:github
在泰国举行的谷歌开发者论坛上,谷歌为咱们介绍了一个名叫 Glide 的图片加载库,做者是bumptech。这个库被普遍的运用在google的开源项目中,包括2014年google I/O大会上发布的官方app。web
Glide和Picasso有90%的类似度,准确的说,就是Picasso的克隆版本。可是在细节上仍是有很多区别的。算法
Glide的使用很是方便,并且使用了链式编程,因此方法直接链接写在后便就能够了。如下是我日常总结的比较经常使用的Glide方法编程
1:Glide的配置canvas
compile 'com.github.bumptech.glide:glide:3.7.0'api
2最简单的用法缓存
异步加载图片:
Glide.with(context).load(url).into(imageView);
这是最简单的用法了,with里填上下文,load里面填写url,into填写要下载到的控件上便可
3设置占位图和错误图:
placeholder(R.drawable.user_placeholder)
error(R.drawable.user_placeholder_error)
4 load方法的使用
Glide基本能够load任何能够拿到的媒体资源,如:
load SD卡资源:load("file://"+Environment.getExternalStorageDirectory().getPath()+"/test.jpg")
load assets资源:load("file:///android_asset/f003.gif")
load raw资源:load("Android.resource://包名/raw/raw_1")
或load("android.resource://包名/raw/"+R.raw.raw_1)
load drawable资源:load("android.resource://包名/drawable/"+R.drawable.news)
load ContentProvider资源:load("content://media/external/images/media/139469")
load http资源:load("http://img.my.csdn.NET/uploads/201508/05/1438760757_3588.jpg")
load https资源:load("https://img.alicdn.com/tps/TB1uyhoMpXXXXcLXVXXXXXXXXXX-476-538.jpg_240x5000q50.jpg_.webp")
固然,load不限于String类型,还能够:
load(Uri uri),
load(File file),
load(Integer resourceId),
load(URL url),
load(byte[] model),
load(T model),
loadFromMediaStore(Uri uri)。
5设置缓存策略
diskCacheStrategy(DiskCacheStrategy.ALL)
可选策略:
DiskCacheStrategy.SOURCE:缓存原始数据;
DiskCacheStrategy.RESULT:缓存变换(如缩放、裁剪等)后的资源数据;
DiskCacheStrategy.NONE:什么都不缓存(不进行磁盘缓存);
DiskCacheStrategy.ALL:缓存SOURC和RESULT;
默认采用DiskCacheStrategy.RESULT策略;
对于download only操做要使用DiskCacheStrategy.SOURCE;
6清空缓存
禁止内存缓存:
.skipMemoryCache(true)
清除内存缓存:
// 必须在UI线程中调用
Glide.get(context).clearMemory();
禁止磁盘缓存:
.diskCacheStrategy(DiskCacheStrategy.NONE)
清除磁盘缓存:
// 必须在后台线程中调用,建议同时clearMemory()
Glide.get(applicationContext).clearDiskCache();
7设置请求优先级
priority(Priority.HIGH)
优先级越高越优先加载,但不保证全部图片都按序加载。可选参数:
枚举Priority.IMMEDIATE,Priority.HIGH,Priority.NORMAL,Priority.LOW。默认为Priority.NORMAL。
8 设置缩略图
thumbnail()
9 直接得到Bitmap
有些时候咱们并不但愿把图片放入ImageView中。咱们只要 Bitmap 自己。Glide 提供了一个用 Targets 的简单的方式去接受图片资源的 Bitmap。Targets 是回调函数,它在 Glide 作完全部的加载和处理以后返回结果。
方法1
Glide.with(this).load(url).asBitmap().into(newSimpleTarget<Bitmap>() {
@Override
public voidonResourceReady(Bitmap resource, GlideAnimation<? super Bitmap>glideAnimation) {
//resource便是得到的Bitmap
}
});
方法2
Bitmap bitmap =Glide.with(MainActivity.this).load(url).asBitmap().into(500, 500).get();
该方式只能在子线程中得到
10加载图片到Notification中
NotificationTarget notificationTarget =
new NotificationTarget(
Context,
RemoteViews,
viewid,
notificationObject,
notifyId);
Glide.with(this).load(url).asBitmap().into(notificationTarget);
11 Glide自带的一个渐变更画
Glide.with(this).load(url).crossFade([duration]).into(iv2);
12加载Gif
//普通显示GIF
Glide.with( context ).load( gifUrl ).into( iv );
//添加GIF检查,若是不是GIF就会显示加载失败位图
Glide.with( context ).load( gifUrl ).asGif().into( iv);
13显示本地视频
String filePath ="/storage/emulated/0/Pictures/example_video.mp4";
Glide
.with(context )
.load(Uri.fromFile( new File( filePath ) ) )
.into( iv );
Glid只能加载本地视频,不能从网络中获取
14显示圆形图片
create(context.getResources(), resource); circularBitmapDrawable.setCircular(true); cimg.setImageDrawable(circularBitmapDrawable); } });
关于Glide就先介绍到这么多了,通常的经常使用开发应该够用了,谢谢你们!喜欢点个赞吧!
转载:http://www.cnblogs.com/whoislcj/p/5558168.html
前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架。技术自己就要不断的更迭,从最初的本身使用SoftReference实现本身的图片缓存,到后来作电商项目本身的实现方案不能知足项目的需求改用Afinal,因为Afinal再也不维护而选择了师出同门的Xutils,中间也接触过别的开源框架好比Picasso,对Picasso的第一次印象就不太好,初次接触是拿到了公司刚从外包公司接手过来的图片社交类app,对内存占用太大,直接感觉就是致使ListView滑动有那么一点卡顿,老牌的图片缓存框架universalImageLoader据说过一直没有真正使用过,以前项目都很小,差很少几百万级别的app,一直使用的都是Xutils,最近以为项目大起来了,万一Xutils不维护了或者说要求支持的图片格式多起来的时候,可能Xutils就不是最佳选择了,这也是来学习Gilde的根本动机吧。其实原本想着去学习Facebook的Fresco图片框架,可是简单的看了一下,须要连同自定义控件一块儿使用,功能虽然强大,可是对于已经在维护的项目修改为本那可不是通常的高,之后有兴趣在学习吧!
图片缓存相关博客地址:
Glide 是 Google 员工的开源项目, Google I/O 上被推荐使用,一个高效、开源、Android设备上的媒体管理框架,它遵循BSD、MIT以及Apache 2.0协议发布。Glide具备获取、解码和展现视频剧照、图片、动画等功能,它还有灵活的API,这些API使开发者可以将Glide应用在几乎任何网络协议栈里。建立Glide的主要目的有两个,一个是实现平滑的图片列表滚动效果,另外一个是支持远程图片的获取、大小调整和展现。
gitHub地址:https://github.com/bumptech/glide
compile 'com.github.bumptech.glide:glide:3.7.0'
咱们能够更加高效的使用Glide提供的方式进行绑定,这样能够更好的让加载图片的请求的生命周期动态管理起来
Glide.with(Context context);// 绑定Context Glide.with(Activity activity);// 绑定Activity Glide.with(FragmentActivity activity);// 绑定FragmentActivity Glide.with(Fragment fragment);// 绑定Fragment
Glide.with(this).load(imageUrl).into(imageView);
api里面对placeholder()、error()函数中有多态实现 用的时候能够具体的熟悉一下
Glide.with(this).load(imageUrl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
Glide.with(this).load(imageUrl).skipMemoryCache(true).into(imageView);
Glide.with(this).load(imageUrl).priority(Priority.NORMAL).into(imageView);
Glide.with(this).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
策略解说:
all:缓存源资源和转换后的资源
none:不做任何磁盘缓存
source:缓存源资源
result:缓存转换后的资源
api也提供了几个经常使用的动画:好比crossFade()
Glide.with(this).load(imageUrl).animate(R.anim.item_alpha_in).into(imageView);
R.anim.item_alpha_in
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0"/> </set>
这样会先加载缩略图 而后在加载全图
Glide.with(this).load(imageUrl).thumbnail(0.1f).into(imageView);
Glide.with(this).load(imageUrl).override(800, 800).into(imageView);
Glide.with(this).load(imageUrl).centerCrop().into(imageView);
api提供了好比:centerCrop()、fitCenter()等函数也能够经过自定义Transformation,举例说明:好比一我的圆角转化器
public class GlideRoundTransform extends BitmapTransformation { private float radius = 0f; public GlideRoundTransform(Context context) { this(context, 4); } public GlideRoundTransform(Context context, int dp) { super(context); this.radius = Resources.getSystem().getDisplayMetrics().density * dp; } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return roundCrop(pool, toTransform); } private Bitmap roundCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rectF, radius, radius, paint); return result; } @Override public String getId() { return getClass().getName() + Math.round(radius); } }
具体使用
Glide.with(this).load(imageUrl).transform(new GlideRoundTransform(this)).into(imageView);
项目中有不少须要先下载图片而后再作一些合成的功能,好比项目中出现的图文混排,该如何实现目标下
Glide.with(this).load(imageUrl).centerCrop().into(new SimpleTarget<GlideDrawable>() { @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { imageView.setImageDrawable(resource); } });
Glide.with(this).load(imageUrl).listener(new RequestListener<String, GlideDrawable>() { @Override public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { //imageView.setImageDrawable(resource); return false; } }).into(imageView);
设置监听的用处 能够用于监控请求发生错误来源,以及图片来源 是内存仍是磁盘
Glide.with(this).load(imageUrl).asBitmap().into(imageView);//显示gif静态图片 Glide.with(this).load(imageUrl).asGif().into(imageView);//显示gif动态图片
Glide.get(this).clearDiskCache();//清理磁盘缓存 须要在子线程中执行 Glide.get(this).clearMemory();//清理内存缓存 能够在UI主线程中进行