Moving an Image Using Accelerometer of Android

Moving an image using Accelerometer of android

Use this code. You were never setting the location of the drawable after you intialized that class. You'll have to do some calculations to set the balls location properly. The way you were doing it was getting values over 10000 which was drawing the oval off screen.

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

public class Accelerometer extends Activity implements SensorEventListener
{
/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
public static int x;
public static int y;

private SensorManager sensorManager = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
// Get a reference to a SensorManager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mCustomDrawableView = new CustomDrawableView(this);
setContentView(mCustomDrawableView);
// setContentView(R.layout.main);

}

// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent)
{
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// the values you were calculating originally here were over 10000!
x = (int) Math.pow(sensorEvent.values[1], 2);
y = (int) Math.pow(sensorEvent.values[2], 2);

}

if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

}
}
}

// I've chosen to not implement this method
public void onAccuracyChanged(Sensor arg0, int arg1)
{
// TODO Auto-generated method stub

}

@Override
protected void onResume()
{
super.onResume();
// Register this class as a listener for the accelerometer sensor
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
// ...and the orientation sensor
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop()
{
// Unregister the listener
sensorManager.unregisterListener(this);
super.onStop();
}

public class CustomDrawableView extends View
{
static final int width = 50;
static final int height = 50;

public CustomDrawableView(Context context)
{
super(context);

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}

protected void onDraw(Canvas canvas)
{
RectF oval = new RectF(Accelerometer.x, Accelerometer.y, Accelerometer.x + width, Accelerometer.y
+ height); // set bounds of rectangle
Paint p = new Paint(); // set some paint options
p.setColor(Color.BLUE);
canvas.drawOval(oval, p);
invalidate();
}
}
}

How to use Accelerometer Values to Move an Image in Android

To move an image, see how it's done in this example. Basically, the key is to get a matrix of some kind, and manipulate it based off of some input, in the case of this example, the touch/drag of a user. The below code doesn't function perfectly, but it shows the key commands and classes required to perform dragging. For more details, see the linked article.

Matrix matrix = new Matrix();
savedMatrix.set(matrix);
matrix.postTranslate(event.getX() - start.x)
ImageView view = (Some image view here)
view.setImageMatrix(matrix);

The next part is to somehow use the accelerometer to get input. I'm going to leave it to you how to best put the two together. My source of this information is this article. There are a few basic steps.

The first step is to register the listener, and is done by this code.

SensorManager  sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);

The next step is to register a sensor listener. The last bit is to show what the sensor listener looks like. Note that there are 3 outputs of the accelerometer, one for each x, y, and z coordinates.

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(accelerationListener, sensor,
SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onStop() {
sensorManager.unregisterListener(accelerationListener);
super.onStop();
}

private SensorEventListener accelerationListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int acc) {
}

@Override
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
y = event.values[1];
z = event.values[2];
}

};

Your next step will be to figure out what the x, y, and z values are for your desired application. I suggest you take and log each of these out using a Log.v(TAG,...) statement. Play around with tilting it, and figure out exactly what you want to do. Tune it until it has the right level of sensitivity, by seeing how far off it is from straight up and down, and having some kind of a rate of changing the x and y coordinates of the image. Good luck!



Related Topics



Leave a reply



Submit