What is the recommended size for background images?

grant1842

Active Member
Licensed User
Longtime User
If it's just a pattern background that doesn't really lose quality when downsized I just make it 800x1280 for portrait and 1280x800 for landscape. Then I just user LoadBitmapSample to efficiently load it.

Now if it's a background image then you can import an image for each device size ( ldpi,mdpi,etc.)
 
Upvote 0

HimeAnator

Member
Licensed User
Longtime User
If it's just a pattern background that doesn't really lose quality when downsized I just make it 800x1280 for portrait and 1280x800 for landscape. Then I just user LoadBitmapSample to efficiently load it.

Now if it's a background image then you can import an image for each device size ( ldpi,mdpi,etc.)

Thank you for the reply grant1842. I am using an image not a pattern. Sorry if this is a noob question lol but how do you detect the type/size such as ldpi,mdpi,hdpi and xhdpi?

The background I created in Photoshop is 1333 x 2542 pixels and I believe is the cause of the Out Of Memory errors I am receiving from users but I'm very unsure of what to do. :sign0104:
 
Last edited:
Upvote 0

HimeAnator

Member
Licensed User
Longtime User
Right now this is how I load the background

B4X:
Dim BG As Bitmap
BG.InitializeSample(File.DirAssets, "background.jpg",Activity.Width,Activity.Height)
Activity.SetBackgroundImage(BG)

Is this not essentially the same thing?

The only way I can get my app run without seeing the Memory error is if I take all of the background images out. But then it doesn't look good at all :/

The simplest approach is to use LoadBitmapSample(..., 100%x, 100%y).
Currently the designer internally uses LoadBitmap to load the images. It will be changed to LoadBitmapSample instead.
 
Last edited:
Upvote 0

HimeAnator

Member
Licensed User
Longtime User
Ok I think I may have solved the problem. I tried declaring the background with

B4X:
Dim Image1 As Bitmap
         
               
            Image1 = LoadBitmapSample(File.DirAssets,"background.jpg",100%x,100%y)
            Image1 = CreateScaledBitmap(Image1,Activity.Width,Activity.Height)
            
      Activity.SetBackgroundImage(Image1)

and I have yet to have it crash after extensive testing. I will keep testing but I think its running good now.


[Update]
Didn't work still crashes, but not as much.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
On high resolution devices / tablets 100%x * 100%y might be too large. You can load a smaller image instead. Note that CreateScaledBitmap will not help here.

It depends whether the image dimensions are bigger than the screen size.
CreateScaledBitmap resizes precisely the image, contrary to LoadBitmapSample. When you use LoadBitmapSample, you don't get the width and height passed as parameters, but a computation based on the nearest power of 2. With some sizes, this is not trivial.
Example for a screen size of 1280x600 with an image of 1500x800:
1500/1280=1.17, 800/600=1.33. In both cases, the nearest power of 2 (rounded down) is 1 so the final result of LoadBitmapSample is unchanged. Memory used = 1500x800x4 = 4 800 000.
After CreateScaledBitmap: 1280x600x4 = 3 072 000.
1.6 MB of difference !
And AFAIK, all devices/tablets with high resolution, even the low cost ones from China, have enough memory to display an image 100%x * 100%y.
What is important is the number of pixels per inch. On a Retina display with more than 260 pixels per inch, using an image with a lower resolution than the screen is unnoticeable. That's less true on cheaper screens.
 
Last edited:
Upvote 0
Top