+ All Categories
Home > Documents > Android Tutorial

Android Tutorial

Date post: 28-Dec-2015
Category:
Upload: sitinadiyah
View: 22 times
Download: 0 times
Share this document with a friend
Popular Tags:
24
ANDROID PROGRAMMING Fun Tutorial - 1 Percy Chen March 2014 http://minoyo.com/training/Android/android_tutorial.pdf Android Fun Tutorial - Revision 1.0 1
Transcript
Page 1: Android Tutorial

ANDROID PROGRAMMING !Fun Tutorial - 1"

Percy Chen!March 2014!

http://minoyo.com/training/Android/android_tutorial.pdf!!

Android Fun Tutorial - Revision 1.0" � 1

Page 2: Android Tutorial

ANDROID PROGRAMMING !Fun Tutorial - 1"

Percy Chen !March 2014!

Introduction!Android platform has been regarded as a major player in modern Smart-Phone operating system, it is a semi-mature (At the moment) and an extremely flexible platform which over time have proven itself to be a formidable platform in a lot of different platform.!

"

With a huge community of avid Android programmers, the whole ecosystem has flourished over the years, with millions of mobile apps populating the online Apps marketplace; One of the main catalyst for such success is the ease of development through the Android SDK as well as flexibility when it comes to executing your apps on your target device.!

What to expect...!I have taken the liberty to design this tutorial which is both fun and educational for all of you, upon completion of this tutorial, it will give you a quick review on the topics in Android pro-gramming which we have learnt in the pass 3 days and it will also show you how, even the sim-plest of statement; Played a part in a full working application.!

So I thought, what better way to enhance the learning experience than to write a simple game which all of us can relate to. A classic game which is available in some pre-smartphone era. Snake.!

I will try to make it as easy to follow as possible, guiding you step by step to create this game from scratch. Skills that we will exercise will include.!

- Android (Java) language basic.!

-Main Activity Class. !

- Declarative Layout.!

- Custom View and Graphics.!

Android Fun Tutorial - Revision 1.0" � 2

Page 3: Android Tutorial

-Random number generation.!

- Android Event handling through Bindings and Touch detection.!

Let’s Begin!We will begin by creating a layout xml file using the Relative Layout template. I simply named mine, “snake.xml”. First assigned an id for the parent Relative Layout view group. We will call it “parentRelative”!

android:id="@+id/parentRelative"

Next we add in a Button to start the game and a TextView to display the game store. !

<Button android:id="@+id/start" android:layout_width="70dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="100dp" android:text="Start" />!and!

<TextView android:id="@+id/score" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/start" android:layout_alignBottom="@+id/start" android:layout_alignParentRight="true" android:text="Score:" />!!

Android Fun Tutorial - Revision 1.0" � 3

Page 4: Android Tutorial

My final outcome should looks like the following.Not too fancy at the moment, our main game engine is go-ing to be created programmatically by creating a custom View class.!

!Now you can go ahead to create a new class or reuse the MainActivity created by the SDK’s project template.!

!! If you create your own class, make sure you change the Activity entry in the Manifest file accordingly. !!!

Now we are going to cheat a bit, because I am pretty sure someone of you will missed off some variables if we jumps up and down in the source code. Variable are normally cre-ated as you code your application.!

!Now that we get that out of the way, complete the MainActivity as follows!

We have requested our Activity to be an OnTouchListener, so please remember to add in the implements keyword.!

We also remove the onCreateOptionsMenu functions as we do not need to pop up any menu option in our game. (Unless you wanted it to!!)!

Lastly we have change the layout to point to our own snake.xml (If you name it differently, please change it accordingly)

Android Fun Tutorial - Revision 1.0" � 4

Page 5: Android Tutorial

!After you have type the whole code, you might see a lot of squiggly red line underneath the items that require our attention. Go ahead and take Android’s SDK suggestion to import the appropriate library and for the MainActivity, they would suggest that you add in the unimple-mented methods. Go ahead and accept them all.!

!! Leave the SnakeView class alone at the moment, it is the custom View class that we will cre-ate !!!

You should have the following.!

!!!

Android Fun Tutorial - Revision 1.0" � 5

Page 6: Android Tutorial

Now let’s create our SnakeView class (in the same file within the MainActivity class, but no one is stopping you in creating a separate class if you are adventurous.), this class with extend the View class.!

Using a for loop, we will traverse through an array drawing boxes of 8 by 8 onto the empty game canvas. Feel free to draw the snake in your own color, you could even be creative by cre-ating a rainbow color snake. !

!

Android Fun Tutorial - Revision 1.0" � 6

Page 7: Android Tutorial

Going back into our onCreate class, we can add in the additional code to initialise this view plus the other widgets we have added on our snake.xml layout file.

Android Fun Tutorial - Revision 1.0" � 7

Page 8: Android Tutorial

What we will get now is going to be the basic view of how our snake game will look like.!

Notice the small green snake (Or worm) we have created harbouring on the top left corner?!

Breathing LIFE...!Next, we will need to give life to our “Static” snake which we have created in the previous sec-tion. Animating the snake is simply a matter of drawing the snake in a new position; To ac-complish that, we will need to manipulate the 2 dimensional array which we have created dur-ing declaration. !

