对Glide的初使用

最近在做一个与图片相关的项目,前阵子学了Glide的基本用法,于是就把Glide运用到了本项目中。Glide有很多的优势:使用简单,一行代码就可以搞定图片的加载
          可配置度高,自适应的程度高
        支持多种图片的格式:jpg、png、gif、webp
          支持多种数据源:网络、本地、资源、assets
          缓存策略高效,支持Memory缓存和Disk缓存,默认的Bitmap格式采用RGB_565,内存使用至少减少一半
          生命周期根据Activity/Fragment自动管理请求
         高效处理Bitmap,使用Bitmap的线程池是Bitmap复用,主动调用recycle回收需要回收的Bitmap,减少系统的压力。

下面介绍Glide的基本用法:

首先是添加依赖:compile 'com.github.bumptech.glide:glide:3.7.0'

(1)加载网络图片:

 Glide.with(this).load("网络图片的url").into(ImageVIew);
(2)加载资源图片:

Glide.with(this).load(资源图片).into(ImageVIew);
(3)加载本地图片

String path= Environment.getExternalStorageDirectory()+"/图片名字";
        File file=new File(path);
        Uri uri=Uri.fromFile(file);
        Glide.with(this).load(uri).into(ImageView);
(4)加载网络Gif:占位图:placeholder(R.mipmap.ic_launcher)

 Glide.with(this).load(网络gif图片url).placeholder(R.mipmap.ic_launcher).into(ImageView);
(5)加载资源gif:

Glide.with(this).load(资源gif图片).asGif().placeholder(R.mipmap.ic_launcher).into(ImageView);
(6)加载本地gif

	File gifFile=new File(gifPath);
        Glide.with(this).load(gifFile).placeholder(R.mipmap.ic_launcher).into(ImageView);
(7)加载本地小视屏、快照首页

 	File videoFile=new File(videoPath);
        Glide.with(this).load(Uri.fromFile(videoFile)).placeholder(R.mipmap.ic_launcher).into(iv7);
(8)设置缩略图比例,然后先加载缩略图,再加载原图:此例子缩略图设为原图的0.1;

	String urlPath=Environment.getExternalStorageDirectory()+"图片的名称";
        Glide.with(this).load(new File(urlPath)).thumbnail(0.1f).centerCrop()
                .placeholder(R.mipmap.ic_launcher).into(ImageView);
(9)先建立缩略图对象,然后加载缩略图、再加载原图:

 	DrawableRequestBuilder thumbnailRequest=Glide.with(this).load(new File(urlPath));
        Glide.with(this).load(Uri.fromFile(videoFile)).thumbnail(thumbnailRequest).centerCrop()
                .placeholder(R.mipmap.ic_launcher).into(ImageView);
(10)设置动画效果

.animate(动画对象)    //动画效果
(11)设置错误时显示的图片

.error(R.mipmap.ic_launcher)

(12)对图片进行裁剪,模糊,滤镜等处理:

添加依赖:compile 'jp.wasabeef:glide-transformations:2.0.1'    

         compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.0'

此时可以对图片进行一些特效处理比如要将图片变成圆形显示:

			Glide.with(mContext)
                        .load(R.mipmap.dogs)
                        .bitmapTransform(new CropCircleTransformation(mContext))
                        .into(holder.mImageView);
关于Glide的基本使用就介绍这些啦,往后还要向大神们学习读它的源码













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值