Glide-4.0.0 Missing placeholder, error, GlideApp and does not resolve its method placeholder, 错误

Glide-4.0.0 Missing placeholder, error, GlideApp and does not resolve its method placeholder,error

我想使用 Glide Android 库下载图像并在 ImageView 中显示。

在之前的版本中我们使用:

Glide.with(mContext).load(imgUrl)
                .thumbnail(0.5f)
                .placeholder(R.drawable.PLACEHOLDER_IMAGE_NAME)
                .error(R.drawable.ERROR_IMAGE_NAME)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);

但是我看过 Glide 文档:

it says use GlideApp.with() instead Glide.with()

我担心的是缺少占位符、错误、GlideApp 和其他选项。

我正在使用

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

我哪里做错了?参考here.

如何使用 GlideApp.with()

API与AppGlideModule在同一个包中生成,默认命名为GlideApp。应用程序可以通过使用 GlideApp.with() 而不是 Glide.with():

开始所有加载来使用 API
GlideApp.with(fragment)
   .load(myUrl)
   .placeholder(placeholder)
   .fitCenter()
   .into(imageView);

尝试使用 RequestOptions:

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_placeholder);
requestOptions.error(R.drawable.ic_error);

Glide.with(context)
     .setDefaultRequestOptions(requestOptions)
     .load(url).into(holder.imageView);

编辑

如果 .setDefaultRequestOptions(requestOptions) 不起作用,请使用 .apply(requestOptions):

Glide.with(MainActivity.this)
            .load(url)
            .apply(requestOptions)
            .into(imageview);
 // or this
 Glide.with(MainActivity.this)
            .load(url)
            .apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
            .into(imageview);

 // or this
 Glide.with(MainActivity.this)
            .load(url)
            .apply(RequestOptions.placeholderOf(R.drawable.booked_circle).error(R.drawable.))
            .into(imageview);

编辑 2 奖金

以下是 Glide-4 的一些其他变化

  • 如何使用
  • 如何使用
  • 如何在 Glide-4
  • 中使用
  • 如何使用作为目标参数

如果你想使用 GlideApp,你必须像屏幕截图一样添加到 dependencies 注释处理器:

然后在您的应用程序中包含一个 AppGlideModule 实现:

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}

不要忘记 @GlideModule 注释。 然后你需要构建项目。而GlideApp会自动生成

如果你使用Glide包依赖,compile 'com.github.bumptech.glide:glide:3.7.0',那么使用应该是使用下面的代码:

GlideApp
    .with(your context)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_image)
    .error(R.drawable.error_image)
    .into(myImageView);

Note: As in the documentation,

Round Pictures: CircleImageView/CircularImageView/RoundedImageView are known to have issues with TransitionDrawable (.crossFade() with .thumbnail() or .placeholder()) and animated GIFs, use a BitmapTransformation (.circleCrop() will be available in v4) or .dontAnimate() to fix the issue.

最新更新的版本compile com.github.bumptech.glide:glide:4.1.1那么使用应该是使用下面的代码:

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.default_avatar)
                    .error(R.drawable.default_avatar)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.HIGH)
                    .dontAnimate()
                    .dontTransform();

Glide.with(this)
     .load(url)
     .apply(options)
     .into(imageView);

查看最新版本glide, bug fixes and features

依赖关系:

compile 'com.github.bumptech.glide:glide:4.1.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1'

添加适当注释的 AppGlideModule 实现:

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

@GlideModule
public final class MyAppGlideModule extends AppGlideModule{}

另外,如果您使用了jack选项,为了避免出现以下类似错误,您需要使用AndroidStudio 3.0.0预览

Error:Execution failed for task ':app:transformJackWithJackForDebug'. com.android.jack.ir.JNodeInternalError: java.lang.Exception: java.lang.AssertionError: No yet implemented

我们也不需要使用 RequestOptions。

生成的 API 添加了一个 GlideApp class,它提供对 RequestBuilder 和 RequestOptions subclass 的访问。 RequestOptions subclass 包含 RequestOptions 中的所有方法和 GlideExtensions 中定义的任何方法。 RequestBuilder subclass 提供对生成的 RequestOptions subclass 中所有方法的访问,而无需使用 apply:

使用 Glide :-

没有生成的 API 的请求可能如下所示:

Glide.with(fragment)
    .load(url)
    .apply(centerCropTransform()
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.error)
        .priority(Priority.HIGH))
    .into(imageView);

使用 GlideApp :-

使用生成的 API,可以内联 RequestOptions 调用:

GlideApp.with(fragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .priority(Priority.HIGH)
    .into(imageView);

您仍然可以使用生成的 RequestOptions subclass 将同一组选项应用于多个加载,但生成的 RequestBuilder subclass 在大多数情况下可能更方便。

工作

Glide.with(context!!)
     .load(user.profileImage)
     .apply (RequestOptions.placeholderOf(R.drawable.dummy_user))
     .into(edit_profile_image)
RequestOptions options = new RequestOptions()
            .placeholder(R.drawable.null_image_profile)
            .error(R.drawable.null_image_profile);
    //.centerCrop()
    //.diskCacheStrategy(DiskCacheStrategy.ALL)
    //.priority(Priority.HIGH);

    Glide.with(context).load(imageUrl)
            .apply(options)
            .into(profileImage);

如果你想在你的应用程序的任何地方使用一个通用的占位符,那么你可以这样做:

当我们从 Glide v4 创建 GlideModule 时,您可以在您的项目中 copy/paste 这个 class所以你将能够使用 GlideApp class(更多步骤 - follow this):

@GlideModule
public class SampleGlideModule extends AppGlideModule {
    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
        builder.setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.logo).error(R.drawable.logo));
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
    }
}

你可以在这里给所有的请求选项设置为默认

通过创建此 class,您无需将 .placeholderGlideApp 一起使用,它将自动应用。