Android Basics Getting Started Tutorial--10.11 sensor Special (2)--direction sensor
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
In the previous section we learned about some of the basic concepts of sensors, and learned about the use of sensor routines,
The sensor that this section brings to you is the use of the direction sensor, OK, start the content of this section ~
1. The concept of three-dimensional coordinate system:
In the Android platform, the sensor framework typically uses a standard three-dimensional coordinate system to represent a value. In this section
To speak of the direction of the sensor as an example, to determine a direction also requires a three-dimensional coordinate, after all, our equipment can not forever
It's all horizontal. The direction value returned by Android is a flaot array of length 3, containing three directions
The value! The official API document has such a diagram: Sensors_overview
If you do not understand the picture, then write down the words to explain:
x-axis direction : From left to right along the horizontal screen, if the phone is not a square, the shorter side needs a level
placed, the longer edges need to be placed vertically.
y-axis direction : from the lower left corner of the screen to the top of the screen along the vertical direction of the screen
direction of Z axis : When horizontally placed, the direction of the sky
2. Three values for the direction sensor
As mentioned in the previous section, the sensor's callback method: onsensorchanged parameter Sensorevent event,event
The value type is float[], and there are at most only three elements, and the directional sensor has just three elements, all of which represent degrees!
The corresponding meanings are as follows:
Values[0]: Azimuth, the angle at which the phone rotates around the z axis. 0 means north, 90 is East
180 means South, and 270 means due west (West). If the value of values[0] is exactly the four values,
And the mobile phone along the horizontal, then the current phone front is the four directions, you can use this to
Write a compass!
values[1]: tilt angle, the extent of the phone up, when the phone around the x-axis tilt when the value will change. Take value
The range is between [-180,180]. If you put your phone on the desktop and the desktop is completely horizontal, the values1 should
is 0, and of course very few tables are absolutely level. lift up from the top of the phone until the phone rotates 180 along the x-axis (this screen
Country level on the desktop). During this rotation, the value of values[1] changes from 0 to -180 , which means that the phone is lifted
, the value of the values1 is gradually smaller, knowing that it is equal to-180, while the join starts from the bottom of the phone and lifts until the phone along the x-axis
Rotates 180 degrees, at which point the value of values[1] changes from 0 to . We can use this feature of value[1] to combine
value[2] to achieve a leveling ruler!
value[2]: scrolling angle, along the y-axis of the rolling angle, the value range is: [ -90,90], assuming that the phone screen facing up horizontally in the
On the desktop, if the desktop is flat, the value of Values2 should be 0. Gradually lift the phone from the left side, the value of values[2] will
Gradually reduce, know perpendicular to the cell phone placement, at this time values[2] value is-90, from the right side is 0-90; join in the vertical position
When you continue scrolling right or left, the value of values[2] will continue to change between 90 and 90!
If you do not understand, it is ok we write a demo to verify that you know ~
3. A simple demo helps us understand the changes in these three values:
Run :
Implementation Code :
Layout code:activity_main.xml:
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:orientation="Vertical"android:padding="5DP"> <textview android:ID="@+id/tv_value1"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_margintop="10DP"Android:text="Bearing Angle"Android:textsize="18SP"android:textstyle="Bold"/> <textview android:ID="@+id/tv_value2"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_margintop="10DP"Android:text="Tilt Angle"Android:textsize="18SP"android:textstyle="Bold"/> <textview android:ID="@+id/tv_value3"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_margintop="10DP"Android:text="Scrolling angle"Android:textsize="18SP"android:textstyle="Bold"/></linearlayout>
Mainactivity.java:
Public class mainactivity extends appcompatactivity implements Sensoreventlistener { PrivateTextView tv_value1;PrivateTextView tv_value2;PrivateTextView Tv_value3;PrivateSensormanager Smanager;PrivateSensor msensororientation;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Smanager = (Sensormanager) getsystemservice (Sensor_service); Msensororientation = Smanager.getdefaultsensor (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 angle:"+ (float) (Math.Round (event.values[0] * -)) / -); Tv_value2.settext ("Tilt angle:"+ (float) (Math.Round (event.values[1] * -)) / -); Tv_value3.settext ("Scrolling angle:"+ (float) (Math.Round (event.values[2] * -)) / -); }@Override Public void onaccuracychanged(Sensor sensor,intaccuracy) {}}
The code is very simple ~, you want to really experience the change of these three values, run the next program to turn the phone to know ~
4. A simple version of the text Compass example
Let's write a simple text version of the compass to experience the experience, when the text shows South, the mobile phone
Right in front of the South!
Run :
Code Implementation :
Custom view:Compassview.java
/** * Created by Jay on 2015/11/14 0014. * * Public class compassview extends View implements Runnable { PrivatePaint Mtextpaint;Private intSwidth,sheight;Private floatDec =0.0FPrivateString msg ="North 0°"; Public Compassview(Context context) { This(Context,NULL); } Public Compassview(context context, AttributeSet attrs) {Super(context, attrs); Swidth = SCREENUTIL.GETSCREENW (context); Sheight = screenutil.getscreenh (context); Init ();NewThread ( This). Start (); } Public Compassview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }Private void Init() {Mtextpaint =NewPaint (); Mtextpaint.setcolor (Color.gray); Mtextpaint.settextsize ( -); Mtextpaint.setstyle (Paint.Style.FILL); }@Override protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); Canvas.drawtext (msg, swidth/4, Swidth/2, Mtextpaint); }//Update compass angle Public void Setdegree(floatdegree) {//Set sensitivity if(Math.Abs (Dec-degree) >=2) {dec = degree;intRange = A; String degreestr = string.valueof (dec);//Pointing to True north if(Dec > the-Range && Dec < the+ range) {msg ="True North"+ Degreestr +"°"; }//point to Zhengdong if(Dec > --Range && Dec < -+ range) {msg ="Zhengdong"+ Degreestr +"°"; }//Pointing South if(Dec > the-Range && Dec < the+ range) {msg ="South"+ Degreestr +"°"; }//point to due West if(Dec > the-Range && Dec < the+ range) {msg ="Due West"+ Degreestr +"°"; }//Pointing to the northeast if(Dec > $-Range && Dec < $+ range) {msg ="Tohoku"+ Degreestr +"°"; }//pointing southeast if(Dec >135-Range && Dec <135+ range) {msg ="Southeast"+ Degreestr +"°"; }//pointing southwest if(Dec >225-Range && Dec <225+ range) {msg ="Southwest"+ Degreestr +"°"; }//Pointing northwest if(Dec >315-Range && Dec <315+ range) {msg ="Northwest"+ Degreestr +"°"; } } }@Override Public void Run() { while(! Thread.CurrentThread (). isinterrupted ()) {Try{Thread.Sleep ( -); }Catch(Interruptedexception e) {Thread.CurrentThread (). interrupt (); } postinvalidate (); } }}
Mainactivity.java:
Public class mainactivity extends appcompatactivity implements Sensoreventlistener { PrivateCompassview CView;PrivateSensormanager Smanager;PrivateSensor msensororientation;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); CView =NewCompassview (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,intAccuracy) {}@Override protected void OnDestroy() {Super. OnDestroy (); Smanager.unregisterlistener ( This); }}
This is the prototype of a very simple compass, interested in drawing a compass and a pointer, and then implementing a
Good-looking compass ~
5. The sample code for this section is downloaded:
Sensordemo2.zip
Sensordemo3.zip
This section summarizes:
OK, this section introduces you to the most commonly used directional sensors in Android, as well as his simple usage, and
Write a compass example, and complete the compass we only use a values[0] value, using the other two
Value we can also be used to measure whether a place is lying flat, that is, the production of horizontal ruler, there is free to write a play ~
All right, here we go, thanks.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started Tutorial--10.11 sensor Special (2)--direction sensor