美文网首页Android开发Android开发
图片加载库 Glide的使用

图片加载库 Glide的使用

作者: i小灰 | 来源:发表于2020-12-31 01:28 被阅读0次

简介

Glide 是一个快速高效的 Android 图片加载库,注重于平滑的滚动。

开始

  1. 在 app/build.gradle 文件当中添加如下依赖:
dependencies {
    compile 'com.github.bumptech.glide:glide:4.8.0';
}

  1. 在 AndroidManifest.xml 添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />

使用

  1. 基本用法
    多数情况下,使用Glide加载图片非常简单,一行代码足矣:
    Glide.with(this) .load(url) .into(imageView);
    解析一下这行代码:
    • 首先,调用Glide.with()方法用于创建一个加载图片的实例。with()方法可以接收Context、Activity或者Fragment类型的参数。

    • 其次,load()方法用于指定待加载的图片资源。Glide支持加载各种各样的图片资源,包括网络图片、本地图片、应用资源、二进制流、Uri对象等等。

// 加载本地图片
File file = new File(getExternalCacheDir() + "/image.jpg");
Glide.with(this).load(file).into(imageView);

// 加载应用资源
int resource = R.drawable.image;
Glide.with(this).load(resource).into(imageView);

// 加载二进制流
byte[] image = getImageBytes();
Glide.with(this).load(image).into(imageView);

// 加载Uri对象
Uri imageUri = getImageUri();
Glide.with(this).load(imageUri).into(imageView);

  • 最后,into() 方法,我们希望让图片显示在哪个 ImageView 上,把这个 ImageView 的实例传进去就可以了。

占位符

  1. 占位符(Placeholder)
    占位符是当请求正在执行时被展示的 Drawable 。当请求成功完成时,占位符会被请求到的资源替换。如果被请求的资源是从内存中加载出来的,那么占位符可能根本不会被显示。如果请求失败并且没有设置 error Drawable ,则占位符将被持续展示。
Glide.with(this)
  .load(url)
  .placeholder(R.drawable.placeholder)
  .into(view);
//或
Glide.with(this)
  .load(url)
  .placeholder(new ColorDrawable(Color.BLACK))
  .into(view);

  1. 错误符(Error)
    error Drawable 在请求永久性失败时展示。error Drawable 同样也在请求的url/model为 null ,且并没有设置 fallback Drawable 时展示。
Glide.with(this)
  .load(url)
  .error(R.drawable.error)
  .into(view);
//或
Glide.with(this)
  .load(url)
  .error(new ColorDrawable(Color.RED))
  .into(view);

  1. 后备回调符(Fallback)
    fallback Drawable 在请求的url/model为 null 时展示。
Glide.with(this)
  .load(url)
  .fallback(R.drawable.fallback)
  .into(view);
//Or:
Glide.with(this)
  .load(url)
  .fallback(new ColorDrawable(Color.GREY))
  .into(view);

源码分析

参考:从源码的角度理解Glide的执行流程

Glide设置图片4个角为圆角

在软件开发过程中,经常遇到设置圆角的过程,有时候设置四个,有时候两个,我们就可以使用以下方法进行设置。
效果图:

image

注意:可以看到图片的上面两个角是圆角

二、代码

1. 依赖

// glide的使用
    implementation 'com.github.bumptech.glide:glide:4.6.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'

2. xml布局:

        <ImageView
            android:id="@+id/iv_prc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

3. 工具类:

public class CornerTransform implements Transformation<Bitmap> {
    private BitmapPool mBitmapPool;

    private float radius;

    private boolean exceptLeftTop, exceptRightTop, exceptLeftBottom, exceptRightBotoom;

    /**
     * 除了那几个角不需要圆角的
     *
     * @param leftTop
     * @param rightTop
     * @param leftBottom
     * @param rightBottom
     */
    public void setExceptCorner(boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
        this.exceptLeftTop = leftTop;
        this.exceptRightTop = rightTop;
        this.exceptLeftBottom = leftBottom;
        this.exceptRightBotoom = rightBottom;
    }

    public CornerTransform(Context context, float radius) {
        this.mBitmapPool = Glide.get(context).getBitmapPool();
        this.radius = radius;
    }

    @Override
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
        Bitmap source = resource.get();
        int finalWidth, finalHeight;
        float ratio; //输出目标的宽高或高宽比例
        if (outWidth > outHeight) { //输出宽度>输出高度,求高宽比
            ratio = (float) outHeight / (float) outWidth;
            finalWidth = source.getWidth();
            finalHeight = (int) ((float) source.getWidth() * ratio); //固定原图宽度,求最终高度
            if (finalHeight > source.getHeight()) { //求出的最终高度>原图高度,求宽高比
                ratio = (float) outWidth / (float) outHeight;
                finalHeight = source.getHeight();
                finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
            }
        } else if (outWidth < outHeight) { //输出宽度 < 输出高度,求宽高比
            ratio = (float) outWidth / (float) outHeight;
            finalHeight = source.getHeight();
            finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
            if (finalWidth > source.getWidth()) { //求出的最终宽度 > 原图宽度,求高宽比
                ratio = (float) outHeight / (float) outWidth;
                finalWidth = source.getWidth();
                finalHeight = (int) ((float) source.getWidth() * ratio);
            }
        } else { //输出宽度=输出高度
            finalHeight = source.getHeight();
            finalWidth = finalHeight;
        }

        //修正圆角
        this.radius *= (float) finalHeight / (float) outHeight;
        Bitmap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
        if (outBitmap == null) {
            outBitmap = Bitmap.createBitmap(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(outBitmap);
        Paint paint = new Paint();
        //关联画笔绘制的原图bitmap
        BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        //计算中心位置,进行偏移
        int width = (source.getWidth() - finalWidth) / 2;
        int height = (source.getHeight() - finalHeight) / 2;
        if (width != 0 || height != 0) {
            Matrix matrix = new Matrix();
            matrix.setTranslate((float) (-width), (float) (-height));
            shader.setLocalMatrix(matrix);
        }

        paint.setShader(shader);
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
        canvas.drawRoundRect(rectF, this.radius, this.radius, paint); //先绘制圆角矩形

        if (exceptLeftTop) { //左上角不为圆角
            canvas.drawRect(0, 0, radius, radius, paint);
        }
        if (exceptRightTop) {//右上角不为圆角
            canvas.drawRect(canvas.getWidth() - radius, 0, radius, radius, paint);
        }

        if (exceptLeftBottom) {//左下角不为圆角
            canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
        }

        if (exceptRightBotoom) {//右下角不为圆角
            canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
        }

        return BitmapResource.obtain(outBitmap, this.mBitmapPool);
    }

    @Override
    public String getId() {
        return this.getClass().getName();
    }
}

在activity或Fragment或适配器中调用:
CornerTransform transformation = new CornerTransform(context, dip2px(context, 10));
//只是绘制左上角和右上角圆角
transformation.setExceptCorner(false, false, false, false);

Glide.with(context)
        .load(HttpContants.URL + list.get(position).getImage())
        .asBitmap()
        .skipMemoryCache(true)
        .placeholder(R.drawable.background4)
        .error(R.drawable.background4)
        .transform(transformation)
        .into(holder.ivImgShop);

还有一个方法:

public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

特别注意:设置4个角为圆角的方法

image

————————————————
参考链接:https://blog.csdn.net/yijiaodingqiankun/article/details/80927281

其他框架

Glide

Glide是一款由 Bump Technologies 开发的图片加载框架,使得我们可以在 Android 平台上以极度简单的方式加载和展示图片。

参考文章:
Glide 最全解析
Glide 和 Picasso 的区别
深入理解Glide源码

Fresco

Fresco 是 Facebook 提供的开源图片加载库,它能够从网络,本地存储和 Android 资源文件中加载图片,且具有三级缓存设计(2级内存,1级文件)。Fresco 中实现了各种加载过程以及加载后的图片绘制,整体都很强大。
参考:
Fresco 的中文官网

Picasso

Picasso 是 Square 公司开源的 Android 端的图片加载和缓存框架。

参考文章:
Picasso最详细的使用指南

相关文章

  • Glide

    ImageView的scaleType设置不当,导致使用Glide时出现OOM Glide图片加载库的使用

  • 开源框架 | Glide 使用流程源码解析

    Glide 强大的图片加载库 1. 基本使用 RequestOptions :请求选项placeHolder:加载...

  • Glide的使用

    Glide的使用 Glide是google开发用于Android加载媒体的类库,包括图片,gif,video,已经...

  • Glide 加载图片保存至本地,加载回调监听

    Glide 加载图片使用到的两个记录 Glide 加载图片保存至本地指定路径 Glide 加载图片回调方法

  • Glide使用及加载圆形图片和圆角图片

    本文以glide 3.7版本为例 Glide使用 Glide可以说是安卓开发者最常使用的图片加载库。它支持本地图片...

  • Glide

    Glide常用: ////原生加载,图片过大,OOM异常 使用Glide加载 Glide 1.简介 Glide,一...

  • glide记录

    Glide是一款比较强大的android图片加载库,加载图片一行代码就能搞定。使用glide以及有一段时间了,最近...

  • Glide基本使用

    参考 Android图片加载框架最全解析(一),Glide的基本用法Google推荐的图片加载库Glide介绍 思...

  • Android使用giflib库高效加载gif图片总结

    最近公司项目需要加载大量gif图片,我们项目用的图片加载库是glide,众所周知glide自带加载gif功能,但是...

  • Glide 图片加载库详细用法

    Gilde 定义 Gilde 是google开源的图片加载库。 glide依赖 glide 基础用法 glide ...

网友评论

    本文标题:图片加载库 Glide的使用

    本文链接:https://www.haomeiwen.com/subject/esyoiktx.html