Friday 6 July 2012

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();
}


}

No comments:

Post a Comment