Keep For loop alive forever?

susu

Well-Known Member
Licensed User
Longtime User
I'm trying to create my first live wallpaper app that show about 200 frames of images named Frame0.png, Frame1.png.... Frame200.png

My WallpaperService code:

B4X:
'Service module
Sub Process_Globals
   Dim lwm As LWManager
   Dim LiveEngine As LWEngine
   Dim image As Bitmap
End Sub

Sub Service_Create
   lwm.Initialize("lwm", True)
   lwm.StartTicking(200)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Sub lwm_SizeChanged (Engine As LWEngine)
   DrawBackground (Engine)
End Sub

Sub DrawBackground (Engine As LWEngine)
   Engine.Canvas.DrawColor(Colors.White)
   Engine.RefreshAll
End Sub

Sub lwm_VisibilityChanged (Engine As LWEngine, Visible As Boolean)
   If Visible Then
      DrawBackground (Engine)
   End If
End Sub

Sub lwm_Tick (Engine As LWEngine)
   Engine.Rect.Initialize(40dip, 20dip, 300dip, 300dip)

   Dim i As Int
        For i = 0 To 200
      Engine.Canvas.DrawColor(Colors.White)
      image.Initialize(File.DirAssets, "Frame" & i &".png")
      Engine.Canvas.DrawBitmap(image, Null, Engine.Rect)
      Engine.RefreshAll
   Next
End Sub

It works but after frame 200, it stops. So can you show me how to keep For loop alive forever?

Is this way consume so much CPU power? Do you have another good idea to make a live wallpaper? Please share.

Thank you so much.
 

Cableguy

Expert
Licensed User
Longtime User
B4X:
Sub lwm_Tick (Engine As LWEngine)
   Engine.Rect.Initialize(40dip, 20dip, 300dip, 300dip)
      
        [COLOR="darkorange"]If i =201 then i=0[/COLOR]
      Engine.Canvas.DrawColor(Colors.White)
      image.Initialize(File.DirAssets, "Frame" & i &".png")
      Engine.Canvas.DrawBitmap(image, Null, Engine.Rect)
      Engine.RefreshAll
       [COLOR="DarkOrange"] i=i+1[/COLOR]
End Sub

Dim i in globals as Int and in the service create set it to be 0(zero)
This way, when service starts, i is alway zero, and in each tick event is gets implemented by 1, unless it is equal to 201, in wich case, it gets setted to be 0(zero) again, rendering an endless loop
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
It worked. Thank you Cableguy.
 
Upvote 0

Mats Kihlberg

New Member
Licensed User
Longtime User
Hi,
I have tried to do the same thing, but it doesn't work. Not at my smartphone anyway.
I've got a Sony Experia S.
When I try to run the wallpaper I got the message that it has stopped running. No image will show up.
I can't figure out what the problem is.
Do you have any idea what I might do wrong???

/ Mats
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…