B4A Library [B4X] BitmapsAsync

Don't use this. Better implementation in SimpleMediaManager: https://www.b4x.com/android/forum/t...rk-for-images-videos-and-more.134716/#content
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.


1593435196447.png
 

Attachments

  • BitmapsAsync.b4xlib
    1.7 KB · Views: 1,777
  • GridExample.zip
    187.3 KB · Views: 1,462
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Forgot to write that there is a field named MaxFileSize. It is set to 3mb by default (3 * 1024 * 1024). Larger images will not be rejected. You can increase or decrease the limit as you need.
B4X:
ImageLoader.MaxFileSize = 10 * 1024 * 1024 '10mb

The idea is to have a safe limit and avoid loading huge images unexpectedly.
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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
Longtime 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)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: in most cases you should use SimpleMediaManager instead of BitmapsAsyncs.

is it possible to integrate it with a click event on the image?
It has no real relation to BitmapsAsync. BitmapsAsync simply loads the bitmap.
Add a transparent panel above each B4XImageView and handle its Click event.
 

omarruben

Active Member
Licensed User
Longtime 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.


View attachment 96374
how to capture a click on any image?
 

Maicon

Member
Licensed User
Longtime User
How to get the corresponding value of each image? always comes the value of the column of 4 in 4

1645478866242.png



1645478888524.png
 
Top