This time we have studied the development of Android sensors, previously introduced, tween use, so we can combine sensors with tween animation, develop a simple compass.
First of all, introduce the sensor knowledge,
in the the use of sensors in Android applications depends on the Android.hardware.SensorEventListener interface. This interface allows you to monitor various events of the sensor. The code for the Sensoreventlistener interface is as follows:
Package Android.hardware;public interface Sensoreventlistener {public void Onsensorchanged (Sensorevent event); public void onaccuracychanged (sensor sensor, int accuracy); }
in theSensoreventlistenertwo methods are defined in the interface:onsensorchangedand theonaccuracychanged. Onsensorchanged when the value of the sensor is changed. When the accuracy of the sensor changes, it is calledonaccuracychangedmethod.
onsensorchangedmethod has only onesensoreventTypes of ParametersEvent, wheresensoreventclass has aValuesvariable is very important, the type of the variable isfloat[]. However, the variable is only3elements, and depending on the sensor,Valuesthe elements in a variable represent different meanings. we use the direction sensor as an example to illustrate the meaning of value:Values[0]: This value represents the azimuth, that is, the angle at which the phone rotates around the Z axis. 0 indicates north (n), and the "n" indicates the Eastern (East); The expression is West.
VALUES[1]: This value indicates the tilt, or the extent to which the phone is tilted. The value changes when the phone is tilted around the X-axis. Values[1] range of values is ----------+≤values[1]<180
values[2]: Indicates the scrolling angle of the phone along the Y-axis. The value range is -values[2≤]≤
Here we illustrate the use of the case with examples,
Layout file:
<imageview android:id= "@+id/iv" android:layout_width= "wrap_content" android:layout_height= "wrap _content " android:src=" @drawable/point "/> <textview android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:textsize= "50DP"/> <textview android:id= "@+id/tv " android:layout_width=" fill_parent " android:layout_height=" wrap_content " android:textsize=" 20DP "/>
One of the pictures is a random look at Baidu in a position of the picture,
Then there is the implementation in the activity:
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); IV = (ImageView) Findviewbyid (R.ID.IV); TV = (TextView) Findviewbyid (r.id.tv); Sensormanager = (Sensormanager) getsystemservice (Sensor_service); Get direction Sensor light sensor sensor.type_light value[0], representing light intensity sensor sensor = Sensormanager.getdefaultsensor (Sensor.type_orienta tion); Listener = new MyListener (); Sensormanager.registerlistener (listener, sensor, sensormanager.sensor_delay_game); } Private class MyListener implements sensoreventlistener{float startangle = 0; When sensor data changes, @Overridepublic void onsensorchanged (Sensorevent event) {//TODO auto-generated method stubfloat[] values = Ev Ent.values;float angle = values[0]; System.out.println ("From the angle of True north:" +angle); Rotateanimation ra = new Rotateanimation (startangle, angle,animation.relative_to_self,0.5f,animation.relative_to_sRa.setduration (elf,0.5f), Iv.startanimation (RA), Tv.settext ("The angle of the North direction is:" +angle); Tv.settextcolor (Color.Black ); startangle =-angle;} Sensor accuracy changes when @overridepublic void onaccuracychanged (sensor sensor, int accuracy) {//TODO auto-generated method stub} } @Override protected void OnDestroy () {//prevents the program from running in the background, consumes memory, and frees resources when the program exits. Sensormanager.unregisterlistener (listener); Sensormanager = null; Super.ondestroy (); }
Finally, when the direction of our mobile phone changes, the picture is also moving, and the following text box, the corresponding azimuth value will be displayed.
Android Learning note-sensor development using sensor and tween development simple compass