Monday 23 July 2012

Add Integation


DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
String myAdId = "MY_LEADBOLT_SECTIONID_320";
if(screenWidth >= 720) {
myAdId = "MY_LEADBOLT_SECTIONID_720";
}
else if(screenWidth >= 640) {
myAdId = "MY_LEADBOLT_SECTIONID_640";
}
else if(screenWidth >= 468) {
myAdId = "MY_LEADBOLT_SECTIONID_468";
}
AdController myController = new AdController(activity, myAdId);
myController.setAsynchTask(true);
myController.loadAd();

Friday 6 July 2012

Move Object on Android Canvas 5

5:- Now Create Activity which call surfaceview :-




import java.io.IOException;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;


public class Aarti extends Activity {

SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;
int soundID = 1;
int width;
int height;
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
  
   //soundPoolMap.put(soundID, soundPool.load(this, R.raw.adaon, 1));
   requestWindowFeature(Window.FEATURE_NO_TITLE);
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
   Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth();//get width
    height = display.getHeight();//get height
   setContentView(new ArtiView(this,width,height));
 
}   

@Override
protected void onDestroy() {
//Log.d(TAG, "Destroying...");
super.onDestroy();
}


@Override
protected void onStop() {
// Log.d(TAG, "Stopping...");
super.onStop();

 
}


}

Move Object on Android Canvas 4

4:- Now Create Class Speed :-



public class Speed {


public static final int DIRECTION_RIGHT = 1;
public static final int DIRECTION_LEFT = -1;
public static final int DIRECTION_UP = -1;
public static final int DIRECTION_DOWN = 1;

private int[] tt={100,500};
private float xv = 1; // velocity value on the X axis
private float yv = 1; // velocity value on the Y axis

private int xDirection = DIRECTION_RIGHT;
private int yDirection = DIRECTION_DOWN;

public Speed() {
this.xv = 2;
this.yv = 2;
}


public Speed(float xv, float yv) {
this.xv = xv;
this.yv = yv;
}


public float getXv() {
return xv;
}
public void setXv(float xv) {
this.xv = xv;
}
public float getYv() {
return yv;
}
public void setYv(float yv) {
this.yv = yv;
}


public int getxDirection() {
return xDirection;
}
public void setxDirection(int xDirection) {
this.xDirection = xDirection;
}
public int getyDirection() {
return yDirection;
}
public void setyDirection(int yDirection) {
this.yDirection = yDirection;
}


// changes the direction on the X axis
public void toggleXDirection() {
xDirection = xDirection * -1;
}


// changes the direction on the Y axis
public void toggleYDirection() {
yDirection = yDirection * -1;
}


}

Move Object on Android Canvas 3

3:- now create Surfaceview Class same As Below:-


import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


public class ArtiView extends SurfaceView implements
SurfaceHolder.Callback {
Context c;
private static final String TAG = ArtiView.class.getSimpleName();
public static int w=0;
public static int h=0;
private ThredArti thread;
private Droid droid;
private MediaPlayer mediaPlayer;
private boolean con=false,first=true;
public ArtiView(Context context,int width,int height) {

super(context);
   c=context;
 
//this.setBackgroundResource(R.drawable.ganeshji);
// adding the callback (this) to the surface holder to intercept events
getHolder().addCallback(this);
        w=width;
        h=height;
// create droid and load bitmap
droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.thali),BitmapFactory.decodeResource(getResources(), R.drawable.ganesh2),BitmapFactory.decodeResource(getResources(), R.drawable.ch1),BitmapFactory.decodeResource(getResources(), R.drawable.ch2), width, height);

// create the game loop thread
thread = new ThredArti(getHolder(), this);
mediaPlayer = MediaPlayer.create(c, R.raw.arti);
// make the GamePanel focusable so it can handle events
setFocusable(true);
}


public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}


public void surfaceCreated(SurfaceHolder holder) {
// at this point the surface is created and
// we can safely start the game loop
thread.setRunning(true);
thread.start();
mediaPlayer.start();

}


public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "Surface is being destroyed");
thread.setRunning(false);
mediaPlayer.stop();
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
Log.d(TAG, "Thread was shut down cleanly");

}

public void render(Canvas canvas) {
canvas.drawColor(Color.rgb(96, 83, 109));//60536d
Paint p=new Paint(Color.WHITE);
   //System.out.println(w+"/"+h);
Rect r = new Rect();
r.left=0;
r.right=w*25/100;
r.top=0;
r.bottom=h*80/100;
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ctnr),null,r,null);

r.left=w*75/100;
r.right=w;
r.top=0;
r.bottom=h*80/100;
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ctnl),null,r,null);


r.left=0;
r.right=w;
r.top=h*90/100;
r.bottom=h;
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.plr),null,r, null);
//canvas.drawCircle(h, w, 200, p);
droid.draw(canvas, h, w);



}



