Displaying Accelerometer Values in Android
Have you checked out the Accelerometer example in the android samples? You could always try something like this (which I got from here):
public class SensorActivity extends Activity implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
//Right in here is where you put code to read the current sensor values and
//update any views you might have that are displaying the sensor information
//You'd get accelerometer values like this:
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
float mSensorX, mSensorY;
switch (mDisplay.getRotation()) {
case Surface.ROTATION_0:
mSensorX = event.values[0];
mSensorY = event.values[1];
break;
case Surface.ROTATION_90:
mSensorX = -event.values[1];
mSensorY = event.values[0];
break;
case Surface.ROTATION_180:
mSensorX = -event.values[0];
mSensorY = -event.values[1];
break;
case Surface.ROTATION_270:
mSensorX = event.values[1];
mSensorY = -event.values[0];
}
}
}
How to use Android's Accelerometer
Your Activity can implement SensorEventListener
, override onSensorChanged(SensorEvent event)
like this:
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if (Math.abs(x) > Math.abs(y)) {
if (x < 0) {
image.setImageResource(R.drawable.right);
textView.setText("You tilt the device right");
}
if (x > 0) {
image.setImageResource(R.drawable.left);
textView.setText("You tilt the device left");
}
} else {
if (y < 0) {
image.setImageResource(R.drawable.up);
textView.setText("You tilt the device up");
}
if (y > 0) {
image.setImageResource(R.drawable.down);
textView.setText("You tilt the device down");
}
}
if (x > (-2) && x < (2) && y > (-2) && y < (2)) {
image.setImageResource(R.drawable.center);
textView.setText("Not tilt device");
}
}
More details, see my full post at: http://www.devexchanges.info/2015/05/detecting-tilt-device-by-using-sensor.html
Related Topics
Missing Styles. Is the Correct Theme Chosen for This Layout
Encoding H.264 from Camera with Android Mediacodec
Caused By: Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Logcat Not Displaying My Log Calls
Java.Lang.Unsatisfiedlinkerror: Dalvik.System.Pathclassloader
How to Add Action Bar Options Menu in Android Fragments
Custom Format Edit Text Input Android to Accept Credit Card Number
Setrotation(90) to Take Picture in Portrait Mode Does Not Work on Samsung Devices
Add Google Maps API V2 in a Fragment
Android - Inner Element Must Either Be a Resource Reference or Empty
Filter Output in Logcat by Tagname
Android - Swipe to Delete Recyclerview
How to Iterate Through a View's Elements