Sub Process_Globals
Dim TimerHorse As Timer
Dim TimerBackground As Timer
Dim TimerInterval As Long
End Sub
Sub Globals
Dim ImageI As Int ' index of the current horse image
Dim ImageDir As Int ' direction of the horse image
Dim ImageNumber As Int ' number of horse images
ImageNumber = 8
Dim imgHorse(2, ImageNumber) As Bitmap ' array horse image bitmaps
Dim imvBackground As ImageView ' background ImageView
Dim imvForeground As ImageView ' foreground ImageView
Dim cvsForeground As Canvas ' canvas for the foreground image
Dim rectHorse As Rect ' rectangle of the horse image
Dim HorseWidth As Int ' horse image width
Dim HorseHeight As Int ' horse image height
Dim HorseTop As Int ' horse image top
Dim HorseLeft As Float ' current left position of the horse image
Dim HorseDelta As Float ' horse move per timer tick
Dim BackgroundLeft As Float ' current left position of the background
Dim BackgroundDelta As Float ' background move per timer tick
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim i As Int
' load the horse images
' first index = 0 for galloping to the right
' first index = 1 for galloping to the left
For i = 0 To ImageNumber - 1
imgHorse(0, i).Initialize(File.DirAssets, "horse0" & i & ".png")
imgHorse(1, i).Initialize(File.DirAssets, "horse1" & i & ".png")
Next
' initialize variables depending on the device orientation
If Activity.Width > Activity.Height Then
HorseDelta = 4dip
HorseHeight = 40%y
TimerInterval = 50
Else
HorseDelta = 2dip
HorseHeight = 25%y
TimerInterval = 80
End If
' initialize the background timer
TimerBackground.Initialize("TimerBackground", TimerInterval)
' initialize the horse timer
' we use two times the background timer interval
TimerHorse.Initialize("TimerHorse", TimerInterval * 2)
' calculate the horse images size and their vertical position
HorseWidth = HorseHeight / imgHorse(0, 0).Height * imgHorse(0, 0).Width
HorseTop = 65%y - HorseHeight / 2
rectHorse.Initialize(0, HorseTop, HorseWidth, HorseTop + HorseHeight)
' initialize the background
imvBackground.Initialize("")
Activity.AddView(imvBackground, 0, 0, 400%y, 100%y)
imvBackground.Gravity = Gravity.FILL
imvBackground.Bitmap = LoadBitmap(File.DirAssets, "Wyoming.jpg")
imvBackground.Left = 0
' calculate BackgroundDelta
' to have the same number of steps as for the horse
i = (100%x - HorseWidth) / HorseDelta
BackgroundDelta = -(imvBackground.Width - 100%x) / 2 / i
' initialize the foreground
imvForeground.Initialize("")
Activity.AddView(imvForeground, 0, 0, 100%x, 100%y)
' initialize the foreground canvas
cvsForeground.Initialize(imvForeground)
' set the foreground to transparent
Dim rect1 As Rect
rect1.Initialize(0, 0, imvForeground.Width, imvForeground.Height)
cvsForeground.DrawRect(rect1, Colors.Transparent, True, 1)
End Sub
Sub Activity_Resume
Activity.Title = "Working with Layers (Animation)"
' initialize the timers
TimerHorse.Enabled = True
TimerBackground.Enabled = True
' set the initial values
HorseLeft = 0
BackgroundLeft = 0
ImageI = 0
ImageDir = 0
' draw the first horse image
DrawHorse(ImageI, 10)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
' stop the timers
TimerHorse.Enabled = True
TimerBackground.Enabled = True
End Sub
Sub TimerHorse_Tick
' increase the horse left position
HorseLeft = HorseLeft + HorseDelta
' test if the horse reaches the right or left border
If HorseLeft >= 100%x - HorseWidth - HorseDelta OR HorseLeft <= 0 Then
BackgroundDelta = - BackgroundDelta
HorseDelta = - HorseDelta
HorseLeft = HorseLeft + HorseDelta
If ImageDir = 0 Then
ImageDir = 1
Else
ImageDir = 0
imvBackground.Left = 0
End If
End If
' update the horse image index
ImageI = ImageI + 1
' reset the image index
If ImageI = ImageNumber Then
ImageI = 0
End If
' draw the new horse image
DrawHorse(ImageI, HorseLeft)
End Sub
Sub TimerBackground_Tick
' set the background left position
BackgroundLeft = BackgroundLeft + BackgroundDelta
imvBackground.Left = BackgroundLeft
End Sub
Sub DrawHorse(i As Int, x As Float)
' drawing routine for the horse image
' erase the current horse image, draw a transparent rectangle
cvsForeground.DrawRect(rectHorse, Colors.Transparent, True, 1)
' set the new horse image position
rectHorse.Left = x
rectHorse.Right = x + HorseWidth
' draw the new horse image
cvsForeground.DrawBitmap(imgHorse(ImageDir, i), Null, rectHorse)
' invalidate (update) the foreground image
imvForeground.Invalidate2(rectHorse)
End Sub