首页 > 解决方案 > How to set the background image for textview with Glide?

问题描述

How do I set background image for text with glide?

file[0] = "mnt/sdcard/sort_icon.png"


SimpleTarget target = new SimpleTarget<Drawable>(){
                    @Override
                    public void onResourceReady(Drawable resource, GlideAnimation<? super Drawable> glideAnimation) {
                        txtView.setBackground(resource);
                    }
                };
                Glide.with(getBaseContext())
                        .load(file[i-4])
                        .into(target);

标签: androidandroid-glide

解决方案


Solution:

Try this:

Add the implementation in your gradle(app):

dependencies {
    .....
    .....

    def glide = "4.8.0"

    // Glide
    implementation "com.github.bumptech.glide:glide:$glide"
    annotationProcessor "com.github.bumptech.glide:compiler:$glide"
    .....
}

Then, in your Activity/Fragment:

File file = new File(file[i-4]);
Uri imageURI = Uri.fromFile(file);

Glide.with(MainActivity.this)
     .load(imageURI)
     .apply(RequestOptions.placeholderOf(R.drawable.ic_launcher))
     .into(new SimpleTarget<Drawable>() {
          @Override
          public void onResourceReady(@NonNull Drawable resource, 
                      @Nullable Transition<? super Drawable> transition) {

                     txtView.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null);

                }
            });

See if it works.

Working with a URL


推荐阅读