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:
And now... happy coding!
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
Last edited: