Programming/Non programming

Tuesday, March 30, 2010

Android:Basic Animation samples

Android:

Basic Animation handling..:

To create basic animations like fade-in, fade-out, scale animation(like zooming..) etc. follow the steps as described below:

1. Create your project as usual.

2. Create a folder called "anim" under "res" folder. It will look like "res/anim"

3. Under "anim" folder, create a empty XML file for animation in the name as you wish. I named that as "fade.xml"

Add and test the following code into that XML file.

*** For "FADE-IN" animation: //in fade.xml




Call it in your code as below for sample:

public void HandleAnimation(View v)
{
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade)); //xml name fade.xml
}

==============================================================================================

*** For image "SCALING" animation: //in fade.xml




Call it in your code as below for sample:

public void HandleAnimation(View v)
{
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade));//xml name fade.xml
}

==============================================================================================

*** For image "SLIDE FROM LEFT and RIGHT" animation: //in fade.xml

For Push LEFT IN:



For Push LEFT OUT:




Call it in your code as below for sample:

public void HandleAnimation(View v)
{
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade));//xml name fade.xml
}

==============================================================================================

*** For "FADE AND AWAY" animation: //in fade.xml



Call it in your code as below for sample:

public void HandleAnimation(View v)
{
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade));//xml name fade.xml
}

==============================================================================================

*** For "SHAKE IMAGE" animation:

Add Two .xml files for this. First name it as anything as you wish, add the code below, fade.xml:




Add the second file with the below code and name it as "cycle_7.xml" ,

cycle_7.xml:




Call it like below in your code anywhere:

public void HandleAnimation(View v)
{
v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade));//xml name fade.xml
}

You will find shake animation is happening nicely there !!!!!!!!!!!!

==============================================================================================


Cheers!

M.P.Prabakar
Senior Systems Analyst.



Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

Monday, March 29, 2010

Android: Basic study

Android:

Introduction, installation and basic starter guide can be learnt from the following link, which i have found very good links for Android learners:

http://developerlife.com/tutorials/?p=300

http://www.helloandroid.com

Cheers!

M.P.Prabakar
Senior Systems Analyst


Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

iPhone:Hide Status bar

iPhone:

How to hide status bar from the view on iPhone development?

Add the below code in your AppDelegate "applicationDidFinishLaunching" method.


[[UIApplication sharedApplication] setStatusBarHidden:YES];


Cheers!

M.P.Prabakar
Senior Systems Analyst


Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

Android: Play PCM(.WAV) audio buffer using AudioTrack

Android:

Play Audio sound .PCM(.WAV) buffer using AudioTrack class

The following is the code samples shows this task:

1. First way:

Play the sound directly without reading file with some amount of buffer by buffer. It is
useful to play short sounds.

private void PlayShortAudioFileViaAudioTrack(String filePath) throws IOException
{
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath==null)
return;

//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath); // for ex. path= "/sdcard/samplesound.pcm" or "/sdcard/samplesound.wav"
byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Set and push to audio track..
int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
if (at!=null) {
at.play();
// Write the byte array to the track
at.write(byteData, 0, byteData.length);
at.stop();
at.release();
}
else
Log.d("TCAudio", "audio track is not initialised ");

}


2. Second way:

Play the sound by reading file with some amount of buffer by buffer. It is useful to play
bigger size sounds.
Reading by 512 kb buffer and writing into track.

private void PlayAudioFileViaAudioTrack(String filePath) throws IOException
{
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath==null)
return;

int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);

AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);


if (at==null){
Log.d("TCAudio", "audio track is not initialised ");
return;
}

int count = 512 * 1024; // 512 kb
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath);

