是否可以使用Glide下载图像加载到TextView中?

问题描述 投票:6回答:3

在我的应用程序中,我从URL下载图像并通过Glide将其设置为ImageView,但是,我正在尝试删除一些不必要的布局,那么是否可以使用Glide下载图像并设置为TextView?

try {
  Glide.with(holder.logo.getContext())
       .load(standingObjectItems.get(position).getImgId()).diskCacheStrategy(DiskCacheStrategy.ALL)
       .error(R.mipmap.ic_launcher)
       .placeholder(R.mipmap.ic_launcher)
       .into(holder.logo);

} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
  e.printStackTrace();
}
android android-glide
3个回答
20
投票
Glide.with(left.getContext())
     .load(((FixturesListObject) object).getHomeIcon())
     .asBitmap()
     .into(new SimpleTarget<Bitmap>(100,100) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
          left.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(left.getResources(),resource), null, null);
        }
});

4
投票

使用Glide 4.7.1:

Glide.with(context)
     .load(someUrl)
     /* Because we can*/
     .apply(RequestOptions.circleCropTransform())
     /* If a fallback is not set, null models will cause the error drawable to be displayed. If
      * the error drawable is not set, the placeholder will be displayed.*/
     .apply(RequestOptions.placeholderOf(R.drawable.default_photo))
     .into(new SimpleTarget<Drawable>() {
         @Override
         public void onResourceReady(@NonNull Drawable resource,
                 @Nullable Transition<? super Drawable> transition) {
             /* Set a drawable to the left of textView */
             textView.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null);
         }
});

1
投票

在Glide 4.9.0中,不推荐使用SimpleTarget。您可以改用CustomTarget。

Glide.with(myFragmentOrActivity)
         .load(imageUrl)
         .into(new CustomTarget<Drawable>(100,100) {

        @Override
        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition)
        {
            left.setCompoundDrawablesWithIntrinsicBounds(null, resource, null, null);
        }

        @Override
        public void onLoadCleared(@Nullable Drawable placeholder)
        {
            left.setCompoundDrawablesWithIntrinsicBounds(null, placeholder, null, null);
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.