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":
Using java Syntax Highlighting
- 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();
- }
- }
- }
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
"src/your_package_structure/BounceView.java":
Using java Syntax Highlighting
- 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);
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4







