Move The Apple on TouchEvent

December 2nd, 2011 by aabhaanjan

The most important technical points you are going to learn are :
SurfaceView : The SurfaceView punches a hole in its window to allow its surface to be displayed.
getHolder(): By calling getHolder()we can access to the underlying surface
surfaceCreated(SurfaceHolder): Gives an idea of when the Surface is created.
surfaceDestroyed(SurfaceHolder):Gives an idea of when the Surface is destroyed
SurfaceHolder.addCallback :The Callback is set with SurfaceHolder.addCallback method

Step 1. Create a new android motodev project.

1

Step 2 : Put a apple.png in your res/drwable folder.
Step 3 : In your MainActivity class paste following codes :
[xml]
package com.ballmovementontouch;

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

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
}
[/xml]

Step 4: Create a new class GameView and paste following codes :
[xml]
package com.ballmovementontouch;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements
SurfaceHolder.Callback {
private MainThread thread;
private Apple appleInstance;

public GameView(Context context) {
super(context);
getHolder().addCallback(this);
appleInstance = new Apple(BitmapFactory.decodeResource(getResources(), R.drawable.apple), 50, 50);
thread = new MainThread(getHolder(), this);
setFocusable(true);
}

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

@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {

}
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
appleInstance.handleActionDown((int)event.getX(), (int)event.getY());
if (event.getY() > getHeight() – 50) {
thread.setRunning(false);
((Activity)getContext()).finish();
}
} if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (appleInstance.isTouched()) {
appleInstance.setX((int)event.getX());
appleInstance.setY((int)event.getY());
}
} if (event.getAction() == MotionEvent.ACTION_UP) {
if (appleInstance.isTouched()) {
appleInstance.setTouched(false);
}
}
return true;
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
appleInstance.draw(canvas);
}

}
[/xml]

Step 5: Create an Apple Class and paste following code :
[xml]
package com.ballmovementontouch;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Apple {
private Bitmap bitmap;
private int x;
private int y;
private boolean touched;

public Apple(Bitmap bitmap, int x, int y) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
}

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 void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, x – (bitmap.getWidth() / 2), y – (bitmap.getHeight() / 2), null);
}

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))) { setTouched(true); } else { setTouched(false); } } else { setTouched(false); } } } [/xml] Step 6: Now Run your app and move the apple on touching it. It will look like this : 2

Tags: , , , , , , , ,