andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

A Bouncing Ball - Basic Graphics Animation Demo


 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
martin_nuke
Junior Developer


Joined: 08 Dec 2007
Posts: 12

PostPosted: Sat Dec 15, 2007 6:20 am    Post subject: A Bouncing Ball - Basic Graphics Animation Demo Reply with quote

A Bouncing Ball - Bacic Graphics Animation Demo


What is this: This is a Basic Graphics Animation demo which uses a Thread and Canvas to display and animate a Drawable.

What it will look like:


Description:
There are two files the BounceActivity.java and the BounceView.java.

"src/your_package_structure/BounceActivity.java":
Java:
package org.anddev.android.bouncingball;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.os.Handler;
import android.os.Message;

public class BounceActivity extends Activity {

     // Just a RANDOM ID to recognize a Message later
     protected static final int GUIUPDATEIDENTIFIER = 0x101;

     Thread myRefreshThread = null;

     /* Our 'ball' is located within this View */
     BounceView myBounceView = null;

     Handler myGUIUpdateHandler = new Handler() {

          // @Override
          public void handleMessage(Message msg) {
               switch (msg.what) {
                    case BounceActivity.GUIUPDATEIDENTIFIER:
                         /* Repaint the BounceView
                          * (where the ball is in) */

                         myBounceView.invalidate();
                         break;
               }
               super.handleMessage(msg);
          }
     };

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          // Set fullscreen
          this.requestWindowFeature(Window.FEATURE_NO_TITLE);

          // Create a
          this.myBounceView = new BounceView(this);
          this.setContentView(this.myBounceView);

          /* create a Thread that will
           * periodically send messages
           * to our Handler */

          new Thread(new RefreshRunner()).start();
     }

     class RefreshRunner implements Runnable {
          // @Override
          public void run() {
               while (!Thread.currentThread().isInterrupted()) {
                    // Send Message to the Handler which will call the invalidate() method of the BOucneView
                    Message message = new Message();
                    message.what = BounceActivity.GUIUPDATEIDENTIFIER;
                    BounceActivity.this.myGUIUpdateHandler.sendMessage(message);

                    try {
                         Thread.sleep(100); // a 10th of a second
                    } catch (InterruptedException e) {
                         Thread.currentThread().interrupt();
                    }
               }
          }
     }
}


"src/your_package_structure/BounceView.java":
Java:
package org.anddev.android.bouncingball;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.View;

public class BounceView extends View {

     /* Our Ball together with the location it will be painted*/
     protected Drawable mySprite;
     protected Point mySpritePos = new Point(0,0);
     
     /* Working with a Enum is 10000%
      * safer than working with int's
      * to 'remember' the direction. */

     protected enum HorizontalDirection {LEFT, RIGHT}
     protected enum VerticalDirection {UP, DOWN}
     protected HorizontalDirection myXDirection = HorizontalDirection.RIGHT;
     protected VerticalDirection myYDirection = VerticalDirection.UP;


     public BounceView(Context context) {
          super(context);
          // Set the background
          this.setBackground(this.getResources().getDrawable(R.drawable.icon));
          // Load our "Ball"
          this.mySprite = this.getResources().getDrawable(R.drawable.icon);
     }

     @Override
     protected void onDraw(Canvas canvas) {
          /* Check if the Ball started to leave
           * the screen on left or right side */

          if (mySpritePos.x >= this.getWidth() - mySprite.getBounds().width()) {
               this.myXDirection = HorizontalDirection.LEFT;
          } else if (mySpritePos.x <= 0) {
               this.myXDirection = HorizontalDirection.RIGHT;
          }

          /* Check if the Ball started to leave
           * the screen on bottom or upper side */

          if (mySpritePos.y >= this.getHeight() - mySprite.getBounds().height()) {
               this.myYDirection = VerticalDirection.UP;
          } else if (mySpritePos.y <= 0) {
               this.myYDirection = VerticalDirection.DOWN;
          }

          /* Move the ball left or right */
          if (this.myXDirection == HorizontalDirection.RIGHT) {
               this.mySpritePos.x += 10;
          } else {
               this.mySpritePos.x -= 10;
          }
          /* Move the ball up or down */
          if (this.myYDirection == VerticalDirection.DOWN) {
               this.mySpritePos.y += 10;
          } else {
               this.mySpritePos.y -= 10;
          }

          /* Set the location, where the sprite
           * will draw itself to the canvas */

          this.mySprite.setBounds(this.mySpritePos.x, this.mySpritePos.y,
                    this.mySpritePos.x + 50, this.mySpritePos.y + 50);
          
          /* Make the sprite draw itself to the canvas */
          this.mySprite.draw(canvas);
     }
}


Last edited by martin_nuke on Fri Feb 22, 2008 6:56 am; edited 1 time in total
Back to top
View user's profile Send private message Yahoo Messenger
plusminus
Site Admin


Joined: 14 Nov 2007
Posts: 2067
Location: Germany

PostPosted: Sat Dec 15, 2007 3:42 pm    Post subject: Reply with quote

Thx martin_nuke for posting that tutorial Smile

I allowed myself to add some comments and some minor code-changes for improved understanding.

Best Regards,
plusminus

_________________

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
rock_win
Junior Developer


Joined: 21 Jan 2008
Posts: 10

PostPosted: Mon Jan 21, 2008 4:26 pm    Post subject: Reply with quote

What is the basic difference between sending a message to the handler and posting to a handler.

I'm unable to run my animation with the above code, however when i post it , it works fine.
Back to top
View user's profile Send private message
plusminus
Site Admin


Joined: 14 Nov 2007
Posts: 2067
Location: Germany

PostPosted: Mon Jan 21, 2008 4:52 pm    Post subject: Reply with quote

Hello rock_win,

you mean the difference between sendMessage(Message) and post(Runnable) Question

Besides the parameters I would say none!
Quote:
post(Runnable)
Causes the Runnable r to be added to the message queue. (The runnable will be run on the thread to which this handler is attached.)

vs.
Quote:
sendMessage(Message)
Pushes a message onto the end of the message queue after all pending messages before the current time. It will be received in handleMessage(Message), in the thread attached to this handler.


Regards,
plusminus

_________________

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.