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

查看:1120
本文介绍了在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天全站免登陆