Android 开源框架 ( 十一 ) 图片加载框架---Glide

一.引言

 Glide被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app

 前面介绍了
  
  而本文介绍的Glide是在Picasso基础上进行的二次开发,和Picasso 有90%相似度,其优势显而易见。Universal ImageLoader已停止服务。
 

二.基本使用

  1.添加最新依赖  

compile 'com.github.bumptech.glide:glide:4.7.1'

注意:Glide默认会导入Android的support-v4包。4.71版本默认导入的是v4包的27版本。如果你的项目中有v4包的别的版本,就会引起冲突发生错误如 java.lang.NoSuchMethodError: No static method。

  2.基本使用

Glide.with(mContext)
    .load(mDatas[position])
    .placeholder(R.mipmap.ic_launcher) //占位图
    .error(R.mipmap.ic_launcher)  //出错的占位图
    .override(width, height) //图片显示的分辨率 ,像素值 可以转化为DP再设置
    .animate(R.anim.glide_anim)
    .centerCrop()
    .fitCenter()
    .into(holder.image);

三.拓展了解Glide加载不同的图片

  Glide不仅仅可以加载网络图片,同样也能加载资源图片,本地图片,GIf, 视频快照,缩略图等。

  //(1)加载网络图片
    Glide.with(this).load("http://img1.imgtn.bdimg.com/it/u=2615772929,948758168&fm=21&gp=0.jpg").into(ivGlide1);

    //(2)加载资源图片
    Glide.with(this).load(R.drawable.atguigu_logo).into(ivGlide2);

    //(3)加载本地图片
    String path = Environment.getExternalStorageDirectory() + "/meinv1.jpg";
    File file = new File(path);
    Uri uri = Uri.fromFile(file);
    Glide.with(this).load(uri).into(ivGlide3);

    // (4)加载网络gif
    String gifUrl = "http://b.hiphotos.baidu.com/zhidao/pic/item/faedab64034f78f066abccc57b310a55b3191c67.jpg";
    Glide.with(this).load(gifUrl).placeholder(R.mipmap.ic_launcher).into(ivGlide4);

    // (5)加载资源gif
    Glide.with(this).load(R.drawable.loading).asGif().placeholder(R.mipmap.ic_launcher).into(ivGlide5);

    //(6)加载本地gif
    String gifPath = Environment.getExternalStorageDirectory() + "/meinv2.jpg";
    File gifFile = new File(gifPath);
    Glide.with(this).load(gifFile).placeholder(R.mipmap.ic_launcher).into(ivGlide6);

    //(7)加载本地小视频和快照
    String videoPath = Environment.getExternalStorageDirectory() + "/video.mp4";
    File videoFile = new File(videoPath);
    Glide.with(this).load(Uri.fromFile(videoFile)).placeholder(R.mipmap.ic_launcher).into(ivGlide7);

    //(8)设置缩略图比例,然后,先加载缩略图,再加载原图
    String urlPath = Environment.getExternalStorageDirectory() + "/meinv1.jpg";
    Glide.with(this).load(new File(urlPath)).thumbnail(0.1f).centerCrop().placeholder(R.mipmap.ic_launcher).into(ivGlide8);

    //(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(ivGlide9);

猜你喜欢

转载自www.cnblogs.com/bugzone/p/Glide.html