Is it possible to use Glide to download image to load into a TextView?

后端 未结 4 556
时光说笑
时光说笑 2021-02-06 01:31

In my app I download an image from an URL and set it into an ImageView through Glide, however, I\'m trying to remove a few unnecessary layouts, so is it possible to use Glide to

相关标签:
4条回答
  • 2021-02-06 01:40
    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);
            }
    });
    
    0 讨论(0)
  • 2021-02-06 01:43

    In Glide 4.9.0 SimpleTarget is deprecated. You can use CustomTarget instead.

    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);
            }
        });
    
    0 讨论(0)
  • 2021-02-06 01:54

    Using 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);
             }
    });
    
    0 讨论(0)
  • 2021-02-06 01:55

    Here is a simple example of how to do using 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)
                    }
    
                })
    
    0 讨论(0)
提交回复
热议问题