无法使用Glide加载图像

时间:2018-01-10 10:30:55

标签: android android-glide

我在我的android项目中使用Glide v4。这是我使用listener的实现。但这并没有给我任何回调:

Glide.with(HomeScreenActivity.this).load(url).listener(new RequestListener<Drawable>() {
    @Override
    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
        e.printStackTrace();
        videoCard_view.setVisibility(View.GONE);
        video_linearlayout.setVisibility(View.GONE);
        fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
        return false;
    }

    @Override
    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
        Log.e(TAG,"Video Image loaded");
        videoCard_view.setVisibility(View.VISIBLE);
        video_linearlayout.setVisibility(View.VISIBLE);
        fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
        return true;
    }
}).apply(options.centerCrop()).into(video_ImageView);

但是当我使用下面的风格时它的工作完美。为什么会这样?:

Glide.with(HomeScreenActivity.this).load(url).apply(options.centerCrop()).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
    Log.e(TAG,"video image loaded");
    video_ImageView.setImageDrawable(resource);
}
});

1 个答案:

答案 0 :(得分:1)

false方法返回onResourceReady

Glide.with(HomeScreenActivity.this).load(url).listener(new RequestListener<Drawable>() {
    @Override
    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
        e.printStackTrace();
        videoCard_view.setVisibility(View.GONE);
        video_linearlayout.setVisibility(View.GONE);
        fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
        return false;
    }

    @Override
    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
        Log.e(TAG,"Video Image loaded");
        videoCard_view.setVisibility(View.VISIBLE);
        video_linearlayout.setVisibility(View.VISIBLE);
        fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
        return false;
    }
}).apply(options.centerCrop()).into(video_ImageView);

如果video_ImageView或其父级为InvisibleGone,则不会调用onLoadFailedonResourceReady个回调,因为Glide需要在进展到加载图像之前的大小,不可见的视图没有有效的大小。或者,您可以使用SimpleTarget代替ImageView

Glide.with(HomeScreenActivity.this).load(url).listener(new RequestListener<Drawable>() {
    @Override
    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
        e.printStackTrace();
        videoCard_view.setVisibility(View.GONE);
        video_linearlayout.setVisibility(View.GONE);
        fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
        return false;
    }

    @Override
    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
        Log.e(TAG,"Video Image loaded");
        videoCard_view.setVisibility(View.VISIBLE);
        video_linearlayout.setVisibility(View.VISIBLE);
        fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
        return false;
    }
}).apply(options.centerCrop())
  .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, 
                                                @Nullable Transition<? super Drawable> transition) {
                        video_ImageView.setImageDrawable(resource);
                    }
                });
相关问题