First Android application

droid

Well since I now posses a Samsung Galaxy S2 phone, its only fair that I expand my horizon and come up with some nice little fun app to begin with.

One of my favourite places to surf and learn computer animation is a nice site called dhteumeuleu, which is authored by ge1doot. This place has amazing JavaScript examples and various demos and here I shamelessly copy or rather port one of his creations into android. Its called No Click ! and hence I shall call it the same although I personally wanted to call it as “chaser”.

The principles of animation in a droid app basically remains the same, there is a input step, then an update state step and finally a render step, i.e We handle input (touch events), update the state (update the sprite position) of our internal objects and render the current state.

There is a concept of activity in a droid app and every thing happens inside it, an activity creates a view, touch events are passed into a view and then rest of the magic happens.

I have four classes three of them form a support infra and then finally a Sprite model where all the cool stuff goes and you will notice that this is a prevalent pattern in the droid world.

I have a class called MainPanel which extends the SurfaceView, this is where I initialise my app and start the main animation thread which is the instance of a class called MainThread that in turn inherits form the Thread class, all of this so far is simply support abstractions to build code structure, after this there is a third class called ScrollActivity which extends Activity and its here where the Sprite class called Chaser is initialised ., a bitmap is set from the resource and then an animation loop simply does some hacky positioning of the sprite based on touch events.

One of the different things (coming from a mouse driven environment ) is how a mouse drag equivalent is captured in touch screen devices here, an ACTION_DOWN event is equivalent to mouse down, an ACTION_UP is equivalent of mouse up and ACTION_MOVE is equivalent of mouse move, since I was interested in capturing a "Mouse Drag" equivalent I kinda do something like given below

Snippet
           
	     
        public boolean onTouchEvent(MotionEvent event) {
        	super.onTouchEvent(event);
       		 switch (event.getAction()) {
            		 case MotionEvent.ACTION_DOWN: {
               		 down = true;
                	 chaser.setXm(event.getX());
                	 chaser.setYm(event.getY());
                	 break;
            		 }
           		 case MotionEvent.ACTION_UP: {
                	 down = false;
               		 break;
            		 }
            		case MotionEvent.ACTION_MOVE: {
               		 if (down) {
                   	 chaser.setXm(event.getX());
                   	 chaser.setYm(event.getY());
                    	chaser.setStart(true);
                	 }
                	break;
                     }
        	}
        	return true;
    	   } 

        
One of the turn down is a bug in the framework, which on exiting i.e on surfaceDestroyed sometimes throws an exception so for now I simply use  hack I found somewhere
Snippet
           
 
 public void surfaceDestroyed(SurfaceHolder holder) {
	// tell the thread to shut down and wait for it to finish
	// this is a clean shutdown
	boolean retry = true;
	while (retry) {
		try {
			thread.join();
			retry = false;
		} catch (InterruptedException e) {
			// try again shutting down the thread
		}
	}
}

     
and that's it folks.
Tagged  | Comments