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:
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:
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)
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