美文网首页Android
安卓Glide(4.7.1)使用笔记 01 - 引入项目

安卓Glide(4.7.1)使用笔记 01 - 引入项目

作者: Joe_zShare | 来源:发表于2018-04-20 14:57 被阅读1210次

    Glide,一个被google所推荐的图片加载库,作者是bumptech。这个库被广泛运用在google的开源项目中。经过多年的迭代,Glide已经成为了安卓开发者最喜爱的图片加载库之一。新版本的使用方式和以前的3.x.x在使用存在区别,以下是演示最新版本的Glide的使用,Glide新的版本也做了较多的优化和更多功能的实现。

    以下是官方简介,专注于顺畅滑动的图片加载库
    官方地址 :https://github.com/bumptech/glide

    An image loading and caching library for Android focused on smooth scrolling

    1、配置到Android项目

    Gradle方式
    repositories {
      mavenCentral()
      google()
    }
    
    dependencies {
      implementation 'com.github.bumptech.glide:glide:4.7.1'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
    }
    
    下载Jar包方式

    地址:https://github.com/bumptech/glide/releases

    添加混肴
    -keep public class * implements com.bumptech.glide.module.GlideModule
    -keep public class * extends com.bumptech.glide.module.AppGlideModule
    -keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
      **[] $VALUES;
      public *;
    }
    -dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder
    # for DexGuard only
    -keepresourcexmlelements manifest/application/meta-data@value=GlideModule
    
    添加权限
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    

    ACCESS_NETWORK_STATE 这个权限不是必须的

    如果你正在从 URL 加载图片,Glide 可以自动帮助你处理片状网络连接:它可以监听用户的连接状态并在用户重新连接到网络时重启之前失败的请求。如果 Glide 检测到你的应用拥有 ACCESS_NETWORK_STATE 权限,Glide 将自动监听连接状态而不需要额外的改动。

    要从本地文件夹或 DCIM 或图库中加载图片,你将需要添加 READ_EXTERNAL_STORAGE 权限

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

    而如果要使用 ExternalPreferredCacheDiskCacheFactory 来将 Glide 的缓存存储到公有 SD 卡上,你还需要添加 WRITE_EXTERNAL_STORAGE 权限:

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

    2、一句代码简单使用

    Glide.with(this)
         .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524215585000&di=bfce834d92cbdd3ded74d695cf5c8638&imgtype=0&src=http%3A%2F%2Fimage5.tuku.cn%2Fpic%2Fwallpaper%2Fmeinv%2Frenbihuajiaominv%2F010.jpg")
         .into(ivImage);
    

    在用以上代码加载到图片之前,我遇到了一个问题,加载图片的时候报错了。
    错误信息

    java.lang.NoSuchMethodError: No static method getFont
    

    一看错误日志就知道是版本冲突了,最后去官网看了下,Glide最新版本是需要

    Minimum Android SDK: Glide v4 requires a minimum API level of 14
    Compile Android SDK: Glide v4 requires you to compile against API 26 or later

    而我使用的版本是

     implementation 'com.android.support:appcompat-v7:26.1.0'
     implementation 'com.android.support:support-v4:26.1.0'
    

    Glide使用的版本是 27.0.2 ,所以导致冲突了,下面是解决方案

    1、升级自己的版本到27.0.2
    2、使用exclue排除冲突 (我使用的方法)
    3、降级Glide到 4.3.1

    我使用的方法

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        //noinspection GradleCompatible
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support:support-v4:26.1.0'
        implementation('com.github.bumptech.glide:glide:4.7.1') {
            exclude group: "com.android.support"
        }
        annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }
    
    

    加载到的效果图如下

    美女

    相关文章

      网友评论

      • Joe_zShare:还有很多使用 最近太忙 没能更多介绍 新版本不仅如此

      本文标题:安卓Glide(4.7.1)使用笔记 01 - 引入项目

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