byteData = new byte[(int)count];
FileInputStream in = null;
try {
in = new FileInputStream( file );

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int bytesread = 0, ret = 0;
int size = (int) file.length();
at.play();
while (bytesread < size) { ret = in.read( byteData,0, count); if (ret != -1) { // Write the byte array to the track at.write(byteData,0, ret); bytesread += ret; } else break; } in.close(); at.stop(); at.release(); }

Thank you followers !

Cheers,
M.P.Prabakar
Senior Systems Analyst

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

iPhone:Types of Touch events

iPhone:

The following are the touch events are available on iPhone development

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}

Thank you for followers!

Cheers,

M.P.Prabakar
Senior Systems Analyst


Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

iPhone:Touch event for particular UI controls interaction:

iPhone:

Handling some process under Touch event during when particular UI control interaction:

Whenever we want to do some process under touch event, we can follow that under
"- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event"

But when we have a UIControl like UILabels, Imagebuttons etc. and we want to do some process when Touch events occurred on that particular UIControls, following the below process to do it.

1. Create a UIControl. For example: UILabel

2. Link UILabel with declaration property in code. Make sure UserInteraction checkbox is enabled for UILabel in I.B

3. Add the below code in your code.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:urlLabel];
if( [urlLabel hitTest:point withEvent:nil] )
[ [UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.mywebsite.com"] ];
else
[super touchesEnded:touches withEvent:event];
}

What it does is, whenever touch event is happening on the UILabel, it will launch the URL website given.

You got to go ... and be success !!!!!!!

Thank you.

Cheers!
M.P.Prabakar
Senior Systems Analyst

Sunday, March 28, 2010

iPhone:UIWebview to load URL:

iPhone:

How load URL in UIWebView

1. Create a UIWebView in I.B and link that with UIWebView declaration in code.
2. Call the below code in viewDidLoad (or) viewWillAppear

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]] ];

When you run this screen, it will pickup the URL and show in Webview.

Thank you for followers!

Cheers,

M.P.Prabakar
Senior Systems Analyst.


Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

iPhone:Adding URL to the text?

iPhone:

How to add URL link to the text:

There are two ways to do that.

1. Via under button controls:

i) Create a button in view. Make the button as 'custom' so that you will not see button borders at runtime and will look like just a text.
ii) Create a button action like below:

-(IBAction) LaunchURL : (id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:/www.corpusmobilelabs.com/"]];
}

Click on the button, you should be success!

2. Via under TouchEvent on a control:

