Android uses a direction sensor to obtain the relative angle of the mobile phone. Example description:

Source: Internet
Author: User

1. How does the android coordinate system define the x and y z axes?

The X axis direction is from left to right along the horizontal direction of the screen. If the mobile phone is not a square, the shorter side needs to be placed horizontally, and the longer side needs to be placed vertically.

The Y-axis direction is from the lower left corner of the screen along the vertical direction of the screen to the top of the screen.
Place the mobile phone on the desk, and the Z axis is directed from the mobile phone to the sky.

2. Orientation Sensor
The three values of the values variable in the direction sensor indicate the degree. Their meanings are as follows:
Values [0]: This value indicates the orientation, that is, the angle from which the mobile phone rotates around the Z axis. 0 indicates North (North); 90 indicates East (East); 180 indicates South (South); 270 indicates West (West ). If the value of values [0] is exactly the four values, and the mobile phone is placed horizontally, it indicates that the front of the mobile phone is the four directions. You can use this feature to implement the electronic compass. instance 76 will detail the implementation process of the Electronic Compass.
Values [1]: This value indicates the inclination or the mobile phone climbing. This value changes when the phone is tilted around the X axis. The value range of values [1] is-180 ≤ values [1] ≤ 180.

Assume that the screen of the mobile phone is placed horizontally on the table. If the table is completely horizontal, the value of values [1] should be 0 (since few tables are absolutely horizontal, this value may not be 0, but is generally a value between-5 and 5 ). At this time, the phone is raised from the top of the phone until it is rotated 180 degrees along the X axis (the screen is horizontally placed on the desktop ). During this rotation process, values [1] will change between 0 and-180. That is to say, when the value of values [1] is lifted from the top of the mobile phone, it will gradually decrease, until it is equal to-180. If the mobile phone is raised from the bottom until it is rotated 180 degrees along the X axis, then values [1] will change between 0 and 180. That is, the value of values [1] will gradually increase until it is equal to 180. You can use values [1] and the values [2] to measure the inclination of objects such as tables.

Values [2]: indicates the scroll angle of the mobile phone along the Y axis. The value range is-90 ≤ values [2] ≤ 90. Assume that the screen of the mobile phone is placed horizontally on the desktop. 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 decreases until the phone is placed perpendicular to the desktop. Then, the value of values [2] is-90. When the right side of the phone is gradually raised, the value of values [2] increases gradually until the phone is placed perpendicular to the desktop, then the value of values [2] is 90. Scroll to the right or left when the vertical position is specified. The value of values [2] will continue to change between-90 and 90.

The following uses an example to describe its application method.
Copy codeThe Code is 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/Optional bytes 0.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
. Getdefasensensor (Sensor. TYPE_ORIENTATION );
VX. setText ("!!!!!! ");
Button. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
// Declare a variable string
StringBuffer sb = new StringBuffer ();
// Obtain all the sensors on the mobile phone
List <Sensor> sensors = sensorManager. getSensorList (Sensor. TYPE_ALL );
// Sensor obtained by iterative output
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 the 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); // cancel listener Registration
}
@ Override
Protected void onResume (){
// TODO Auto-generated method stub
Super. onResume ();
SensorManager. registerListener (this, gyroSensor,
SensorManager. SENSOR_DELAY_NORMAL); // registers the listener for the 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 direction sensor provides three data types: azimuth, pitch, and roll.
//
// Azimuth: returns the angle between the magnetic pole and the Y axis at the normal time, ranging from 0 ° to 360 °.
// 0 ° = North, 90 ° = east, 180 ° = South, 270 ° = West.
//
// Pitch: the angle between the X axis and the horizontal plane. The range is-180 ° to 180 °.
// When the Z axis rotates towards the Y axis, the angle is positive.
//
// Roll: the angle between the Y axis and the horizontal plane. The range is-90 ° to 90 ° due to historical reasons.
// When the X axis moves toward the Z axis, the angle is positive.

VX. setText ("Orientation X:" + event. values [0]);
VY. setText ("Orientation Y:" + event. values [1]);
VZ. setText ("Orientation Z:" + event. values [2]);

}
}

The layout file is as follows:Copy codeThe Code is 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 = "getting sensors"
/>
<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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.