Android Working with Layers Animation using Basic4Android - Tutorial Part1

In this tutorial, we will create an animation that can move an image character. This images will move but with contents to the layers of image an also for our background image. First you will need this images: 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 And here's our image for our background: background First we will initialized our Sub Global code. Take note that i provided my explanation using comments (the text in the Green color.)Type the code below:
  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
Next we will initialize an image that draws a horse. Follow the code below.
  1. Sub DrawHorse(i As Int, x As Float)
  2.         ' drawing routine for the horse image
  3.        
  4.         ' erase the current horse image, draw a transparent rectangle
  5.         cvsForeground.DrawRect(rectHorse, Colors.Transparent, True, 1)
  6.        
  7.         ' set the new horse image position
  8.         rectHorse.Left = x
  9.         rectHorse.Right = x + HorseWidth
  10.                 ' draw the new horse image
  11.         cvsForeground.DrawBitmap(imgHorse(ImageDir, i), Null, rectHorse)
  12.        
  13.         ' invalidate (update) the foreground image
  14.         imvForeground.Invalidate2(rectHorse)
  15. End Sub
Then to move the background using Timer. Type the code below.
  1. Sub TimerBackground_Tick
  2.         ' set the background left position
  3.         BackgroundLeft = BackgroundLeft + BackgroundDelta
  4.         imvBackground.Left = BackgroundLeft
  5. End Sub
Next the timer for our horse to move. Follow the code below.
  1. Sub TimerHorse_Tick
  2.         ' increase the horse left position
  3.         HorseLeft = HorseLeft + HorseDelta
  4.        
  5.         ' test if the horse reaches the right or left border
  6.         If HorseLeft >= 100%x - HorseWidth - HorseDelta OR HorseLeft <= 0 Then
  7.                 BackgroundDelta = - BackgroundDelta
  8.                 HorseDelta = - HorseDelta
  9.                 HorseLeft = HorseLeft + HorseDelta
  10.                 If ImageDir = 0 Then
  11.                         ImageDir = 1
  12.                 Else
  13.                         ImageDir = 0
  14.                         imvBackground.Left = 0
  15.                 End If
  16.         End If
  17.        
  18.         ' update the horse image index
  19.         ImageI = ImageI + 1
  20.         ' reset the image index
  21.         If ImageI = ImageNumber Then
  22.                 ImageI = 0
  23.         End If
  24.        
  25.         ' draw the new horse image
  26.         DrawHorse(ImageI, HorseLeft)
  27. End Sub
Take note that all of my explanation of the coding are in comments with a color Green text. That's it for now and we will proceed to our second part which is the main sub procedure of Activity_Create. 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