i) Create a UILabel and write text like your URL www.yourwebsite.com
ii) Make sure UILabel linked with UILabel declaration in code and UserInteraction checkbox is
enabled in I.B.
iii) Add the below touch event code in your code, which will execute the launching URL only when the created UILabel is touched.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:urlLabel];
if( [urlLabel hitTest:point withEvent:nil] )
[ [UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www. yourwebsite.com"] ];
else
[super touchesEnded:touches withEvent:event];
}

That's it, you got to go!!!

Thank you for followers!

Cheers,

M.P.Prabakar
Senior Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

Tuesday, March 23, 2010

Android:setTag() and getTag() usage for changing the controls..

Android:

setTag() and getTag() usage for changing the controls..

When we are in a situation to dynamically change something on the controls(for example), we can use setTag() and getTag() to identify which one needs to be chosen.

For example:

When i was need to change the background image for the buttons dynamically one to another, i might need to use such techniques. Here is a example code below..

Concept: When "touching" on a button, button should change the current background image to next background image. When again clicking on same button, should change the background image to previous one.
ImageButton button1;
ImageButton button2;
private int firstImgId = 0;
private int secondImgId = 0;

// setting up buttons background image..
private void setBackButtonImage()
{
Log.d("mysample", "Set back button images");
button1 = (ImageButton) findViewById(R.id.ImageButton01);
button2 = (ImageButton) findViewById(R.id.ImageButton02);
// set touch listener for buttons
button1.setOnTouchListener(this);
button2.setOnTouchListener(this);

firstImgId = R.drawable.myFirstImage;
secondImgId = R.drawable.mySecondImage;

// setting the proper images in the background.
button1.setImageResource(firstImgId);
button1.setTag("1");

button2.setImageResource(secondImgId);
button2.setTag("2");
}
public boolean onTouch(View v, MotionEvent event) 
{
ImageButton imgBtn = (ImageButton) v;
if ( imgBtn==button1 ) // first button touch
{
if ( button1.getTag()=="1" )
{
button1.setImageResource(R.drawable.myFirstImage);
button1.setTag("2");
}
else
{
button1.setImageResource(R.drawable.mySecondImage);
button1.setTag("1");
}
}
else if ( imgBtn==button2 ) // second button touch
{
if ( button2.getTag()=="1" )
{
button2.setImageResource(R.drawable.myFirstImage);
button2.setTag("2");
}
else
{
button2.setImageResource(R.drawable.mySecondImage);
button2.setTag("1");
}
}

}


That's it, you got go !!!


Thank you for followers!

Cheers,

M.P.Prabakar
Senior Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

Monday, March 22, 2010

Android:How to avoid XML verification failed error and Upgrading to 2.x SDK successfully?

Android:

How to avoid XML verification failed error and Upgrading to 2.x SDK successfully?

Reason:
When we want to upgrade Android SDK to any latest available versions, we will choose go to Eclipse menu->Window->Android SDK and AVD Manager, sometimes it throws error as follows:

Error:
XML verification failed for https://dl-ssl.google.com/android/repository/repository.xml. Error: cvc-elt.1: Cannot find the declaration of element 'sdk:sdk- repository'. Failed to fetch URL

To resolve such thing, there are two ways described below, either one would resolve it!

One quick solution:

Instead of "https", try to add same repository URL as "http"

If you are still facing the same problems,

Then you are running into older version of Android tools 3.0 or below, which will not be able to do upgradation from Repository URL in this way. you need to follow the below steps:

Second solution

Close Eclipse.

1. You will need to download a newer starter package from http://developer.android.com/sdk/index.html

There will be a Zip file to download. For example, for mac download, choose "Mac OS X (intel)-android-sdk_r05-mac_86.zip"

2. After downloading it, just extract it.

3. Replace "Tools" folder from the new one to existing installed one.
Example: copy "Tools" folder from the new download folder and then overwrite with the "Tools" folder installed the Android SDK already available in your computer.

4. Launch Eclipse. You may find error as "The Android SDK requires Android Developer Toolkit version 0.9.6 or above. Current version if 0.9.3.v200090 . Please update to latest version of ADT. "

do not worrry, do the below...

5. Go to eclipse menu "Help->Install New Software". Click on "Add". Provide a name and add URL as
https://dl-ssl.google.com/android/eclipse/
Click "OK". you can find the site selected with checkbox.
Check the name you have provided for that link, it will display the ADT tools. Choose and click "Finish".

6. Go again to,

Eclipse menu->Window->Android SDK and AVD Manager

It should be able to download the latest version of SDK's and other docs etc. Choose whatever you want and download it.
After downloading all selected, it will ask you restart the computer automatically. Do the same.

That's it, you got to go and eat your peanuts well ( no tension ;-) ) !


Thank you for followers!

Cheers,

M.P.Prabakar
Senior Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

Android:Create new Android Virtual Device(AVD) to deploy applications on Emulator:

Android:

How to create new Android Virtual Device(AVD) to deploy applications on Emulator:

1. Launch Eclipse
2. Click Window->Android SDK and AVD Manager

(OR)

If you are facing launching "Android SDK and AVD Manager", then
1. go to your installed Android SDK folder, and "Tools" folder there.
2. Click "android" file there

It will automatically launch Android SDK and AVD Manager.
Provide new name for your virtual device and choose the Target to be deployable(version) and click on 'Create AVD' to create it.

============================================================================================

After done with either of the above thing, do the following..

1. Launch Eclipse
2. Menu Run->Run Configurations
3. Choose the project and then "Target" tab
4. In the Target tab, you will find list of Virtual devices which you have added by following above point. Choose there which version you want to deploy.


That's it ;-) !!!

Cheers!
M.P.Prabakar
Senior Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..
http://itunes.apple.com/us/app/tossring/id358100360?mt=8

Sunday, March 21, 2010

Eclipse: Launch Eclipse with -clean option On Mac OS X?

Eclipse:

How to launch Eclipse with -clean option using command line on Mac OS X?

Benefits of Launching Eclipse with -clean option:

* Cleans cached data used by the OSGi framework and Eclipse runtime.
* Try to run Eclipse once with this option if you observe startup errors after install, update, or using a shared configuration.

Steps to launch:

1. Make sure you are "cd" to Eclipse folder where you installed on Mac.

For ex:
myname-mymacbook:~ myname$ cd library
myname-mymacbook:library myname$ cd eclipse

2. Use the command "./eclipse -clean -vmargs -XstartOnFirstThread" (without quotes) to launch Eclipse with -clean option.

For ex:
myname-mymacbook:eclipse myname$./eclipse -clean -vmargs -XstartOnFirstThread

Source found from:
http://www.brooksandrus.com/blog/2007/06/30/run-eclipse-from-the-command-line-on-os-x/

That's it.

Cheers,

M.P.Prabakar
Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..
http://itunes.apple.com/us/app/tossring/id358100360?mt=8

Friday, March 19, 2010

Android:How to import existing Android project into project workspace?

Android:

How to import existing Android project into project workspace?

1. Launch Eclipse
2. Choose File menu, click New->Android Project
3. then Choose "Create project from existing resource".
4. Browse the location where the project is.
After chosen, it will take automatically other settings there.
5. Click 'Finish' to complete it.

Cheers,

M.P.Prabakar
Senior Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..
http://itunes.apple.com/us/app/tossring/id358100360?mt=8

Sunday, March 14, 2010

Android:Give space between top and bottom controls

Android:

How to give space between top and bottom controls

* 'Layout Margin Top' - add or change to some value. For ex: 25dip

Cheers,

M.P.Prabakar
Senior Systems Analyst

Have fun and be addictive by playing iPhone "TossRing" water game, click the link is below..
http://itunes.apple.com/us/app/tossring/id358100360?mt=8

Thursday, March 4, 2010

Android: Animation support on Android

Android:

Animation support on Android

I.
Core Animation: Animation can be applied to Views, Surfaces and other objects.
Animations are available for, Translation, Rotation, ScaleAnimation, Alpha Animation(Object level animation, fading etc) and AnimationSet(for combining several animations into one)

http://developer.android.com/intl/de/reference/android/view/animation/Animation.html
http://developer.android.com/intl/de/reference/android/view/animation/LayoutAnimationController.html

II.
Android supports tweened animation which includes rotations, fading, moving and stretching.

http://developer.android.com/intl/de/guide/topics/resources/available-resources.html

III.
Android supports 2D graphics library for drawing and animating shapes and images.

http://developer.android.com/intl/de/guide/topics/graphics/2d-graphics.html

IV)
Apart from the above, Android supports always Open GL ES 1.0 too for high end graphics.

