能否使用Glide下载图片并加载到TextView中?

16

我在我的应用程序中通过Glide从URL下载图像并将其设置到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();
}

1
你会如何在 ImageView 上设置 Bitmap - Emmanuel
你的意思是在文本视图上设置位图吗?那就是我的问题。 - RedEagle
你不能将图像设置到 TextView 中。你可以将一个 Drawable 设置到 TextView 的侧面,但不能在 TextView 内部设置。 - Emmanuel
@Emmanuel,这正是我的问题......我是否应该使用Glide将图像下载为位图,然后将其转换为可绘制对象,还是有更聪明的方法..? - RedEagle
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Emmanuel
@tinysunlight 好的 - RedEagle
4个回答

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

谢谢!我也在尝试达到同样的目标,现在我可以了 :) - Pangu
谢谢。对我非常有用。 - hungtdo
加载(((FixturesListObject) object).getHomeIcon()),你能解释一下这行代码吗? - Ramana V V K
1
SimpleTarget现在已经过时了。 - merdan

14

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

6
使用 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);
         }
});

5

这里是一个使用 Kotlin 的简单示例。

GlideApp.with(context)
            .load("url")
            .placeholder(R.drawable.your_placeholder)
            .error(R.drawable.your_error_image)
            .into(object : CustomTarget<Drawable>(100, 100) {
                override fun onLoadCleared(drawable: Drawable?) {
                    header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
                }

                override fun onResourceReady(res: Drawable, transition: com.bumptech.glide.request.transition.Transition<in Drawable>?) {
                    header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(res,null,null,null)
                }

            })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接