如何在Glide 4上使用GlideModule?

时间:2017-10-09 02:28:49

标签: android android-glide

我最近更新了我的应用程序以使用Glide 4,准确地说,Glide 4.2.0。 的gradle:

compile 'com.github.bumptech.glide:glide:4.2.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
compile ('com.github.bumptech.glide:okhttp3-integration:4.2.0'){
    exclude group: 'glide-parent'
}
清单中的

<meta-data
            android:name="com.xxx.MyGlideModule"
            android:value="GlideModule"/>

GlideModule类:

@GlideModule
public class MyGlideModule extends AppGlideModule {

    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .build();

        OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);

        glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
    }
}

我如何在适配器中使用滑动:

        RequestOptions myOptions = new RequestOptions()
                .placeholder(R.drawable.ic_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .dontAnimate()
                .skipMemoryCache(true)
                ;

        Glide.with(mContext)
                .load(Imageid[position])
                .apply(myOptions)
                .into(imageView);

使用这些代码,当我运行它时出现错误:

java.lang.RuntimeException: Expected instanceof GlideModule, but found: [my app package].MyGlideModule@d1c2328
  at com.bumptech.glide.module.ManifestParser.parseModule(ManifestParser.java:81)
  at com.bumptech.glide.module.ManifestParser.parse(ManifestParser.java:43)
  at com.bumptech.glide.Glide.initializeGlide(Glide.java:193)
  at com.bumptech.glide.Glide.checkAndInitializeGlide(Glide.java:172)
  at com.bumptech.glide.Glide.get(Glide.java:156)
  at com.bumptech.glide.Glide.getRetriever(Glide.java:540)
  at com.bumptech.glide.Glide.with(Glide.java:566)
  at [adapter line where I implement Glide]

我该如何使用MyGlideModule?

3 个答案:

答案 0 :(得分:26)

Glide 4.0无需 AndroidManifest.xml 中声明“GlideModule”。您只需要执行以下步骤:

  1. YourGlideModule 扩展 AppGlideModule ,您可以覆盖 YourGlideModule 类中的函数applyOptions
  2. 您应该在“android studio - &gt; build - &gt; make project”中创建项目,它将生成GlideApp类。
  3. 使用GlideApp,例如GlideApp.with(this).load(imgUrl).into(glide_test_iv1)

答案 1 :(得分:0)

如果无法生成GlideApp,则-

确保您具有build.gradle的{​​{1}}依赖项

确保您的annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'扩展了UnsafeOkHttpGlideModule,并且在AppGlideModule中,meta-data应该是android:value=""

答案 2 :(得分:0)

添加@kevin 和@brady lee 的答案。 对于 Kotlin 用户

resources:
  repositories:
  - repository: MyGitHubRepo # The name used to reference this repository in the checkout step
    type: github
    endpoint: serviceconnectionname
    name: githuborg/reponame   #e.g. yy/test
    ref: main

pool:
  vmImage: ubuntu-latest

stages:
- stage: Build
  displayName: 'Build'
  jobs:  
  - template: Azure-Pipelines/build.yml@MyGitHubRepo