Android Working with Layers Animation using Basic4Android - Tutorial Part 2

Good day! This is my continuation of my tutorial in working with layer animation in Android using Basic4Android. The last time i created all the necessary procedures in part1 but for now I'll give you the complete code for this. Here is the complete code for this tutorial.
  1. Sub Process_Globals
  2.  
  3. Dim TimerHorse As Timer
  4. Dim TimerBackground As Timer
  5. Dim TimerInterval As Long
  6. End Sub
  7.  
  8. Sub Globals
  9. Dim ImageI As Int ' index of the current horse image
  10. Dim ImageDir As Int ' direction of the horse image
  11. Dim ImageNumber As Int ' number of horse images
  12. ImageNumber = 8
  13. Dim imgHorse(2, ImageNumber) As Bitmap ' array horse image bitmaps
  14. Dim imvBackground As ImageView ' background ImageView
  15. Dim imvForeground As ImageView ' foreground ImageView
  16. Dim cvsForeground As Canvas ' canvas for the foreground image
  17. Dim rectHorse As Rect ' rectangle of the horse image
  18. Dim HorseWidth As Int ' horse image width
  19. Dim HorseHeight As Int ' horse image height
  20. Dim HorseTop As Int ' horse image top
  21. Dim HorseLeft As Float ' current left position of the horse image
  22. Dim HorseDelta As Float ' horse move per timer tick
  23. Dim BackgroundLeft As Float ' current left position of the background
  24. Dim BackgroundDelta As Float ' background move per timer tick
  25. End Sub
  26.  
  27. Sub Activity_Create(FirstTime As Boolean)
  28. Dim i As Int
  29.  
  30. ' load the horse images
  31. ' first index = 0 for galloping to the right
  32. ' first index = 1 for galloping to the left
  33. For i = 0 To ImageNumber - 1
  34. imgHorse(0, i).Initialize(File.DirAssets, "horse0" & i & ".png")
  35. imgHorse(1, i).Initialize(File.DirAssets, "horse1" & i & ".png")
  36. Next
  37.  
  38. ' initialize variables depending on the device orientation
  39. If Activity.Width > Activity.Height Then
  40. HorseDelta = 4dip
  41. HorseHeight = 40%y
  42. TimerInterval = 50
  43. Else
  44. HorseDelta = 2dip
  45. HorseHeight = 25%y
  46. TimerInterval = 80
  47. End If
  48.  
  49. ' initialize the background timer
  50. TimerBackground.Initialize("TimerBackground", TimerInterval)
  51.  
  52. ' initialize the horse timer
  53. ' we use two times the background timer interval
  54. TimerHorse.Initialize("TimerHorse", TimerInterval * 2)
  55.  
  56. ' calculate the horse images size and their vertical position
  57. HorseWidth = HorseHeight / imgHorse(0, 0).Height * imgHorse(0, 0).Width
  58. HorseTop = 65%y - HorseHeight / 2
  59. rectHorse.Initialize(0, HorseTop, HorseWidth, HorseTop + HorseHeight)
  60.  
  61. ' initialize the background
  62. imvBackground.Initialize("")
  63. Activity.AddView(imvBackground, 0, 0, 400%y, 100%y)
  64. imvBackground.Gravity = Gravity.FILL
  65. imvBackground.Bitmap = LoadBitmap(File.DirAssets, "Wyoming.jpg")
  66. imvBackground.Left = 0
  67.  
  68. ' calculate BackgroundDelta
  69. ' to have the same number of steps as for the horse
  70. i = (100%x - HorseWidth) / HorseDelta
  71. BackgroundDelta = -(imvBackground.Width - 100%x) / 2 / i
  72.  
  73. ' initialize the foreground
  74. imvForeground.Initialize("")
  75. Activity.AddView(imvForeground, 0, 0, 100%x, 100%y)
  76.  
  77. ' initialize the foreground canvas
  78. cvsForeground.Initialize(imvForeground)
  79.  
  80. ' set the foreground to transparent
  81. Dim rect1 As Rect
  82. rect1.Initialize(0, 0, imvForeground.Width, imvForeground.Height)
  83. cvsForeground.DrawRect(rect1, Colors.Transparent, True, 1)
  84. End Sub
  85.  
  86. Sub Activity_Resume
  87. Activity.Title = "Working with Layers (Animation)"
  88.  
  89. ' initialize the timers
  90. TimerHorse.Enabled = True
  91. TimerBackground.Enabled = True
  92.  
  93. ' set the initial values
  94. HorseLeft = 0
  95. BackgroundLeft = 0
  96. ImageI = 0
  97. ImageDir = 0
  98.  
  99. ' draw the first horse image
  100. DrawHorse(ImageI, 10)
  101. End Sub
  102.  
  103. Sub Activity_Pause (UserClosed As Boolean)
  104. ' stop the timers
  105. TimerHorse.Enabled = True
  106. TimerBackground.Enabled = True
  107. End Sub
  108.  
  109. Sub TimerHorse_Tick
  110. ' increase the horse left position
  111. HorseLeft = HorseLeft + HorseDelta
  112.  
  113. ' test if the horse reaches the right or left border
  114. If HorseLeft >= 100%x - HorseWidth - HorseDelta OR HorseLeft <= 0 Then
  115. BackgroundDelta = - BackgroundDelta
  116. HorseDelta = - HorseDelta
  117. HorseLeft = HorseLeft + HorseDelta
  118. If ImageDir = 0 Then
  119. ImageDir = 1
  120. Else
  121. ImageDir = 0
  122. imvBackground.Left = 0
  123. End If
  124. End If
  125.  
  126. ' update the horse image index
  127. ImageI = ImageI + 1
  128. ' reset the image index
  129. If ImageI = ImageNumber Then
  130. ImageI = 0
  131. End If
  132.  
  133. ' draw the new horse image
  134. DrawHorse(ImageI, HorseLeft)
  135. End Sub
  136.  
  137. Sub TimerBackground_Tick
  138. ' set the background left position
  139. BackgroundLeft = BackgroundLeft + BackgroundDelta
  140. imvBackground.Left = BackgroundLeft
  141. End Sub
  142.  
  143. Sub DrawHorse(i As Int, x As Float)
  144. ' drawing routine for the horse image
  145.  
  146. ' erase the current horse image, draw a transparent rectangle
  147. cvsForeground.DrawRect(rectHorse, Colors.Transparent, True, 1)
  148.  
  149. ' set the new horse image position
  150. rectHorse.Left = x
  151. rectHorse.Right = x + HorseWidth
  152.  
  153. ' draw the new horse image
  154. cvsForeground.DrawBitmap(imgHorse(ImageDir, i), Null, rectHorse)
  155.  
  156. ' invalidate (update) the foreground image
  157. imvForeground.Invalidate2(rectHorse)
  158. End Sub
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment