Android Tutorial LoadBitmap / LoadBitmapResize / LoadBitmapSample

Erel

Administrator
Staff member
Licensed User
This tutorial explains the differences between the three methods.

The bottom line is that the best option is to use LoadBitmapResize and set the container gravity, if possible, to Gravity.CENTER.

LoadBitmap - Simply loads the bitmap as-is. Loading unknown bitmap files with LoadBitmap is considered a programming bug as it can easily lead to out of memory errors.
The memory required to load a bitmap is not related to the file size but rather to the image size. It is approximately width * height * 4.

LoadBitmapSample - Returns a bitmap that its size is equal or larger than the passed width and height values. This means that the container gravity should be set to FILL (this is the default value). Otherwise the image might be larger than the container.
This also means that the aspect ratio will be lost.

LoadBitmapResize - Returns a bitmap that its size equals to the passed width and height values. If the KeepAspectRatio parameter is set to True (in most cases it should be) then the aspect ratio will be kept. In that case the width or height (not both) might be smaller than the passed values. This is similar to B4i FIT content mode.
The bitmap scale is set based on the device scale.

Usage example:
B4X:
'loading an icon that should be 32 x 32 pixels (the dip units are important!).
Dim bmp As Bitmap = LoadBitmapResize(File.DirAssets, "someimage.png", 32dip, 32dip, True)
The image file should be 64 x 64 pixels or more. The actual physical size will be more or less the same on all devices.

View.SetBackgroundImage returns a BitmapDrawable object. This allows us to set the gravity to center and keep the bitmap aspect ratio:
B4X:
Dim bg As Bitmap = LoadBitmapResize(File.DirAssets, "bg.png", ImageView1.Width, ImageView1.Height, True)
ImageView1.SetBackgroundImage(bg).Gravity = Gravity.CENTER
 

LucaMs

Expert
Licensed User
Usage example:
B4X:
'loading an icon that should be 32 x 32 pixels (the dip units are important!).
Dim bmp As Bitmap = LoadBitmapResize(File.DirAssets, "someimage.png", 32dip, 32dip, True)
The image file should be 64 x 64 pixels or more.
What means this?

I really don't understand the example. You load an image file whose ratio is 1:1 using LoadBitmapResize with 32dip x 32dip, same ratio, 1:1.
What would be the purpose in the example?


I tried another example, using an image file whose ratio is 1.5:1.
Two ImageViews:
1) ratio 1:1
2) ratio 1.5:1

1) used LoadBitmapResize(... ImageView1.Width, ImageView1.Height, True)
2) used the simple LoadBitmap.

Bad result for the first ImageView.


I thought the result would be what you get using the routine created and published by Klaus in this thread. I think this is a feature that is still missing.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
I really don't understand the example. You load an image file whose ratio is 1:1 using LoadBitmapResize with 32dip x 32dip, same ratio, 1:1.
What would be the purpose in the example?
The purpose is to have a 32dip x 32dip bitmap. This is not something that you can do with LoadBitmap.

1) used LoadBitmapResize(... ImageView1.Width, ImageView1.Height, True)
2) used the simple LoadBitmap.

Bad result for the first ImageView.
Have you set the ImageView gravity to CENTER?
 

Erel

Administrator
Staff member
Licensed User
And with LoadBitmapSample(... 32dip, 32dip)?
The size will be equal or larger:
Returns a bitmap that its size is equal or larger than the passed width and height values.
This means that you cannot set the gravity to center and you lose the aspect ratio if the container doesn't have the same aspect ratio as the bitmap.
 
Top