public boolean onTouchEvent(MotionEvent event) 
{
if(event.getAction()==MotionEvent.ACTION_UP)
   {
// Log.i("msg","X="+event.getX()+" and Y="+event.getY());
if(event.getY()>(h-(h*6/100))&&event.getY()<(h-(h*2/100)))
{
if(event.getX()>w*35/100&&event.getX()<w*41/100)
{
//Play
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
droid.setTouched(false);
}
}
else if(event.getX()>w*19/100&&event.getX()<w*25/100)
{
if(mediaPlayer.isPlaying())
{
con=false;
mediaPlayer.pause();
droid.setTouched(true);
}
}

}

  
        }
return true;
}

public  boolean onKey (View v, int keyCode, KeyEvent event)
{
System.out.print("FSDFASDF"+keyCode);
return true;

}
protected void onDetachedFromWindow ()
{
System.out.print("dETECHED");
}
public void update() {
// check collision with right wall if heading right
/*if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
&& droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) {
droid.getSpeed().toggleXDirection();
}
// check collision with left wall if heading left
if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
&& droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) {
droid.getSpeed().toggleXDirection();
}
// check collision with bottom wall if heading down
if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
&& droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()) {
droid.getSpeed().toggleYDirection();
}
// check collision with top wall if heading up
if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP
&& droid.getY() - droid.getBitmap().getHeight() / 2 <= 0) {
droid.getSpeed().toggleYDirection();
}*/
// Update the lone droid
//if(con)
droid.update();
}


}

Move Object on Android Canvas 2

2:- Create Class for Object which move:-

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.widget.Button;


public class Droid {
    private static int i=-1;
private Bitmap bitmap;
private Bitmap bitmap1;// the actual bitmap
private Bitmap bitmap2;
private Bitmap bitmap3;// the actual bitmap
private int x; // the X coordinate
private int y; // the Y coordinate
private boolean touched; // if droid is touched/picked up
private Speed speed; // the speed with its directions
private  Point[] po ;
public Droid(Bitmap bitmap,Bitmap bitmap1,Bitmap bitmap2,Bitmap bitmap3, int x, int y) {
this.bitmap = bitmap;
this.bitmap1=bitmap1;
this.bitmap2=bitmap2;
this.bitmap3=bitmap3;
this.x = x;
this.y = y;
this.speed = new Speed();
Point p=new Point();
p.set(x/2-50,y/2-30);
getNPointsOnCircle(p,x*30/100,200);
}

public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}


public boolean isTouched() {
return touched;
}


public void setTouched(boolean touched) {
this.touched = touched;
}

public Speed getSpeed() {
return speed;
}


public void setSpeed(Speed speed) {
this.speed = speed;
}


public void draw(Canvas canvas,int h,int w) {
   canvas.setDensity(120);
   Rect r = new Rect();
   r.left=w/2-w*25/100;
   r.right=w/2+w*25/100;
   r.top=h*5/100;
   r.bottom=r.top+w*50/100;
if(i%2==0)
   canvas.drawBitmap(bitmap2, null, r,null);
else
canvas.drawBitmap(bitmap3,null,r,null);
r.top=h*10/100;
r.bottom=h*65/100;
r.left=w*20/100;
r.right=w*80/100;
canvas.drawBitmap(bitmap1, null, r, null);
canvas.drawBitmap(bitmap, po[i].x, po[i].y, null);

}


public void getNPointsOnCircle( Point p, int r, int n )   {


   double Number = Math.PI * 2 / n;
  po= new Point[n];
            
   int i = -1;
   while( ++i < n )
   {
       double theta = Number * i;
       Point pointOnCircle = new Point( );
       pointOnCircle.x=(int) (Math.cos( theta ) * r)+p.x;
       pointOnCircle.y=(int) (Math.sin( theta ) * r)+p.y;
       System.out.println("X:"+ pointOnCircle.x+ " and Y:"+ pointOnCircle.y);
       po[ i ] = pointOnCircle ;
   }
   
 //  return points;
   
   } 
/**
* Method which updates the droid's internal state every tick
*/
public void update() {
if (!touched) {
//x += (speed.getXv() * speed.getxDirection()); 
//y += (speed.getYv() * speed.getyDirection());
if(i<195)
{
i=i+5;
}
else
i=0;
}
}


public void handleActionDown(int eventX, int eventY) {
if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {
if (eventY >= (y - bitmap.getHeight() / 2) && (y <= (y + bitmap.getHeight() / 2))) {
// droid touched
setTouched(true);
} else {
setTouched(false);
}
} else {
setTouched(false);
}


}
}
  

Move Object on Android Canvas 1

1:- Create First thread Class :-


import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;


public class Thred extends Thread {


private SurfaceHolder surfaceHolder;
// The actual view that handles inputs
// and draws to the surface
private ArtiView gamePanel;


// flag to hold game state 
private boolean running;
public void setRunning(boolean running) {
this.running = running;
}


public Thred(SurfaceHolder surfaceHolder, ArtiView gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}


@Override
public void run() {
Canvas canvas;
Log.d("ghj", "Starting game loop");
while (running) {
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state 
this.gamePanel.update();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.render(canvas);
}
} finally {
// in case of an exception the surface is not left in 
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}

}