• We just launched and are currently in beta. Join us as we build and grow the community.

Android Working with Layers Animation using Basic4Android - Tutorial Part 2

IconicJay

Dungeon Explorer
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
83
Likes
179
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. Dim

    TimerHorse As

    Timer
  3. Dim

    TimerBackground As

    Timer
  4. Dim

    TimerInterval As

    Long
  5. End

    Sub

  6. Sub

    Globals
  7. Dim

    ImageI As

    Int ' index of the current horse image
  8. Dim

    ImageDir As

    Int ' direction of the horse image
  9. Dim

    ImageNumber As

    Int ' number of horse images
  10. ImageNumber = 8
  11. Dim

    imgHorse(2, ImageNumber) As

    Bitmap ' array horse image bitmaps
  12. Dim

    imvBackground As

    ImageView ' background ImageView
  13. Dim

    imvForeground As

    ImageView ' foreground ImageView
  14. Dim

    cvsForeground As

    Canvas ' canvas for the foreground image
  15. Dim

    rectHorse As

    Rect ' rectangle of the horse image
  16. Dim

    HorseWidth As

    Int ' horse image width
  17. Dim

    HorseHeight As

    Int ' horse image height
  18. Dim

    HorseTop As

    Int ' horse image top
  19. Dim

    HorseLeft As

    Float ' current left position of the horse image
  20. Dim

    HorseDelta As

    Float ' horse move per timer tick
  21. Dim

    BackgroundLeft As

    Float ' current left position of the background
  22. Dim

    BackgroundDelta As

    Float ' background move per timer tick
  23. End

    Sub

  24. Sub

    Activity_Create(FirstTime As

    Boolean

    )
  25. Dim

    i As

    Int

  26. ' load the horse images
  27. ' first index = 0 for galloping to the right
  28. ' first index = 1 for galloping to the left
  29. For

    i = 0 To

    ImageNumber - 1
  30. imgHorse(0, i).Initialize(File.DirAssets, "horse0"

    & i & ".png"

    )
  31. imgHorse(1, i).Initialize(File.DirAssets, "horse1"

    & i & ".png"

    )
  32. Next

  33. ' initialize variables depending on the device orientation
  34. If

    Activity.Width > Activity.Height Then
  35. HorseDelta = 4dip
  36. HorseHeight = 40%y
  37. TimerInterval = 50
  38. Else
  39. HorseDelta = 2dip
  40. HorseHeight = 25%y
  41. TimerInterval = 80
  42. End

    If

  43. ' initialize the background timer
  44. TimerBackground.Initialize("TimerBackground"

    , TimerInterval)

  45. ' initialize the horse timer
  46. ' we use two times the background timer interval
  47. TimerHorse.Initialize("TimerHorse"

    , TimerInterval * 2)

  48. ' calculate the horse images size and their vertical position
  49. HorseWidth = HorseHeight / imgHorse(0, 0).Height * imgHorse(0, 0).Width
  50. HorseTop = 65%y - HorseHeight / 2
  51. rectHorse.Initialize(0, HorseTop, HorseWidth, HorseTop + HorseHeight)

  52. ' initialize the background
  53. imvBackground.Initialize(""

    )
  54. Activity.AddView(imvBackground, 0, 0, 400%y, 100%y)
  55. imvBackground.Gravity = Gravity.FILL
  56. imvBackground.Bitmap = LoadBitmap(File.DirAssets, "Wyoming.jpg"

    )
  57. imvBackground.Left = 0

  58. ' calculate BackgroundDelta
  59. ' to have the same number of steps as for the horse
  60. i = (100%x - HorseWidth) / HorseDelta
  61. BackgroundDelta = -(imvBackground.Width - 100%x) / 2 / i

  62. ' initialize the foreground
  63. imvForeground.Initialize(""

    )
  64. Activity.AddView(imvForeground, 0, 0, 100%x, 100%y)

  65. ' initialize the foreground canvas
  66. cvsForeground.Initialize(imvForeground)

  67. ' set the foreground to transparent
  68. Dim

    rect1 As

    Rect
  69. rect1.Initialize(0, 0, imvForeground.Width, imvForeground.Height)
  70. cvsForeground.DrawRect(rect1, Colors.Transparent, True

    , 1)
  71. End

    Sub

  72. Sub

    Activity_Resume
  73. Activity.Title = "Working with Layers (Animation)"

  74. ' initialize the timers
  75. TimerHorse.Enabled = True
  76. TimerBackground.Enabled = True

  77. ' set the initial values
  78. HorseLeft = 0
  79. BackgroundLeft = 0
  80. ImageI = 0
  81. ImageDir = 0

  82. ' draw the first horse image
  83. DrawHorse(ImageI, 10)
  84. End

    Sub

  85. Sub

    Activity_Pause (UserClosed As

    Boolean

    )
  86. ' stop the timers
  87. TimerHorse.Enabled = True
  88. TimerBackground.Enabled = True
  89. End

    Sub

  90. Sub

    TimerHorse_Tick
  91. ' increase the horse left position
  92. HorseLeft = HorseLeft + HorseDelta

  93. ' test if the horse reaches the right or left border
  94. If

    HorseLeft >= 100%x - HorseWidth - HorseDelta OR

    HorseLeft <= 0 Then
  95. BackgroundDelta = - BackgroundDelta
  96. HorseDelta = - HorseDelta
  97. HorseLeft = HorseLeft + HorseDelta
  98. If

    ImageDir = 0 Then
  99. ImageDir = 1
  100. Else
  101. ImageDir = 0
  102. imvBackground.Left = 0
  103. End

    If
  104. End

    If

  105. ' update the horse image index
  106. ImageI = ImageI + 1
  107. ' reset the image index
  108. If

    ImageI = ImageNumber Then
  109. ImageI = 0
  110. End

    If

  111. ' draw the new horse image
  112. DrawHorse(ImageI, HorseLeft)
  113. End

    Sub

  114. Sub

    TimerBackground_Tick
  115. ' set the background left position
  116. BackgroundLeft = BackgroundLeft + BackgroundDelta
  117. imvBackground.Left = BackgroundLeft
  118. End

    Sub

  119. Sub

    DrawHorse(i As

    Int, x As

    Float)
  120. ' drawing routine for the horse image

  121. ' erase the current horse image, draw a transparent rectangle
  122. cvsForeground.DrawRect(rectHorse, Colors.Transparent, True

    , 1)

  123. ' set the new horse image position
  124. rectHorse.Left = x
  125. rectHorse.Right = x + HorseWidth

  126. ' draw the new horse image
  127. cvsForeground.DrawBitmap(imgHorse(ImageDir, i), Null

    , rectHorse)

  128. ' invalidate (update) the foreground image
  129. imvForeground.Invalidate2(rectHorse)
  130. 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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top