Android Question Random images instead of random numbers

luisftv

Member
Licensed User
Longtime User
How do I show random images instead of random numbers on the center of the screen?


I have a small app that shows random numbers (0 through 22, for example). You press a button and a random numbers shows in the center of the screen for a few seconds and then it automatically disappears. It is working perfectly.


Here is the basic code:

B4X:
Sub Globals
    Dim K As Int
    Dim Label1 As Label
    Dim btnrandom As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout01")
    Timer1.Initialize ("Timer1",2000)
End Sub

Sub Timer1_Tick
    Label1.Text = ""
    Timer1.Enabled = False
End Sub

Sub btnrandom_Click
    k = Rnd (0, 23)
    Label1.Text = K
    Timer1.Enabled = True
End Sub


Now, I’ve been trying to show random images instead. I have 22 small images named “image00.jpg”, “image01.jpg”, etc all the way to “image22.jpg”. I know that I have to use “ImageView” in the Visual Designer instead of “Label1” which I currently use to show the random numbers. I tried mixing suggestions in the forum but I got nowhere… If I don’t get a Java error I get a missing value…


Can I also show a mix of image formats, not just jpg but png and bmp etc all at once?


I know that in Sub Globals I have to Dim ImageView1 as ImageView, and that in Sub btnrandom_Click and won’t need the Label1.text = K from the current code…


I have tried some of these suggestions and combined them, but the answer evades me, perhaps because my implementation varies too much:


http://www.b4x.com/android/forum/threads/loading-imageview-bitmaps.19044/


http://www.b4x.com/android/forum/threads/how-to-load-an-image-at-random.21047/


http://www.b4x.com/android/forum/threads/load-layout-randomly.42591/


Some of these examples only include the bottom line code but not the background info or additional/supporting code or libraries to load… I can’t guess it… I also tried loading all the libraries available, and still nothing.


I’m using Basic4Android 3.82


Any suggestions? Please help, and, thank you so much in advanced.
 

KMatle

Expert
Licensed User
Longtime User
You can use both views. In the designer you have added label1 (for the number). So add an imageview and put it next to the label (don't forget to generate the "Dim"). Let's say you called it MyImageView. No additional libs needed :)

In your code

Sub btnrandom_Click
k = Rnd (0, 23)
Label1.Text = K
Timer1.Enabled = True
End Sub

add


Sub btnrandom_Click
k = Rnd (0, 23)
Label1.Text = K
ImageName = "image" & NumberFormat(K,2,0) & ".jpg"
MyImageView.Bitmap = LoadBitmap(File.DirAssets, ImageName")
Timer1.Enabled = True
End Sub

ImageName must be added to Globals ("DIM ImageName as string")

The Numberformat adds a 0 to values < 10 and the filename will be generated to be variable.

Do you want to add the image to the button?

Sorry, no Java @work :-(
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
... and you don't need to rename all your images if you use a Map.

You could add the paths of the image files to a Map, using a progressive number as Map Key and path as Value, then the random number could be the Map key, allowing you to load the "selected" picture.

Note that the Map could contain the bitmaps instead of the paths, but this is not convenient for the memory consumption.
 
Upvote 0

luisftv

Member
Licensed User
Longtime User
Perfect... I got it to work thanks to you both Klaus Matle and LucaMS.

There is only one thing that still doesn't work: the timer. If you see my original code, the timer was automatically clearing the numbers from the screen after 2 seconds, but the timer was linked to the "Label1.Text" in the Sub Timer1_Tick. I can't figure out what to link it to for the images... the "ImageView1" dot something?... but what?

Aside from the timer, everything is working perfectly. Here is the working and tested code so far:

B4X:
Sub Process_Globals
    Dim Timer1 As Timer
End Sub

Sub Globals
    Dim K As Int

    'Dim Label1 As Label 'Not needed since I want to show ONLY images now, instead of numbers

    Dim btnrandom As Button
    Dim ImageView1 As ImageView
    Dim ImageName As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout01")
    Timer1.Initialize ("Timer1",2000)
End Sub

Sub Timer1_Tick
    'ImageView1 ??? 'What do i type here to have the images desappear from the screen after 2 seconds, as I did with the numbers???
    'Label1.Text = "" 'Not needed since I want to show only images now, instead of numbers
    Timer1.Enabled = False
End Sub

Sub btnrandom_Click
    k = Rnd (0, 11)
 
    'Label1.Text = K 'This is no longer needed because I ONLY want to show images, instead of numbers

    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "Image" & NumberFormat(k, 2, 0) & ".png")
    Timer1.Enabled = True
End Sub

How do I fix the timer?

NOTE: The image file names go as image00.png, image01.png, etc., and they were added to the Assets within Basic4Android "Add Files" option... for those like me who love details.

Thanks... sincerely.

p.s. I'm only using 10 images now, not that it matters.
 
Last edited:
Upvote 0

luisftv

Member
Licensed User
Longtime User
LucaMS, thank you... that did it!!!!

Now here is the fully working code:

B4X:
Sub Process_Globals
    Dim Timer1 As Timer
End Sub

Sub Globals
    Dim K As Int
    Dim btnrandom As Button
    Dim ImageView1 As ImageView
    Dim ImageName As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout01")
    Timer1.Initialize ("Timer1",2000)
End Sub

Sub Timer1_Tick
    ImageView1.Bitmap = Null
    Timer1.Enabled = False
End Sub

Sub btnrandom_Click
    k = Rnd (0, 11)
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, "Image" & NumberFormat(k, 2, 0) & ".png")
    Timer1.Enabled = True
End Sub


LucaMS, thanks again.
 
Upvote 0
Top