35. 图片加载框架Glide

35. 图片加载框架Glide

35.1 Glide的引入

官方地址:https://github.com/bumptech/glide

在这里插入图片描述

简体中文文档地址:https://muyangmin.github.io/glide-docs-cn/

在这里插入图片描述

引入

implementation 'com.github.bumptech.glide:glide:4.13.2'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2'

在这里插入图片描述

同步

35.2 简单实用

布局文件中定义一个ImageView

在这里插入图片描述

java代码实现

//定位组件
ImageView imageView = findViewById(R.id.iv);

Glide.with(this)
        .load("https://profile.csdnimg.cn/8/9/D/1_weixin_44226181")
        .into(imageView);

在这里插入图片描述

由于是加载网络图片,所以要在清单文件中,配置网络权限。

<uses-permission android:name="android.permission.INTERNET"/>

在这里插入图片描述

运行

在这里插入图片描述

OK。

35.3 Glide占位符
placeholder

正在请求图片时展示的图片

error

请求失败时展示的图片(如果没有设置,还是展示placeholder的占位符)

fallback

如果请求的url/model为null时展示的图片(如果没有设置,还是展示placeholder的占位符)

创建请求配置

RequestOptions requestOptions = new RequestOptions()
        .placeholder(R.drawable.ic_baseline_cast_for_education_24)
        .error(R.drawable.ic_baseline_error_24)
        .fallback(R.drawable.ic_baseline_electric_bike_24)
        .override(100, 100);

在这里插入图片描述

35.4 Glide过渡动画

定义Glide如何从占位符到新加载的图片,或者从缩略图到全尺寸图像的过渡。

交叉淡入效果:

在这里插入图片描述

在这里插入图片描述

35.5 Glide变换

获取资源并修改它,然后返回被修改后的资源,通常变换操作是用来完成剪裁或对位图应用过滤器。

  1. CircleCrop:圆角
  2. RoundedCorners:四个角度统一指定
  3. GranularRoundedCorners:四个角度单独指定
  4. Rotate:旋转

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

35.6 Generated API

添加依赖

在这里插入图片描述

就是之前这个

在Application模块中包含一个AppGlideModule的实现:

package com.dingjiaxiong.myglide;

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public class MyAppModule extends AppGlideModule {
}

在这里插入图片描述

接下来以配置占位符为例,如下

在这里插入图片描述

GlideExtension与GlideOption

→ 可以定义一个频繁使用的选项集合。

定义一个配置类

在这里插入图片描述

package com.dingjiaxiong.myglide;

import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.BaseRequestOptions;

@GlideExtension
public class MyAppExtension {
    private MyAppExtension(){} // 必须是私有的、无参的构造方法

    @GlideOption
    public static BaseRequestOptions<?> defaultImg(BaseRequestOptions<?> options){
        return options
                .placeholder(R.drawable.ic_baseline_electric_bike_24)
                .error(R.drawable.ic_baseline_error_24)
                .fallback(R.drawable.ic_baseline_cast_for_education_24);
    }
}

使用对比

在这里插入图片描述

posted @ 2022-09-19 08:38  随遇而安==  阅读(53)  评论(0编辑  收藏  举报