Concept
The use of gravity-sensing technology of Android games have been common, do not know whether they will be used later, so first studied a bit.
Learning on the Internet, seemingly no API, so you have to analyze the mobile phone in what state. Note: The demo program provided below can only be run on a real machine with gravity sensing.
Gravity Sensing coordinate system
Look at the simulation diagram:
With the bottom left of the screen as the origin, the arrow points to a positive direction. From 10 to 10, floating-point units (2D programming time, the upper left of the screen is the origin, which is not the same as the 3D place)
The XYZ value rule is: The upside-down is positive (the bottom left corner of the screen and the ground parallel face as the base surface, on this side, the farther away the value, under this face, the farther away the value of the smaller), the ground is negative. Using x, Y, z three values to calculate the trigonometric functions, you can accurately detect the mobile phone's state of motion.
So theoretically:
The value of (x, Y, z) is (0,0,10) when the mobile screen is horizontally placed upward (Z axis);
The value of (x, Y, z) is (0,0,-10) when the mobile screen is horizontally placed downward (z-axis);
The value of (x, Y, z) is (10,0,0) when the screen of the phone is placed to the left.
When the cell phone is upright (y-axis) upward, the (x, y, Z) values are (0,10,0) respectively;
Source
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/textx"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center_horizontal"android:textsize= "40dip" /> <TextViewAndroid:id= "@+id/texty"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center_horizontal"android:textsize= "40dip" /> <TextViewAndroid:id= "@+id/textz"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center_horizontal"android:textsize= "40dip" /></LinearLayout>
Packagecom.example.layout;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.hardware.Sensor;Importandroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classMainactivityextendsactionbaractivity {PrivateSensormanager sensormgr; Sensor sensor; TextView TEXTX=NULL; TextView texty=NULL; TextView Textz=NULL; Private floatx, y, Z; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Sensormgr=(Sensormanager) Getsystemservice (Sensor_service); Sensor=sensormgr.getdefaultsensor (Sensor.type_accelerometer); TEXTX=(TextView) Findviewbyid (R.ID.TEXTX); Texty=(TextView) Findviewbyid (r.id.texty); Textz=(TextView) Findviewbyid (R.ID.TEXTZ); Sensoreventlistener LSN=NewSensoreventlistener () { Public voidonsensorchanged (sensorevent e) {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); Textx.settext ("X=" + (int) x); Texty.settext ("Y=" + (int) y); Textz.settext ("Z=" + (int) (z); } Public voidOnaccuracychanged (Sensor S,intaccuracy) { } }; //Register Listener, the third parameter is the accuracy of the detectionSensormgr.registerlistener (LSN, sensor, sensormanager.sensor_delay_game); }}
The actual results look at
Level on the table:
Standing on the table:
Side standing on the table:
To place randomly:
That's clear.
"Android Development Learning note", "Nineth lesson" Gravity sensing