Android开发之Glide设置View背景图的方法

先看效果图:

看了下效果还可以,虽然此方法在Glide4.1.2版本已过时但是还可以使用

针对Glide4.0以上版本设置View背景图方法如下:Kotlin版本

package com.noboauto.module_album.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.noboauto.module_album.R

/**
 * @author xiayiye5
 * @date 2021/10/18 15:12
 */
class Demo2 {
    /**
     * 使用本地图片显示背景图的方法
     *
     * @param context 上下文对象
     * @param view    要显示背景图的控件
     */
    fun show1(context: Context?, view: View) {
        Glide.with(context!!).load(R.drawable.base_ic_back)
            .into(object : SimpleTarget<Drawable?>() {
                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable?>?
                ) {
                    view.background = resource
                }
            })
    }

    /**
     * 使用本地图片显示背景图的方法
     *
     * @param context 上下文对象
     * @param view    要显示背景图的控件
     * @param url     背景图的url
     */
    fun show2(context: Context?, view: View, url: String?) {
        Glide.with(context!!).load(url).into(object : SimpleTarget<Drawable?>() {
            override fun onResourceReady(
                resource: Drawable,
                transition: Transition<in Drawable?>?
            ) {
                view.background = resource
            }

        })
    }
}

再看下Java版本写法

package com.noboauto.module_album.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.noboauto.module_album.R;

/**
 * @author xiayiye5
 * @date 2021/10/18 15:12
 */
public class Demo {
    /**
     * 使用本地图片显示背景图的方法
     *
     * @param context 上下文对象
     * @param view    要显示背景图的控件
     */
    public void show1(Context context, View view) {
        Glide.with(context).load(R.drawable.base_ic_back).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    view.setBackground(resource);
                }
            }
        });
    }

    /**
     * 使用本地图片显示背景图的方法
     *
     * @param context 上下文对象
     * @param view    要显示背景图的控件
     * @param url     背景图的url
     */
    public void show2(Context context, View view, String url) {
        Glide.with(context).load(url).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    view.setBackground(resource);
                }
            }
        });
    }
}

感谢原博主:博主直达

猜你喜欢

转载自blog.csdn.net/xiayiye5/article/details/120826972