Wednesday, March 30, 2011

Raw Data

String ReadRaw(int id)
{
try
{
InputStream raw = getResources().openRawResource(id);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
i = raw.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = raw.read();
}
raw.close();
return byteArrayOutputStream.toString();
}
catch (IOException e)
{
e.printStackTrace();
return "";
}
}

Store DB Links

http://www.screaming-penguin.com/node/7742
http://android.bigresource.com/Android-Want-Database-to-save-retrieve-names-in-phone-application-haK6YhJIr.html


main.xml


xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
"http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content"
android:text="@string/hello" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />



DataHelper.java

package com.ram.sampleDB;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

private static final String DATABASE_NAME = "example.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";

private Context context;
private SQLiteDatabase db;


private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
+ TABLE_NAME + "(name) values (?)";


public DataHelper(sampleDB sampleDB) {
// TODO Auto-generated constructor stub
this.context = sampleDB;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}

public void deleteAll() {
// TODO Auto-generated method stub
this.db.delete(TABLE_NAME, null, null);
}

public long insert(String name) {
// TODO Auto-generated method stub
this.insertStmt.bindString(1, name);
return this.insertStmt.executeInsert();

}

public List selectAll() {
// TODO Auto-generated method stub
List list = new ArrayList();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
null, null, null, null, "name desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}

private static class OpenHelper extends SQLiteOpenHelper {

OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}


sampleDB.java

package com.ram.sampleDB;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class sampleDB extends Activity {
/** Called when the activity is first created. */

private TextView output;

private DataHelper dh;

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

this.output = (TextView) this.findViewById(R.id.out_text);

this.dh = new DataHelper(this);
this.dh.deleteAll();
this.dh.insert("Porky Pig");
this.dh.insert("Foghorn Leghorn");
this.dh.insert("Yosemite Sam");
List names = this.dh.selectAll();
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n");
for (String name : names) {
sb.append(name + "\n");
}

Log.d("EXAMPLE", "names size - " + names.size());

this.output.setText(sb.toString());

}
}

Saturday, March 19, 2011

Android Links

http://www.ndtv.com/convergence/ndtv/rssfeedback.aspx
http://www.google.com/language_tools
http://maxood-android-corner.blogspot.com/2011/03/tutorial-using-google-translate-api-in.html
http://android-er.blogspot.com/2009/10/multi-language-translate.html
http://stackoverflow.com/questions/4979078/google-translator-in-android
http://stackoverflow.com/questions/2325547/how-to-have-a-translation-service-in-android-app

Thursday, March 10, 2011

USING CANVAS TO ROTATE BALL

package in.ram.JungleMayhem;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;


public class JungleMayhem extends Activity {
//private Bitmap bg;
private DrawOval dw;
int x = 30;
int y = 230;
//private boolean start = true;
private boolean running = true;
private boolean direction = true;
private boolean direction1;
private boolean direction2;
private boolean direction3;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bg1480x320);
//Bitmap bg= Bitmap.createScaledBitmap(mBitmap, 240, 320, true);
//int pic_width = mBitmap.getWidth();
//int pic_height = mBitmap.getHeight();
//ImageView img = new ImageView(this);
//img.setImageBitmap(bg);
//setContentView(img);
dw = new DrawOval(this);
setContentView(dw,new ViewGroup.LayoutParams(240,400));
(new Thread(new Animationloop())).start();
}

public void updatePhysics() {
if(direction == true) {
if(x<= 130) {
x+=1;
y-=1;
}else if(x==131){
direction = false;
direction1=true;
}
}

if(direction1==true){
if(x<=230){
x+=1;
y+=1;
}else if(x==231){
direction1=false;
direction2=true;
}
}

if(direction2==true){
if(x>=131){
x-=1;
y+=1;
}else if(x==130){
direction2=false;
direction3=true;
}
}

if(direction3==true){
if(x>=30){
x-=1;
y-=1;
}else if(x==29){
direction3=false;
direction=true;
x = 30;
y = 230;
}
}
}

class DrawOval extends View {
//Canvas c;
//private ShapeDrawable shp;
private Bitmap bg = BitmapFactory.decodeResource(getResources(),R.drawable.bg2480x320);
private Bitmap bg1= Bitmap.createScaledBitmap(bg, 240, 320, true);
private Bitmap p1 = BitmapFactory.decodeResource(getResources(),R.drawable.egg);
private Bitmap egg = Bitmap.createScaledBitmap(p1, 10, 10, true);

private Bitmap pl1 =BitmapFactory.decodeResource(getResources(),R.drawable.egg1);
private Bitmap pl2 =BitmapFactory.decodeResource(getResources(),R.drawable.egg2);
private Bitmap pl11 = Bitmap.createScaledBitmap(pl1, 45,45, true);
private Bitmap pl22 = Bitmap.createScaledBitmap(pl2, 45,45, true);


public DrawOval(Context context) {
super(context);
}
//shp = new ShapeDrawable(new OvalShape());
//shp.getPaint().setColor(0xff74AC23);
//shp.setBounds(0, 0, 480, 360);


protected synchronized void onDraw(Canvas canvas) {

canvas.drawBitmap(bg1, new Rect(0,0,240,400), new Rect(0,0,240,400), new Paint());
canvas.drawBitmap(pl11,25,150,new Paint());
canvas.drawBitmap(pl22,165,150,new Paint());
canvas.drawBitmap(egg,200,200,new Paint());
canvas.drawBitmap(egg,x,y,new Paint());
//canvas.drawBitmap(p1, new Rect(0,0,100,100), new Rect(0,0,100,100), new Paint());
//canvas.drawColor(Color.BLUE);
//shp.draw(canvas);
invalidate();
}
}

