B4A Library [B4X] BitmapsAsync

Erel

Administrator
Staff member
Licensed User
A cross platform library that allows loading images using background threads. It can be useful with keeping the app responsive while loading many images.

It is specifically built to work with HttpJobs.

It has two methods:
LoadFromHttpJob (HttpJob, MaxWidth, MaxHeight) - Loads a bitmap that was downloaded with HttpJob.
LoadFromFile (Dir, FileName, MaxWidth, MaxHeight) - Loads a local file

Usage example:
B4X:
Wait For (j) JobDone (j As HttpJob)
If j.Success Then
Wait For (ImageLoader.LoadFromHttpJob(j, 500dip, 500dip)) Complete (bmp As B4XBitmap)
If Bmp.IsInitialized Then
   'work with bitmap
End If
End If
If the image size is larger than MaxWidth or MaxHeight then the image will be downsampled, similar to LoadBitmapSample (not LoadBitmapResize).
The best way to work with such images is by setting the ImageView gravity to Fill and resizing the ImageView based on the bitmap size. See the attached example.

Notes:

- In B4J the images are loaded synchronously.
- In B4i the images are never downsampled. The reason behind it is that the OS takes care of uncompressing the image data right before it is displayed with the correct size.
- The example shows two ways to resize the ImageView. In both methods there is an assumption that each ImageView is inside a panel and the dimensions are based on that panel. Each ImageView should be in its own panel.
ResizeImageViewBasedOnBitmapRatio - Sets the image to be as large as the panel while preserving the image ratio.
ResizeImageViewAndFillWidth - Sets the image to fill the parent width while preserving the image ratio. The image might be cut at the top and bottom.

1593435196447.png
 

Attachments

Erel

Administrator
Staff member
Licensed User
Q: Why resize the ImageViews instead of resizing the bitmaps?

A: Good question. Resizing the bitmaps is very simple, however when performance is critical it should be avoided. It is done on the CPU (on the main thread!) and it is a relatively heavy task. By resizing the ImageView we are letting the hardware acceleration feature do its part.
 

FelixCusi

Member
Licensed User
Hello
Do you have an example using this method?
LoadFromFile

This gives me error:
Dim bmp As B4XBitmap=ImageLoader.LoadFromFile(j,500dip,500dip)

Felix
 

Alexander Stolte

Expert
Licensed User
Do you have an example using this method?
LoadFromFile (Dir, FileName, MaxWidth, MaxHeight) - Loads a local file
same handle as in the LoadFromHttpJob, look at your parameters... they are wrong. You need Dir and FileName.
B4X:
Wait For (ImageLoader.LoadFromFile ("yourDir","yourFilename", 500dip, 500dip)) Complete (bmp As B4XBitmap)
 
Top