【问题标题】:Do something on placeholder applied to Glide?对应用于 Glide 的占位符做些什么?
【发布时间】:2021-09-10 05:08:52
【问题描述】:

我能否知道在 加载 占位符 到 Glide 时是否有任何方法可以用来做某事。在这里,我使用Glide 将firebase 图像网址设置为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图片不可用时设置默认图片。

【问题讨论】:

    标签: android firebase android-glide placeholder


    【解决方案1】:

    您可以在这里使用fallback:

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

    如果url 是null,就会显示。

    以防万一error处理程序:

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

    【讨论】:

    • 谢谢,但是如何运行我的 setBitmap() ?
    【解决方案2】:

    终于,我找到了答案。但它不像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) {}
            });
    

    【讨论】:

    • 抱歉没有注意到asBitmap()。是的,在这种情况下,占位符将不起作用。感谢分享您的解决方案。
    【解决方案3】:

    尝试添加

                @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);
                }
    

    进入你的进入块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      相关资源
      最近更新 更多