class Animationloop implements Runnable {

@Override
public void run() {
while(true) {
while(running) {
try {
Thread.sleep(10);
}catch(InterruptedException e) {
System.out.println("-------InterruptedException-----"+e);
}
//dw.postInvalidate();
updatePhysics();
}
}

}

}
}

Ondraw/canvas method

package in.ram.JungleMayhem;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;


public class JungleMayhem extends Activity {
//private Bitmap bg;
private DrawOval dw;
int x = 0;
int y = 100;
//private boolean start = true;
private boolean running = true;
private boolean direction = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bg1480x320);
//Bitmap bg= Bitmap.createScaledBitmap(mBitmap, 240, 320, true);
//int pic_width = mBitmap.getWidth();
//int pic_height = mBitmap.getHeight();
//ImageView img = new ImageView(this);
//img.setImageBitmap(bg);
//setContentView(img);
dw = new DrawOval(this);
setContentView(dw,new ViewGroup.LayoutParams(240,320));
(new Thread(new Animationloop())).start();
}
public void updatePhysics() {
if(direction == true) {
if(x<= 80) {
x+=1;
y-=1;
}
else if(x>80 && x<=100) {
y-=0;
x+=1;
}
else if(x>=100) {
y+=1;
x+=1;
}
}
if(x==200){
direction = false;
}
if(direction == false) {
//running = false;
if(x>=100) {
x-=1;
y-=1;
}
else if(x<=100 && x>=80) {
y-=0;
x-=1;
}
else if(x<=80) {
y+=1;
x-=1;
}
if(x == 50) {
direction = true;
}

}

}

class DrawOval extends View {
//Canvas c;
//private ShapeDrawable shp;
private Bitmap bg = BitmapFactory.decodeResource(getResources(),R.drawable.bg2480x320);
private Bitmap bg1= Bitmap.createScaledBitmap(bg, 240, 320, true);
private Bitmap p1 = BitmapFactory.decodeResource(getResources(),R.drawable.egg);
private Bitmap egg = Bitmap.createScaledBitmap(p1, 10, 10, true);

private Bitmap pl1 =BitmapFactory.decodeResource(getResources(),R.drawable.egg1);
private Bitmap pl2 =BitmapFactory.decodeResource(getResources(),R.drawable.egg2);
private Bitmap pl11 = Bitmap.createScaledBitmap(pl1, 45,45, true);
private Bitmap pl22 = Bitmap.createScaledBitmap(pl2, 45,45, true);


public DrawOval(Context context) {
super(context);

//shp = new ShapeDrawable(new OvalShape());
//shp.getPaint().setColor(0xff74AC23);
//shp.setBounds(0, 0, 480, 360);
}



protected synchronized void onDraw(Canvas canvas) {

canvas.drawBitmap(bg1, new Rect(0,0,240,320), new Rect(0,0,240,320), new Paint());
canvas.drawBitmap(pl11,25,150,new Paint());
canvas.drawBitmap(pl22,165,150,new Paint());
//canvas.drawBitmap(egg,200,200,new Paint());
canvas.drawBitmap(egg,x,y,new Paint());
//canvas.drawBitmap(p1, new Rect(0,0,100,100), new Rect(0,0,100,100), new Paint());
//canvas.drawColor(Color.BLUE);
//shp.draw(canvas);
invalidate();

}
}

class Animationloop implements Runnable {

@Override
public void run() {
while(true) {
while(running) {
try {
Thread.sleep(10);
}catch(InterruptedException e) {
System.out.println("-------InterruptedException-----"+e);
}
//dw.postInvalidate();
updatePhysics();
}
}

}

}
}

Wednesday, March 9, 2011

Android Record/play

http://www.hascode.com/2010/05/sensor-fun-creating-a-simple-audio-recorderplayer/

Android Links

http://www.droidnova.com/2d-sprite-animation-in-android,471.html

http://www.deitel.com/ResourceCenters/Programming/Android/Animation/tabid/3554/Default.aspx

http://www.hascode.com/2010/05/sensor-fun-location-based-services-and-gps-for-android

/http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/

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