Set Background Image to Relative Layout using Glide in Android

后端 未结 7 555
逝去的感伤
逝去的感伤 2020-12-24 11:33

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

7条回答
  •  攒了一身酷
    2020-12-24 12:13

    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.

    For Glide version before 4.X

    Glide.with(this).load(imageViewPath).asBitmap().into(new SimpleTarget(relLayoutWidth, relLayoutHeight) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation 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.

    Update for Glide v4 onward:

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

提交回复
热议问题