how the 1.android coordinate system defines the x, y Z axis
The direction of the x-axis is the horizontal direction of the screen from left to right, if the cell phone is not a square, the shorter side needs to be placed horizontally, the longer side needs to be placed vertically.
The y-axis is directed from the bottom left corner of the screen to the top of the screen along the vertical direction of the screen.
Put the phone on the table, the direction of the z axis from the phone pointing to the sky.
2. Direction sensor
The 3 values of the value variables in the directional sensor represent degrees, and their meanings are as follows:
VALUES[0]: This value represents the orientation, which is the angle at which the phone rotates around the z axis. 0 represents northern (north), 90 for East (eastern), 180 for South (South), and 270 for West (western). If the value of values[0] is exactly these 4 values, and the cell phone is placed horizontally, it means that the phone is in front of the 4 directions. This feature can be used to achieve the electronic compass, example 76 will detail the implementation of the electronic compass process.
VALUES[1]: This value indicates the inclination, or the extent to which the phone is tilted. The value changes when the phone is tilted around the x axis. The value range of values[1] is -180≤values[1]≤180.
If you put your phone screen up on the table, at this point, if the table is completely horizontal, the value of values[1] should be 0 (since very few tables are absolutely horizontal, the value is probably not 0, but typically a value between 5 and 5). Lift it from the top of the phone until you rotate the phone 180 degrees along the x axis (the screen is horizontally on the desktop). In this rotation, values[1] will change between 0 and 180, that is to say, when lifted from the top of the phone, the value of values[1 will gradually become smaller until it equals 180. If you start at the bottom of your phone until you rotate the phone 180 degrees along the x axis, values[1 will change between 0 and 180. The value of values[1] increases gradually until it equals 180. You can use Values[1] and the values[2 described below to measure the tilt of objects such as tables.
VALUES[2]: Indicates the mobile phone's scrolling angle along the y axis. The value range is -90≤values[2]≤90. Suppose you put your phone screen up on the desktop, and if the desktop is flat, the value of values[2] should be 0. When the left side of the phone is gradually raised, the value of values[2] gradually becomes smaller until the phone is placed perpendicular to the desktop, when the value of values[2] is 90. When the right side of the phone is gradually raised, the value of values[2] gradually increases until the phone is placed perpendicular to the desktop, when the value of values[2] is 90. Continue scrolling to the right or left at the vertical position, the value of values[2] continues to change between 90 and 90.
Here is an example of how to apply it
Copy Code code as follows:
Package com.example.sensortest;
Import java.util.List;
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.util.Log;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;
public class Sensortest extends activity implements sensoreventlistener{
Private Sensormanager Sensormanager = null;
Private Sensor gyrosensor = null;
Private TextView VX;
Private TextView VY;
Private TextView VZ;
Private TextView V;
Private button button;
Private static final float ns2s = 1.0f/1000000000.0f;
private float timestamp;
Private float[] angle = new FLOAT[3];
@SuppressWarnings ("deprecation")
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_sensor_test);
VX = (TextView) Findviewbyid (R.ID.VX);
VY = (TextView) Findviewbyid (R.id.vy);
VZ = (TextView) Findviewbyid (R.ID.VZ);
v = (TextView) Findviewbyid (R.ID.V);
Button = (button) Findviewbyid (R.id.button);
Sensormanager = (Sensormanager) getsystemservice (Sensor_service);
GyroSensor = Sensormanager
. Getdefaultsensor (Sensor.type_orientation);
Vx.settext ("!!!!!!");
Button.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Declaring a variable string
StringBuffer sb = new StringBuffer ();
Get all the sensors on the phone
list<sensor> sensors = sensormanager.getsensorlist (Sensor.type_all);
Iterative output to obtain the sensor on
for (Sensor sensor:sensors) {
System.out.println (Sensor.getname (). toString ());
Sb.append (Sensor.getname (). toString ());
Sb.append ("\ n");
LOG.I ("Sensor", Sensor.getname (). toString ());
}
Assign a value to a text control
V.settext (Sb.tostring ());
}
});
}
Public Sensortest () {
TODO auto-generated Constructor stub
Angle[0] = 0;
ANGLE[1] = 0;
ANGLE[2] = 0;
timestamp = 0;
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (r.menu.activity_sensor_test, menu);
return true;
}
@Override
protected void OnPause () {
TODO auto-generated Method Stub
Super.onpause ();
Sensormanager.unregisterlistener (this); Remove Listener Registration
}
@Override
protected void Onresume () {
TODO auto-generated Method Stub
Super.onresume ();
Sensormanager.registerlistener (This, gyrosensor,
Sensormanager.sensor_delay_normal); Registering listeners for a sensor
}
@Override
public void onaccuracychanged (Sensor Sensor, int accuracy) {
TODO auto-generated Method Stub
}
@Override
public void Onsensorchanged (Sensorevent event) {
TODO auto-generated Method Stub
if (event.accuracy = = sensormanager.sensor_status_unreliable)
// {
Return
// }
if (timestamp!= 0) {
Final float DT = (event.timestamp-timestamp) * NS2S;
Angle[0] + = event.values[0] * DT * 100;
ANGLE[1] + = event.values[1] * DT * 100;
ANGLE[2] + = event.values[2] * DT * 100;
// }
timestamp = Event.timestamp;
//
//
Vx.settext ("X:" + float.tostring (angle[0));
Vy.settext ("Y:" + float.tostring (angle[1));
Vz.settext ("Z:" + float.tostring (angle[2));
The directional sensor provides three data, namely azimuth, pitch and roll.
//
Azimuth: Azimuth, returns the angle between the magnetic north and the Y axis at a horizontal time, ranging from 0° to 360°.
0°= North, 90°= East, 180°= South, 270°= West.
//
The angle between the pitch:x axis and the horizontal plane, ranging from -180° to 180°.
When the z axis rotates to the y-axis, the angle is positive.
//
The angle between the roll:y axis and the horizontal plane, for historical reasons, ranges from -90° to 90°.
The angle is positive when the x axis moves to the z-axis.
Vx.settext ("Orientation X:" + event.values[0]);
Vy.settext ("Orientation Y:" + event.values[1]);
Vz.settext ("Orientation Z:" + event.values[2]);
}
}
The layout files are as follows:
Copy Code code as follows:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Sensortest "
android:orientation= "Vertical"
>
<button
Android:id= "@+id/button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Get Sensor"
/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/v"
Android:textsize= "30px"
></TextView>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/vx"
Android:textsize= "50px"
/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/vy"
Android:textsize= "50px"
/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/vz"
Android:textsize= "50px"
/>
</LinearLayout>