在占位符上做一些应用于 Glide 的事情?

Do something on placeholder applied to Glide?

我可以知道在 loading placeholder 时是否有任何方法可以用来做某事吗滑行。在这里,我使用 Glidefirebase 图像 url 设置为 setBitmap 方法。如果资源可用 setBitmap 方法将 运行。如果它不可用,我想 运行 另一种方法。有没有类似onLoadingPlaceholder()的方法?

 Glide.with(getApplicationContext()).asBitmap()
                            .load(dataSnapshot.child("imageUrl").getValue(String.class))
                            .apply(new RequestOptions()
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .placeholder(R.drawable.image)
                                    .error(R.drawable.image))
                            .into(new CustomTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                    Log.d(TAG, "onResourceReady: ");
                                    setBitmap(resource);
                                }

                                @Override
                                public void onLoadCleared(@Nullable Drawable placeholder) {}
                            });

简单地说,我需要做的是,在加载占位符时设置位图。所以我需要在 firebase 图片不可用时设置默认图片。

您可以像这里一样使用 fallback

Glide.with(fragment)
  .load(url)
  .fallback(R.drawable.fallback)
  .into(view);

如果urlnull就会显示。

以防万一 error 处理程序:

Glide.with(fragment)
  .load(url)
  .error(R.drawable.error)
  .into(view);

终于,我找到了答案。但它不像 onLoadPlaceholder(),这对我有用!

String imageUrl = dataSnapshot.child("imageUrl").getValue(String.class);
if(imageUrl==null){
    setBitmap(resource);
}

Glide.with(getApplicationContext()).asBitmap()
        .load(imageUrl)
        .apply(new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.contatctmani)
                .error(R.drawable.contatctmani))
        .into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                setBitmap(resource);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {}
        });

尝试添加

            @Override
            public void onLoadStarted(@NonNull placeholder Drawable) {
               // here you call another method, e.g.
               imageView.setImageDrawable(placeholder);
            }

            @Override
            public void onLoadFailed(@NonNull errorDrawable: Drawable) {
               // here you call another method, e.g.
               imageView.setImageDrawable(errorDrawable);
            }

进入你的 into 块。