Basic tutorial for Android -- 10.11 sensor topic (2) -- orientation Sensor
Basic tutorial for Android -- 10.11 sensor topic (2) -- orientation Sensor
Tags (separated by spaces): basic Android tutorial
This section introduces:
In the previous section, we learned some basic concepts of sensors and how to use sensors,
This section describes how to use a direction sensor ~
1. Concept of three-dimensional coordinate system:
In Android, a sensor framework usually uses a standard three-dimensional coordinate system to represent a value. This section
The direction sensor is used as an example. It also requires a three-dimensional coordinate to determine a direction. After all, our devices cannot be forever.
It's all horizontally. The direction value Android returns is a 3-character flaot array, which contains three directions.
Value! The official API documentation has the following figure: sensors_overview.
If you do not understand the graph, write down the text explanation:
X axis direction: From left to right along the horizontal direction of the screen. If the phone is not a square, the shorter side needs to be horizontal.
Place. Longer edges must be placed vertically.
Y axis direction: Points vertically to the top of the screen from the lower left corner of the screen.
Z axis direction: Points to the sky when placed horizontally
2. Three values of the direction Sensor
As mentioned in the previous section, the callback method of the sensor is: the parameter SensorEvent event in onSensorChanged
The value type is Float [], and there are only three elements at most, while the direction sensor just has three elements, all representing the degree!
The corresponding meaning is as follows:
Values [0]:The azimuth angle. The mobile phone rotates around the Z axis. 0 indicates North, 90 indicates East ),
180 indicates South and 270 indicates West ). If the value of values [0] is exactly the four values,
If the mobile phone is placed horizontally, the front of the current mobile phone is in these four directions, which can be used
Write a compass!
Values [1]: Tilt angle, the degree to which the mobile phone is tilted. This value changes when the mobile phone is tilted around the X axis. Value
The value range is [-180,180. If the mobile phone is placed on the desktop while the desktop is completely horizontal, values1 should
It is 0. Of course, few tables are absolutely horizontal. Slave phoneThe top is lifted.Until the phone rotates 180 along the X axis (the screen at this time)
Country level on the desktop ). During this rotation, values [1The value0 to-180Between changes, that is, mobile phone lifting
The value of values1 gradually decreases, and the value is-180.Start lifting at the bottomUntil the phone goes along the X axis
Rotate 180 degrees. At this time, values [1] WillFrom 0 to 180. We can use value [1].
Value [2] To achieve a flat ruler!
Value [2]: Scroll angle, which is the scroll Angle Along the Y axis. value range: [-90, 90]. Assume that the mobile phone screen is placed horizontally upwards.
In this case, if the desktop is flat, the value of values2 should be 0. Gradually lift the phone from the left, values [2]
Gradually decrease, knowing that it is placed perpendicular to the mobile phone, then values [2The value is-90, and the value is 0-90 from the right.
Continue to the right or scroll left, values [2] Will continue to change between-90 and 90!
If you do not understand it very well, we will understand it after writing a demo for verification ~
3. A simple Demo helps us understand the changes of these three values:
Run:
Implementation Code:
Layout code:Activity_main.xml:
MainActivity. java:
Public class MainActivity extends AppCompatActivity implements extends {private TextView TV _value1; private TextView TV _value2; private TextView TV _value3; private SensorManager sManager; private Sensor extends; @ Override protected void onCreate (Bundle limit. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sManager = (SensorManager) getSystemService (SENSOR_SERVICE); mSensorOrientation = sManager. getdefasensensor (Sensor. TYPE_ORIENTATION); sManager. registerListener (this, mSensorOrientation, SensorManager. SENSOR_DELAY_UI); bindViews ();} private void bindViews () {TV _value1 = (TextView) findViewById (R. id. TV _value1); TV _value2 = (TextView) findViewById (R. id. TV _value2); TV _value3 = (TextView) findViewById (R. id. TV _value3) ;}@ Override public void onSensorChanged (SensorEvent event) {TV _value1.setText ("Azimuth:" + (float) (Math. round (event. values [0] * 100)/100); TV _value2.setText ("tilt angle:" + (float) (Math. round (event. values [1] * 100)/100); TV _value3.setText ("scroll corner:" + (float) (Math. round (event. values [2] * 100)/100);} @ Override public void onAccuracyChanged (Sensor sensor, int accuracy ){}}
The code is very simple ~, If you want to really experience the changes in these three values, You will know when you run the program and go to the mobile phone ~
4. A simple text compass example
Next we will write a simple text version of the compass to experience the experience, when the text shows the South, indicating the mobile phone
The front is the south!
Run:
Code Implementation:
Custom View:CompassView. java
/*** Created by Jay on 0014. */public class CompassView extends View implements Runnable {private Paint mTextPaint; private int sWidth, sHeight; private float dec = 0.0f; private String msg = "North 0 ° "; public CompassView (Context context) {this (context, null);} public CompassView (Context context, AttributeSet attrs) {super (context, attrs); sWidth = ScreenUtil. getScreenW (context); sHeigh T = ScreenUtil. getScreenH (context); init (); new Thread (this ). start ();} public CompassView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} private void init () {mTextPaint = new Paint (); mTextPaint. setColor (Color. GRAY); mTextPaint. setTextSize (64); mTextPaint. setStyle (Paint. style. FILL) ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (c Anvas); canvas. drawText (msg, sWidth/4, sWidth/2, mTextPaint);} // update the compass angle public void setDegree (float degree) {// set the sensitivity if (Math. abs (dec-degree)> = 2) {dec = degree; int range = 22; String degreeStr = String. valueOf (dec); // point to North if (dec> 360-range & dec <360 + range) {msg = "North" + degreeStr + "° ";} // point to East-East if (dec> 90-range & dec <90 + range) {msg = "East-East" + degreeStr + "° ";} // Point to the south-facing if (dec> 180-range & dec <180 + range) {msg = "south-facing" + degreeStr + "° ";} // point to zhengxi if (dec> 270-range & dec <270 + range) {msg = "zhengxi" + degreeStr + "° ";} // point to Northeast if (dec> 45-range & dec <45 + range) {msg = "Northeast" + degreeStr + "° ";} // point to the southeast if (dec> 135-range & dec <135 + range) {msg = "southeast" + degreeStr + "° ";} // point to the southwest if (dec> 225-range & dec <225 + range) {msg = "Southwest China" + degreeStr + "°";} // point to northwest China if (dec> 315-range & dec <315 + range) {msg = "Northwest" + degreeStr + "°" ;}}@ Override public void run () {while (! Thread. currentThread (). isInterrupted () {try {Thread. sleep (100);} catch (InterruptedException e) {Thread. currentThread (). interrupt () ;}postinvalidate ();}}}
MainActivity. java:
public class MainActivity extends AppCompatActivity implements SensorEventListener { private CompassView cView; private SensorManager sManager; private Sensor mSensorOrientation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cView = new CompassView(MainActivity.this); sManager = (SensorManager) getSystemService(SENSOR_SERVICE); mSensorOrientation = sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); sManager.registerListener(this, mSensorOrientation, SensorManager.SENSOR_DELAY_UI); setContentView(cView); } @Override public void onSensorChanged(SensorEvent event) { cView.setDegree(event.values[0]); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override protected void onDestroy() { super.onDestroy(); sManager.unregisterListener(this); }}
This is the prototype of a very simple compass. If you are interested, you can draw a compass and pointer on your own, and then implement
Nice-looking compass ~
5. Download the sample code in this section:
SensorDemo2.zip
SensorDemo3.zip
Summary:
Okay. This section describes the most commonly used direction sensors in Android, their simple usage, and
I wrote an example of a compass. To complete the compass, we only use the value of one values [0] and use the other two
The value can also be used to determine whether a certain place is lying down, that is, to make a horizontal ruler. If you are free, you can write a horizontal ruler for fun ~
Okay. Here we are. Thank you ~
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.