B4A Library [lib] ImageStock v1.0

Hi there!

I wrote a class that allows you to use a image stock... instead of loads into your projects a bunch of image files (eg all the icons for buttons and menu items), you can create a large image that contains them all, and give them a key to retrieve them easily (and, why not, reuse same images between apps).

Images must all be the same size ... but you can define multiple ImageStock inside your app, each with their own images and their size.

The images must be organized in a grid, with no extra spaces between them. Each grid cell must have exactly the image size. See the file provided with the sample (the icons used within the example are free and can be used for free and commercial apps ... but for those business you must indicate the author of the icons ... see readme and license files provided in the Files folder of sample - or tap relative buttons in sample app)

Of course, if you create your own image stock you're not limited to any license!

For ease and reuse, you can define the set of keys in a text file, and load it with the appropriate function of ImageStock. In the file, each row contains a key, and the keys must correspond to the images of ImageStock. The ImageStock must be seen as a single band of images, or as a linearized table: each row follows the previous one.

And if you want, you can use ImageStock also as a way to manage a tileset...

Note: Make a unique image with all icons in the app saves a lot of space (in my sample app, use the imagestock instead of individual icons, saved me more than 170Kb!)

the interface of my class is below:

B4X:
'Initializes the object. 
'Sets icon stock image and icon dimension (all icons must be the same Size).
Public Sub Initialize(ImageStock As Bitmap, ImageWidth As Int, ImageHeight As Int)

'Gets icons width.
Public Sub GetImageWidth As Int 

'Gets icon height.
Public Sub GetImageHeight As Int 

'Sets new icons width and height.
Public Sub SetImageSize(ImageWidth As Int, ImageHeight As Int)

'Gets current icon stock bitmap.
Public Sub GetImageStock As Bitmap

'Sets new icon stock bitmap.
Public Sub SetImageStock(ImageStock As Bitmap)

'Loads all keys from file. 
'File must be a text File, with each key on a separate line. 
'The Keys are linearized (line by line).
'Remember To keep the correspondence icons and keys
Public Sub LoadKeys(Dir As String, FileName As String)

'Sets all keys. Each item is a different key.
'The Keys are linearized (line by line).
'Remember To keep the correspondence icons and keys.
Public Sub SetKeys(KeyList As List)

'Returns all keys.
'The Keys are linearized (line by line).
Public Sub GetKeys As List

'Append a key to keyset. 
'Remember To keep the correspondence icons and keys.
Public Sub AppendKey(Key As String)

'Remove a key from keyset.
'Remember To keep the correspondence icons and keys.
Public Sub RemoveKey(Key As String) As Boolean

'Returns the number of icons by strip.
Public Sub GetCols As Int 

'Returns the number of icon strips.
Public Sub GetRows As Int 

'Returns the number of mapped keys.
Public Sub Size As Int 

'Returns the max number of icons storable into defined icon stock.
Public Sub Capacity As Int 

'Tool function: creates a bitmap that contains a portion of the original bitmap.
'Src is the original bitmap, Left, Top, PartWidth and PartHeight define the portion.
Public Sub CreateBitmapFromBitmap(Src As Bitmap, Left As Int, Top As Int, PartWidth As Int, PartHeight As Int) As Bitmap

'Tool function: resizes the original bitmap.
'Src is the image to resize, NewWidth and NewHeight define new image dimension.
'UseMatrix sets if use (true) or not use (false) the java matrix during resize operation.
'UseFilters sets if use (true) or not use (false) the smooth filter during resize operation.
Public Sub CreateScaledBitmap(Src As Bitmap, NewWidth As Int, NewHeight As Int, UseMatrix As Boolean, UseFilters As Boolean) As Bitmap

'Returns the icon from icon stock, mapped by Key.
Public Sub Get(Key As String) As Bitmap

'Returns the icon from icon stock, indexed by Index.
'The Keys are linearized (line by line).
Public Sub Get2(Index As Int) As Bitmap 

'Returns the icon from icon stock, mapped by Key, and resized by NewWidth and NewHeight.
'UseMatrix sets if use (true) or not use (false) the java matrix during resize operation.
'UseFilters sets if use (true) or not use (false) the smooth filter during resize operation.
Public Sub GetScaled(Key As String, NewWidth As Int, NewHeight As Int, UseMatrix As Boolean, UseFilters As Boolean) As Bitmap

'Returns the icon from icon stock, indexed by Index, and resized by NewWidth and NewHeight.
'UseMatrix sets if use (true) or not use (false) the java matrix during resize operation.
'UseFilters sets if use (true) or not use (false) the smooth filter during resize operation.
Public Sub GetScaled2(Index As Int, NewWidth As Int, NewHeight As Int, UseMatrix As Boolean, UseFilters As Boolean) As Bitmap

'Draws the icon, mapped by Key, on canvas, starting at position (X, Y)
Public Sub Draw(C As Canvas, Key As String, X As Int, Y As Int)

'Draws the icon, indexed by Index, on canvas, starting at position (X, Y)
Public Sub Draw2(C As Canvas, Index As Int, X As Int, Y As Int)

'Draws the icon, mapped by Key, on canvas, starting at position (X, Y)
'Icon will be resized by NewWidth and NewHeight.
'UseMatrix sets if use (true) or not use (false) the java matrix during resize operation.
'UseFilters sets if use (true) or not use (false) the smooth filter during resize operation.
Public Sub DrawScaled(C As Canvas, Key As String, X As Int, Y As Int, NewWidth As Int, NewHeight As Int, UseMatrix As Boolean, UseFilters As Boolean)

'Draws the icon, indexed by Index, on canvas, starting at position (X, Y)
'Icon will be resized by NewWidth and NewHeight.
'UseMatrix sets if use (true) or not use (false) the java matrix during resize operation.
'UseFilters sets if use (true) or not use (false) the smooth filter during resize operation.
Public Sub DrawScaled2(C As Canvas, Index As Int, X As Int, Y As Int, NewWidth As Int, NewHeight As Int, UseMatrix As Boolean, UseFilters As Boolean)

And now... happy coding! :)
 

Attachments

  • ImageStock.zip
    186.5 KB · Views: 495
  • shot1.jpg
    shot1.jpg
    22.5 KB · Views: 557
  • shot2.jpg
    shot2.jpg
    28.8 KB · Views: 553
  • shot3.jpg
    shot3.jpg
    32.4 KB · Views: 510
  • iconset1.jpg
    iconset1.jpg
    55.2 KB · Views: 631
Last edited:

Harris

Expert
Licensed User
Longtime User
Very nice. However, I need an eye loop magnifier to see the example icons.

Here is a tip:

Why not create packages of icons in popular sizes and categories and sell them back to us. Frankly, I hate searching for, creating, tweaking what end up resulting in awful looking icons for my projects.

Possibly the same strategy could b used for 9 patch as well.

These collections would be a great asset.

Thanks
 

Cableguy

Expert
Licensed User
Longtime User
Nice addition... Does anyone know of any piece of software capable of taking a folder content (images) and create automaticaly a StockImage?
 

Troberg

Well-Known Member
Licensed User
Longtime User
Nice! Reminds me alot of the old PictureClip in VB3, which I liked a lot. I've always found it much handier to use than the more object oriented, but more awkward to use, ImageList.

This way, with a large resource file, better suits how I like to work when doing the graphics.
 
Top