Android Compass-core code

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.