Excellent tutorials available here:
http://developerlife.com/tutorials/?p=343

Some example sources are available in the below link:
http://www.deitel.com/ResourceCenters/Programming/Android/AndroidAnimation/tabid/3554/Default.aspx


Thank you for followers!

Cheers,

M.P.Prabakar
Senior Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..

Android: How to zoom-in zoom-out images on Android

Android:

How to zoom-in zoom-out images on Android:

I.)
Sample code:

float scaleWidth, scaleHeight;
float zoom = 0.0f;

void ShowImage() {
Bitmap cur_bm = BitmapFactory.decodeFile(path);
float imageWidth = (float)cur_bm.getWidth();
float imageHeight = (float)cur_bm.getHeight();
float newHeight = imageHeight / (imageWidth / screenWidth);
float newWidth = screenWidth;
scaleWidth = screenWidth / imageWidth;
scaleHeight = newHeight / imageHeight;
image.setImageBitmap(cur_bm);
SetImageMatrix();

}

void SetImageMatrix() {
Matrix mtrx = new Matrix();
mtrx.postScale(scaleWidth, scaleHeight);
image.setImageMatrix(mtrx);
image.setScaleType(ScaleType.MATRIX);
image.invalidate();

}

void ZoomIn(float zoom) {
zoom += 0.01;
scaledWidth += zoom;
scaledHeight += zoom;
ShowImage();

}

void ZoomOut(float zoom) {
zoom -= 0.01;
scaledWidth -= zoom;
scaledHeight -= zoom;
}

II.) References:

http://groups.google.com/group/android-developers/browse_thread/thread/bdc4998a70f310c7

http://www.anddev.org/images_and_photos_zoom_in-out-t265.html


Thank you for followers!

Cheers,

M.P.Prabakar
Senior Systems Analyst.

Have fun and be addictive by playing "TossRing" marvelous iPhone game. Link is below..