2

Hi all i want to move an image using accelerometer values,but m not able to do it. Can anyone please help me to do this, I have already gone through sample code in android developers site but couldn't understand properly.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
dharan
  • 743
  • 2
  • 10
  • 28

1 Answers1

5

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!

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • hii Pearsonartphoto thanks for ur reply,i tried ur code but its not working.The thing is event.values[] returns values in m^2,how can v convert dis values to corrdinate system values. – dharan Jun 08 '11 at 06:56