Thursday, December 22, 2011

Android Animation App - Bouncing Ball

So looking for play with animation stuff.. good!! you are on right place  :)
today we are going to learn how to create simple android animation app.
so in this application we will create Background View and Ball programatically which will jump.

let's began the code, first of all we need to create simple android app having single Activity
name is :
BouncingBallActivity.java


package com.mytest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class BouncingBallActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);

View bouncingBallView = new BouncingBallView(this);
setContentView(bouncingBallView);
}
}

Now we need to create a View programatically [we will not using any xml file]
here we go..
BouncingBallView.java
  
package com.mytest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.KeyEvent;
import android.view.View;

public class BouncingBallView extends View {
private int xMin = 0; // This view's bounds
private int xMax;
private int yMin = 0;
private int yMax;
private float ballRadius = 40; // Ball's radius
private float ballX = ballRadius + 20; // Ball's center (x,y)
private float ballY = ballRadius + 40;
private float ballSpeedX = 5; // Ball's speed (x,y)
private float ballSpeedY = 3;
private RectF ballBounds; // Needed for Canvas.drawOval
private Paint paint; // The paint (e.g. style, color) used for drawing

// Constructor
public BouncingBallView(Context context) {
super(context);
ballBounds = new RectF();
paint = new Paint();

//to enable keypad
this.setFocusable(true);
this.requestFocus();
}

// Called back to draw the view. Also called by invalidate().
@Override
protected void onDraw(Canvas canvas) {
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
canvas.drawOval(ballBounds, paint);

// Update the position of the ball, including collision detection and reaction.
update();

// Delay
try {
Thread.sleep(60);
} catch (InterruptedException e) { }

invalidate(); // Force a re-draw
}

// Detect collision and update the position of the ball.
private void update() {
// Get new (x,y) position
// ballX += ballSpeedX;
ballY += ballSpeedY;
// Detect collision and react
if (ballX + ballRadius > xMax) {
ballSpeedX = -ballSpeedX;
ballX = xMax-ballRadius;
} else if (ballX - ballRadius < xMin) {
ballSpeedX = -ballSpeedX;
ballX = xMin+ballRadius;
}
if (ballY + ballRadius > yMax) {
ballSpeedY = -ballSpeedY;
ballY = yMax - ballRadius;
} else if (ballY - ballRadius < yMin) {
ballSpeedY = -ballSpeedY;
ballY = yMin + ballRadius;
}
}

// Called back when the view is first created or its size changes.
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
// Set the movement bounds for the ball
xMax = w-1;
yMax = h-1;
}

// key-up event handler
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_RIGHT: // Increase rightward speed
ballSpeedX++;
break;
case KeyEvent.KEYCODE_DPAD_LEFT: // Increase leftward speed
ballSpeedX--;
break;
case KeyEvent.KEYCODE_DPAD_UP: // Increase upward speed
ballSpeedY--;
break;
case KeyEvent.KEYCODE_DPAD_DOWN: // Increase downward speed
ballSpeedY++;
break;
case KeyEvent.KEYCODE_DPAD_CENTER: // Stop
ballSpeedX = 0;
ballSpeedY = 0;
break;
case KeyEvent.KEYCODE_A: // Zoom in
// Max radius is about 90% of half of the smaller dimension
float maxRadius = (xMax > yMax) ? yMax / 2 * 0.9f : xMax / 2 * 0.9f;
if (ballRadius < maxRadius) {
ballRadius *= 1.05; // Increase radius by 5%
}
break;
case KeyEvent.KEYCODE_Z: // Zoom out
if (ballRadius > 20) { // Minimum radius
ballRadius *= 0.95; // Decrease radius by 5%
}
break;
}
return true; // Event handled
}


}
at last our manifest file should look like this..
AndroidManifest.xml
  















output is like this

we have done great job on ma B'day!!   :D

cheers!!

I'd love to here your thoughts!

Monday, December 12, 2011

AlertBox in Android (Advance)

This is also simple Tutorial for Android Alert Box Dialog.

Few things we can do with this that cann't with Basic Tutorial like

  • We are adding the alert icon to Dialog box.
  • It shows the alert message as Basic does.
  • We can make decision whether cancel alert message or do some task

okay! so let's try this small app

 -------------------------------------------
App Name: AlertBoxAdvance
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: AlertBoxAdvance
-------------------------------------------

project structure is look like this


Note: you need to save below alert icon and put into drawable folder


AlertBoxAdvance.java
package com.rdc;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class AlertBoxAdvance extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//create advance Alert dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit this App?")
.setCancelable(false)
.setTitle("AlertBox")
.setIcon(R.drawable.alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//finish the current activity
AlertBoxAdvance.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//cancel the dialog box
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

}
}

main.xml



AndroidManifest.xml














The output Screen will be like this..

You can download the complete source code zip file here : AlertBoxAdvance

cheers!!

I'd love to hear your thoughts!

Labels: , ,

Monday, December 5, 2011

AlertBox in Android (Basic)

This is the simple and very basic Tutorial for Android Alert Box Dialog.
Just create alert box and you can call it whenever you want in app

so let's try this small app

 -------------------------------------------
App Name: AlertBoxBasic
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: AlertBoxActivity
-------------------------------------------

 AlertBoxActivity.java

package com.rdc;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class AlertBoxActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//call alert box code to display dialog
alertbox("Alert Box","Your Application has been started..");
}

//create alert dialog box
protected void alertbox(String title, String mymessage)
{
new AlertDialog.Builder(this)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){}
})
.show();
}
}

main.xml



AndroidManifest.xml














The output Screen will be like this..

You can download the complete source code zip file here : AlertBoxBasic

cheers!!

 I'd love to hear your thoughts!

Labels: , , ,