Gravity sensing is mainly used in game development. This effect is very good! It is mainly three axes. It is easy to understand: that is facing up, the value is positive, and the value is down negative! I have tested this effect, because gravity sensing can be realized on the mobile phone, so no. Sorry! Reprinted please indicate the source: http://blog.csdn.net/wdaming1986/article/details/6752232
1. MainActivity. Java code:
Package com. ray. test;
Import android. app. Activity;
Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private SensorManager sensorMgr;
Private TextView show_TextView;
Sensor sensor;
Private float x, y, z;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Show_TextView = (TextView) findViewById (R. id. text_view );
SensorMgr = (SensorManager) getSystemService (SENSOR_SERVICE );
Sensor = sensorMgr. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
SensorEventListener lsn = new SensorEventListener ()
{
@ Override
Public void onSensorChanged (SensorEvent e ){
// TODO Auto-generated method stub
X = e. values [SensorManager. DATA_X];
Y = e. values [SensorManager. DATA_Y];
Z = e. values [SensorManager. DATA_Z];
SetTitle ("x =" + (int) x + "," + "y =" + (int) y + "," + "z =" + (int) z );
Show_TextView.setText ("x =" + (int) x + "," + "y =" + (int) y + "," + "z =" + (int) z );
}
@ Override
Public void onAccuracyChanged (Sensor arg0, int arg1 ){
}
}; // Register the listener. The third parameter is the detection accuracy.
SensorMgr. registerListener (lsn, sensor, SensorManager. SENSOR_DELAY_GAME );
}
}
Ii. main. xml code: www.2cto.com
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: id = "@ + id/text_view"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "25pt"
/>
</LinearLayout>