The compass has always been an indispensable and important role.
The idea of developing a compass is very simple. The program first prepares a picture of the compass so that the pointer on the picture points to the north, and then develops the sensor in the detection direction, the program detects the number of degrees at the top of the phone that bypasses the ZF axis, so that the picture of the compass can be reversed.
The main code is as follows:
Package COM. example. 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 mainactivity extends activity implements Sensoreventlistener {// define the image imageview znzimage for displaying the compass; // record the angle float currentdegree = 0f; // define the sensor manager sensormanager msensormanager of the real machine; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); system. out. println ("mainactivity. oncreate () "); setcontentview (R. layout. activity_main); // obtain the picture znzimage = (imageview) findviewbyid (R. id. znzimage); // gets the sensor management service of a real machine. Msensormanager = (sensormanager) getsystemservice (sensor_service) ;}@ overrideprotected void onresume () {super. onresume (); system. out. println ("mainactivity. onresume () "); // registers the listener msensormanager for the system's direction sensor. registerlistener (this, msensormanager. getdefasensensor (sensor. type_orientation), sensormanager. sensor_delay_game) ;}@ overrideprotected void onpause () {system. out. println ("mainactivity. onpause () "); // cancel Ms Registration Ensormanager. unregisterlistener (this); super. onpause () ;}@ overrideprotected void onstop () {system. out. println ("mainactivity. onstop () "); // cancel msensormanager registration. unregisterlistener (this); super. onstop ();} public void onsensorchanged (sensorevent event) {system. out. println ("mainactivity. onsensorchanged () "); // The sensor type int sensortype = event on the real machine. sensor. getType (); // obtain the sensor type switch (sensortype) that triggers the event on the simulator) {Case sensor. type_orientation: // obtain the angle of rotation around the Z axis. 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); // run the znzimage animation. startanimation (RA); currentdegree =-degree; break;} public void onaccuracychanged (sensor, int accuracy) {system. out. println ("mainactivity. onaccuracychanged ()");}}
Right, it's easy, not as hard as you think. In fact, it is based on the data processing animation sent back from the sensor. The first data that is passed back is the angle of rotation around the Z axis.