We need to add in new procedure in our SnakeView class to add and remove snake links. The reason will become obvious later.!

!!

Android Fun Tutorial - Revision 1.0" � 8

Page 9: Android Tutorial

Next we add in a Timer which in Android is call a Runnable. So that, whenever this Runnable is invoke, it will remove the last link of the snake and add to the front of the snake with a new block govern by variables dx and dy. Redraw the snake and Voila... The snake moved !!!!

The timer will also post a delay back into itself, so this will become a perpetual events, firing every 150ms.!

Lastly we will bind the Start button to start the timer, which in turn generate a new 2 dimen-sional link through the timer.

Android Fun Tutorial - Revision 1.0" � 9

Page 10: Android Tutorial

!Try it now!!!

If everything works out properly. Congratulation, you have one moving snake. !

Help !!!!The snake will start moving after starting the game. But we can’t navigate the snake and it is running off the screen! !

We will need to help the snake move around and using the arrow key and some ways to navi-gate using the touch screen.!

1st if we look at the predefined variable we have created a couple of variable to hold the direc-tion we need to travel. dir as well as old_dir. !

We modified the Runnable to handle all the different directions the snake could possibly move.

Android Fun Tutorial - Revision 1.0" � 10

Page 11: Android Tutorial

And next we will bind the D-Keypad (Emulator has them and so do some android Phone).

Android Fun Tutorial - Revision 1.0" � 11

Page 12: Android Tutorial

Recall that we have touch screen on our phone? and our Activity class has been extended to support Touch event?!

So how about it. Now your snake should be moving to your bidding.!

Collision detection!Our snakes seems to be moving around the canvas happily and stress free. We can’t let that happen now can we? Let’s give our little snake a bit of pressure by penalizing him if he hit the wall or his own body. !

We accomplish the collision detection by checking if the new snake segment (head) is resting on the wall (borders) or any part his own body.!

Android Fun Tutorial - Revision 1.0" � 12

Page 13: Android Tutorial

If it collide with any of the above, we will request the game engine to stop and drop into a game over state.!

In the SnakeView class add in a colision_detection subroutine.It will sense if the snake touches the wall or onto its own body by going through the links with a loop and compare the coordinate.!

And we update our handler to check the collision whenever a link is about to be added.!

Android Fun Tutorial - Revision 1.0" � 13

Page 14: Android Tutorial

Try it by driving the snake into itself or into a wall. Look at the LogCat for the debug message.!

Treat!By now, our little snake should be famish. Let’s reward our little snake for running around for us. What about an apple ?!

!!!!I have prepare apple for our little guy here. Go to your Linux terminal and download the apple bitmap for our little snake!

> minoyo.com/training/Android/apple.bmp!

In order to load the Apple bitmap, we use a BitmapFactory to read the apple.bmp which we have placed in the resource/drawable folder. (line 57 and 58)!

!We can’t make it way too easy for our snake, we will use a random generator to place the apple on the canvas (line 217). Next we will create another subroutine to detect collision with the Apple.!

First at the top of the SnakeView Class add in line 217.!

!Android Fun Tutorial - Revision 1.0" � 14

Page 15: Android Tutorial

Next add in the implementation of the subroutine.!

!! Pay Attention to line 293 above, we request on draw to paint our apple / Treat each redraw !!!

Going back to our binding on the Start button, we will need to keep track of the score as well as to initialise the snack.

Android Fun Tutorial - Revision 1.0" � 15

Page 16: Android Tutorial

Next is in our Runnable, instead of just wall collision, we will need to add in the portion for food collision as well as score updating.!

So what we are doing here is to reward our snake whenever he manages to find an apple. So af-ter eating the apple, we will grow our snake by a single segment (keep a segment, by not firing removeSnake_link). !TODO: You can try to make him run faster after each apple too!!

!

Android Fun Tutorial - Revision 1.0" � 16

Page 17: Android Tutorial

Conclusion!Well our game is pretty much complete, but we can still add a finishing touch by adding a sim-ple toast message when our snake crash.!

!What’s Next ?!We have a fully working Snake game written from scratch in Android, !

TODO: You can try to add a top score list! Good Luck !!!

Android Fun Tutorial - Revision 1.0" � 17

Page 18: Android Tutorial

This is the final outcome.

Android Fun Tutorial - Revision 1.0" � 18

Page 19: Android Tutorial

Appendix - Full Complete Code 1/6!!

!

Android Fun Tutorial - Revision 1.0" � 19

Page 20: Android Tutorial

Appendix - Full Complete Code 2/6!!

Android Fun Tutorial - Revision 1.0" � 20

Page 21: Android Tutorial

Appendix - Full Complete Code 3/6

Android Fun Tutorial - Revision 1.0" � 21

Page 22: Android Tutorial

Appendix - Full Complete Code 4/6!!

!

Android Fun Tutorial - Revision 1.0" � 22

Page 23: Android Tutorial

Appendix - Full Complete Code 5/6!!

!

Android Fun Tutorial - Revision 1.0" � 23

Page 24: Android Tutorial

Appendix - Full Complete Code 6/6

Android Fun Tutorial - Revision 1.0" � 24


Recommended