As mentioned above, the first parameter value passed back by the horizontal sensor represents the angle from which the mobile phone rotates around the Z axis, that is, the angle between the top of the mobile phone and the north. In the program, you can check the angle to implement the compass application. In fact, the idea is very simple. First prepare an image, which directs the pointer to the north. Then, a sensor is developed to detect the direction. When the program detects how many degrees the camera turns around the Z axis on the top of the mobile phone, the number of degrees the picture of the compass is reversed. In this way, the pointer is always directed to the north. This is also the principle of the compass. The Code is as follows:
Activity:
Package COM. home. compass; 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. view. animation. animation; import android. view. animation. rotateanimation; import android. widget. imageview; public class compasstestactivity extends activity implementssensoreventlistener {// define the private imageview image component that displays the compass image; // record the angle from which the compass image is rotated private float currentdegree = 0f; // define the sensor manager private sensormanager msensormanager of the real machine; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); image = (imageview) findviewbyid (R. id. main_iv); // obtain the Sensor Management Service msensormanager = (sensormanager) getsystemservice (sensor_service) ;}@ overrideprotected void onresume () {super. onresume (); // registers the listener msensormanager for the system's sensor. registerlistener (this, msensormanager. getdefasensensor (sensor. type_orientation), sensormanager. sensor_delay_game) ;}@ overrideprotected void onpause () {super. onpause (); // cancel msensormanager registration. unregisterlistener (this) ;}@ overridepublic void onaccuracychanged (sensor, int accuracy) {}@ overridepublic void onsensorchanged (sensorevent event) {// if the sensor type of the event triggered on the real machine is level sensor type if (event. sensor. getType () = sensor. type_orientation) {// obtain the angle float degree = event. values [0]; // create a rotation animation (reverse turn degree) rotateanimation Ra = new rotateanimation (currentdegree,-degree, animation. relative_to_self, 0.5f, animation. relative_to_self, 0.5f); // sets the animation duration Ra. setduration (200); // sets the retained state Ra after the animation ends. setfillafter (true); // start the animated image. startanimation (RA); currentdegree =-degree ;}}}
Layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <ImageView android:id="@+id/main_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/znz" /></LinearLayout>
A picture of the compass is attached here: