Android Question How to display things with time in between

clurbina

Member
Licensed User
Longtime User
I am trying to learn how display text with some time in between.
I tried a regular sub but it only returned the last item.
I looked at the forum and guessed that I ndeeded a Resumable Sub.
So I watched the video on Resumable Subs twice, then borrowed ImagesSameCanvas and tried to see if I could display the 4 images with 2 seconds in between.
If I call DRAWIT without the Wait For, all 4 images display but without the 2 second sleep.
If I call DRAWIT with the Wait For, only the first image displays.
If I step through the code in debug mode, nothing shows.
I know I am doing something wrong, but can't figure out what it is.
I have been trying for a while and it's time to ask for help.
The code is below.
Any suggestions?
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: ImagesSameCanvas
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region
'Activity module
Sub Process_Globals
End Sub
Sub Globals
    Dim cvsActivity As Canvas
End Sub
Sub Activity_Create(FirstTime As Boolean)
    cvsActivity.Initialize(Activity)
    Dim i As Int
    Dim imgW, imgH As Int
   
    imgW = 50%x
    imgH = imgW / 3 * 2        ' image ratio W/H = 3/2
    For i = 0 To 3
        'this shows all 4 images in the proper place, but no sleep
'        drawit(i,imgW,imgH)
       
        'this shows only the first image in the proper place
        wait for (drawit(i,imgW,imgH)) complete (Result As Object)
       
        'NOTE: In debug mode, nothing shows
    Next
End Sub
Sub Activity_Resume
End Sub
Sub drawit(i As Int, imgW As Int, imgH As Int) As ResumableSub
    Dim r As Rect
    Dim x, y As Int
    x = (i Mod 2) * imgW
    y = Floor(i / 2) * imgH
    r.Initialize(x, y, x + imgW, y + imgH)
    cvsActivity.DrawBitmap(LoadBitmap(File.DirAssets, "image" & i & ".jpg"), Null, r)
    Sleep(2000)
    Return Null
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
hi, can you try my code? it´s more simple.

B4X:
Sub Process_Globals
    Private timer1 As Timer
End Sub
Sub Globals
    Dim cvsActivity As Canvas
    Private imgindex As Int
    Dim imgW, imgH As Int
 End Sub
Sub Activity_Create(FirstTime As Boolean)
    cvsActivity.Initialize(Activity)
    imgW = 50%x
    imgH = imgW / 3 * 2
    timer1.Initialize("timer1",2000)
    timer1.Enabled=True
End Sub
Sub Activity_Resume
End Sub
Sub timer1_Tick
    drawit(imgindex)
    imgindex=imgindex+1
    If imgindex>3 Then timer1.Enabled=False
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub drawit(i As Int)
    Dim r As Rect
    Dim x, y As Int
    x = (i Mod 2) * imgW :  y = Floor(i / 2) * imgH
    r.Initialize(x, y, x + imgW, y + imgH)
    cvsActivity.DrawBitmap(LoadBitmap(File.DirAssets, "image" & i & ".jpg"), Null, r)
    Activity.Invalidate ' Important
End Sub
 
Upvote 0

clurbina

Member
Licensed User
Longtime User
Thank you very much Eme. It worked perfectly. I will study it until I understand why it worked without using a resumable sub.
 
Upvote 0
Top