I have a game object, well a bitmap, which I'd like to "fling". I'm struggling to get it to fling ontouchlistener due to it being a bitmap and not sure how to proceed and I'm struggling to find the resources to help. Here's my code so far:
https://github.com/addrum/Shapes
GameActivity class:
package com.main.shapes;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View.OnTouchListener;
import android.view.Window;
public class GameActivity extends Activity {
private GestureDetector gestureDetector;
View view;
Bitmap ball;
float x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
view = new View(this);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
gestureDetector = new GestureDetector(this, new GestureListener());
x = 0;
y = 0;
setContentView(view);
ball.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(android.view.View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
@Override
protected void onPause() {
super.onPause();
view.pause();
}
@Override
protected void onResume() {
super.onResume();
view.resume();
}
public class View extends SurfaceView implements Runnable {
Thread thread = null;
SurfaceHolder holder;
boolean canRun = false;
public View(Context context) {
super(context);
holder = getHolder();
}
public void run() {
while (canRun) {
if (!holder.getSurface().isValid()) {
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 255, 255, 255);
c.drawBitmap(ball, x - (ball.getWidth() / 2), y - (ball.getHeight() / 2), null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
canRun = false;
while (true) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
thread = null;
}
public void resume() {
canRun = true;
thread = new Thread(this);
thread.start();
}
}
}
GestureListener class:
package com.main.shapes;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
public class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//From Right to Left
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//From Left to Right
return true;
}
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
//From Bottom to Top
return true;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
//From Top to Bottom
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
//always return true since all gestures always begin with onDown and<br>
//if this returns false, the framework won't try to pick up onFling for example.
return true;
}
}