sagar suri
sagar suri

Reputation: 4731

Proper way to set background image for a layout android

I have tried many possible ways to set a perfect background image for a layout but never got a perfect result. Let me describe you my problem. I have an image of size 2880 x 5120 pixels. I have kept the image in the xxxhdpi folder and then loading the image using Glide library as the background image. I am resizing the image by calculating the screen size programmatically. Here is my following code to load image as the background image.

Glide.with(LoginActivity.this)
                    .load(R.drawable.login_background)
                    .override(screenSize[0]*2,screenSize[1])
                    .animate(animationObject)
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(backgroundImage);

screenSize() method:

private int[] screenSize() {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return new int[]{size.x, size.y};
    }

This is the actual background image:

enter image description here

After loading the image as the background image. I am getting the below result.

enter image description here

The image is not completely fitting the screen. What can be the best optimal solution to set a background image ?

Upvotes: 0

Views: 784

Answers (1)

Aalap Patel
Aalap Patel

Reputation: 2076

You need not go through this trouble of calculating different screen size. You should use different size image resources and android will handle it as per the screen size. You can use this post to read more but I have summarized sizes you can use in your project for the background image.

It has all the sizes you need to use for different screens as well as landscape mode sizes are mentioned.

MDPI:
Portrait: 320x480px
Landscape: 480x320px
HDPI:
Portrait: 480x800px
Landscape: 800x480px
XHDPI:
Portrait: 720px1280px
Landscape: 1280x720px
XXHDPI:
Portrait: 960px1600px
Landscape: 1600x960px
XXXHDPI:
Portrait: 1280px1920px
Landscape: 1920x1280px

Upvotes: 1

Related Questions