Develop a compass using Android Sensors

Source: Internet
Author: User

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 implements SensorEventListener {// 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; @ Override protected 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) ;}@ Override protected void onResume () {super. onResume (); // registers the listener mSensorManager for the system's sensor. registerListener (this, mSensorManager. getdefasensensor (Sensor. TYPE_ORIENTATION), SensorManager. SENSOR_DELAY_GAME) ;}@ Override protected void onPause () {super. onPause (); // cancel mSensorManager registration. unregisterListener (this) ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {}@ Override public 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 ;}} 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 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>  <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> here is an image of the compass:

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.