I am seasoned developer and I have just returned to Android after being off work ill for 2 years (6 full strokes and near death but rebuilt everything!). Anyway I'm playing around with a custom subclassed ImageView. I've got the onTouch and onDraw working perfectly. I've added a listener for the accelerometer in the main activity so I can also move my sprite with this.
Tilting left makes my sprite move left as intended, as does up. If I tilt right though it still moves left. Likewise if I tilt down it still moves up.
I'm sure I can't see something as I've been staring at it too long so I'd appreciate a new pair of eyes!
Thanks
Leigh
package com.tilleytech.wubl.view;
/**
* Created by Leigh on 09/03/2015.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import com.tilleytech.wubl.R;
import com.tilleytech.wubl.WUBLApplication;
import com.tilleytech.wubl.object.Point;
import com.tilleytech.wubl.util.Util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class DrawView extends ImageView implements OnTouchListener {
public static enum DIRECTION {LEFT, RIGHT, UP, DOWN};
private static final String TAG = "DrawView";
private Bitmap player = null;
private Bitmap saucer = null;
private Bitmap saucer2 = null;
private Bitmap saucer3 = null;
private int saucerLeft=1100;
private int saucer2Left=1400;
private int saucer3Left=1250;
//boundary of our view
private static final int XMIN = 0;
private static final int XBOUNDARY=1080;
private static final int YMIN=0;
private static final int YBOUNDARY=1920;
List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
init(context);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
/* paint.setColor(Color.WHITE);
paint.setAntiAlias(true);*/
player = Util.decodeSampledBitmapFromResource(getResources(), R.drawable.player, 80, 93);
saucer = Util.decodeSampledBitmapFromResource(getResources(), R.drawable.saucer, 100, 73);
saucer2 = Util.decodeSampledBitmapFromResource(getResources(), R.drawable.saucer2, 100, 73);
saucer3 = Util.decodeSampledBitmapFromResource(getResources(), R.drawable.saucer3, 100, 73);
setWillNotDraw(false);
}
@Override
public void onDraw(Canvas canvas) {
//test out some UFOs in here for now
if(saucer2Left<-150) {
saucerLeft=1100;
saucer2Left= 1400;
saucer3Left=1250;
}
canvas.drawBitmap(saucer, saucerLeft--, 10, null);
canvas.drawBitmap(saucer2, saucer2Left--, 10, null);
canvas.drawBitmap(saucer3, saucer3Left--, 125, null);
//We have 20 points which will create a trail effect
WUBLApplication.logMessage("Whiz through our points drawing to canvas.", TAG, WUBLApplication.LogLevel.Debug);
synchronized (points) {
for (Point point : points) { //set in onTouch
canvas.drawBitmap(player, point.getX(), point.getY(), null);
}
}
}
public boolean onTouch(View view, MotionEvent event) {
int eventAction = event.getAction();
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
// finger touches the screen
break;
case MotionEvent.ACTION_MOVE:
// finger moves on the screen
Point point = new Point(event.getX(), event.getY());
addPoint(point);
synchronized (points) {
if (points.size() > 20) {
points.remove(0);
}
}
break;
case MotionEvent.ACTION_UP:
// finger leaves the screen
cleanUpPoints();
break;
}
//Force a repaint
invalidate();
return true;
}
public void cleanUpPoints() {
synchronized (points) {
int counter = 1;
int max = points.size()-1;
Iterator<Point> it = points.iterator();
while (it.hasNext() & counter <= max) {
it.next();
it.remove();
counter++;
}
}
}
public void addPoint(Point point) {
//WUBLApplication.logMessage("addPoint: " + point.toString(), TAG, WUBLApplication.LogLevel.Debug);
synchronized (points){
points.add(point);
}
}
public Point getPoint(int index) {
Point point=null;
if(index>-1) {
synchronized (points) {
if (!points.isEmpty()) {
point = points.get(index);
}
}
}
return point;
}
public Point getLastPoint() {
Point point=null;
//WUBLApplication.logMessage("getLastPoint before synch block", TAG, WUBLApplication.LogLevel.Debug);
synchronized (points) {
//WUBLApplication.logMessage("Points isEmpty: " + points.isEmpty(), TAG, WUBLApplication.LogLevel.Debug);
if (!points.isEmpty()) {
point = points.get(points.size() - 1);
}
}
return point;
}
public void clearPoints() {
synchronized (points){
points.clear();
}
}
public int getPointsSize(){
int result=0;
synchronized (points){
result=points.size();
}
return result;
}
public void moveSprite(DIRECTION direction, float change) {
//Called from accelerometer listener in activity using this view
WUBLApplication.logMessage("moveSprite with direction: " + direction.toString(), TAG, WUBLApplication.LogLevel.Debug);
//Get the last point and shift from here
Point lastKnownPoint = getLastPoint();
switch (direction) {
case LEFT: {
WUBLApplication.logMessage("moveSprite inside LEFT", TAG, WUBLApplication.LogLevel.Debug);
if (lastKnownPoint != null) {
float lastX = lastKnownPoint.getX();
float lastY = lastKnownPoint.getY();
if (lastX - change > XMIN && lastX - change < XBOUNDARY) {
WUBLApplication.logMessage("LEFT: subtract from x", TAG, WUBLApplication.LogLevel.Debug);
lastX -= change;
addPoint(new Point(lastX, lastY));
}
}
break;
}
case RIGHT: {
WUBLApplication.logMessage("moveSprite inside RIGHT", TAG, WUBLApplication.LogLevel.Debug);
if (lastKnownPoint != null) {
float lastX = lastKnownPoint.getX();
float lastY = lastKnownPoint.getY();
if (lastX + change > XMIN && lastX + change < XBOUNDARY) {
WUBLApplication.logMessage("RIGHT: add to x", TAG, WUBLApplication.LogLevel.Debug);
lastX += change;
addPoint(new Point(lastX, lastY));
}
}
break;
}
case UP: {
WUBLApplication.logMessage("moveSprite inside UP", TAG, WUBLApplication.LogLevel.Debug);
if (lastKnownPoint != null) {
float lastX = lastKnownPoint.getX();
float lastY = lastKnownPoint.getY();
WUBLApplication.logMessage("UP: Add to y", TAG, WUBLApplication.LogLevel.Debug);
if (lastY + change > YMIN && lastY + change < YBOUNDARY) {
lastY += change;
addPoint(new Point(lastX, lastY));
}
}
break;
}
case DOWN: {
WUBLApplication.logMessage("moveSprite inside DOWN", TAG, WUBLApplication.LogLevel.Debug);
if (lastKnownPoint != null) {
float lastX = lastKnownPoint.getX();
float lastY = lastKnownPoint.getY();
if (lastY - change > YMIN && lastY - change < YBOUNDARY) {
WUBLApplication.logMessage("DOWN: subtract from y", TAG, WUBLApplication.LogLevel.Debug);
lastY -= change;
addPoint(new Point(lastX, lastY));
}
}
break;
}
}
invalidate();
}
}
and the activity class:
package com.tilleytech.wubl.activity;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import com.tilleytech.wubl.R;
import com.tilleytech.wubl.WUBLApplication;
import com.tilleytech.wubl.view.DrawView;
/**
* Created by Leigh on 09/03/2015.
*/
public class DrawingViewActivity extends Activity implements SensorEventListener {
DrawView drawView;
private SensorManager sensorManager;
private Sensor accelerometer;
private final static String TAG = DrawingViewActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_drawingview);
drawView = (DrawView)findViewById(R.id.drawingview);
listenAccelerometer();
drawView.requestFocus();
}
protected void listenAccelerometer() {
WUBLApplication.logMessage("Request sensor service", TAG, WUBLApplication.LogLevel.Debug);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
// success! we have an accelerometer
WUBLApplication.logMessage("Link to accelerometer", TAG, WUBLApplication.LogLevel.Debug);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
} else {
// fai! we dont have an accelerometer!
WUBLApplication.logMessage("No accelerometer found!", TAG, WUBLApplication.LogLevel.Debug);
}
}
//onResume() register the accelerometer for listening the events
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
//onPause() unregister the accelerometer for stop listening the events
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
int speed = 6;
if (Math.abs(x) > Math.abs(y)) {
if (x < 0) {
//right
WUBLApplication.logMessage("accelerometer right", TAG, WUBLApplication.LogLevel.Debug);
float xChange = speed * x;
drawView.moveSprite(DrawView.DIRECTION.RIGHT, xChange);
}
if (x > 0) {
//left
WUBLApplication.logMessage("accelerometer left", TAG, WUBLApplication.LogLevel.Debug);
float xChange = speed * x;
drawView.moveSprite(DrawView.DIRECTION.LEFT, xChange);
}
} else {
if (y < 0) {
//up
WUBLApplication.logMessage("accelerometer up", TAG, WUBLApplication.LogLevel.Debug);
float yChange = speed * y;
drawView.moveSprite(DrawView.DIRECTION.UP, yChange);
}
if (y > 0) {
//down
WUBLApplication.logMessage("accelerometer down", TAG, WUBLApplication.LogLevel.Debug);
float yChange = speed * y;
drawView.moveSprite(DrawView.DIRECTION.DOWN, yChange);
}
}
if (x > (-2) && x < (2) && y > (-2) && y < (2)) {
//centre
drawView.cleanUpPoints();
//nothing, hold
}
}
@Override
protected void onStop()
{
// Unregister the listener
sensorManager.unregisterListener(this);
super.onStop();
}
}