在 Android 中使用 Glide 将背景图像设置为相对布局 [英] Set Background Image to Relative Layout using Glide in Android

查看:33
本文介绍了在 Android 中使用 Glide 将背景图像设置为相对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Glide 执行此操作?我想 cache 图像也可以再次使用它.提前致谢.

How can I do this using Glide? I want to cache image to use it another time also. Thanks in advance.

推荐答案

您可以像这样在 RelativeLayout 中加载图像.我只是向您展示了将图像设置为背景的困难部分.

You can load an image in a RelativeLayout like this. I'm just showing you the hard part which is setting an image to the background.

对于 4.X 之前的 Glide 版本

Glide.with(this).load(imageViewPath).asBitmap().into(new SimpleTarget<Bitmap>(relLayoutWidth, relLayoutHeight) {
    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
        Drawable drawable = new BitmapDrawable(context.getResources(), resource);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            yourRelativeLayout.setBackground(drawable);
        }
    }
});

关于缓存,参考这个页面:缓存和缓存失效.

For caching, refer to this page: Caching and Cache Invalidation.

Glide v4 以后的更新:

GlideApp.with(this).load(R.drawable.backgroundimage).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    yourRelativeLayout.setBackground(resource);
                }
            }
        });

这篇关于在 Android 中使用 Glide 将背景图像设置为相对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