Android Simple step using directional sensor to implement compass

Source: Internet
Author: User
Tags xmlns

Step 1: Create a new project compass and import a compass picture into the res/drawable-hdpi directory

Step 2: Design the UI interface for the application, Main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=  
"http://schemas.android.com/apk/" Res/android "
    android:orientation=" vertical "
    android:layout_width=" fill_parent "
    android:layout_" height= "Fill_parent"
    android:gravity= "center"
    >  
<imageview
    android:layout_width= "WRAP_" Content "
    android:layout_height=" wrap_content "
    android:src=" @drawable/compass "
    android:id=" @+id/ ImageView "
    />  
</LinearLayout>

Step 3:mainactivity.java

Import android.app.Activity;  
Import Android.content.Context;  
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 {private ImageView ImageView;  
      
    /** Sensor Manager */private Sensormanager manager;  
      
    Private Sensorlistener listener = new Sensorlistener ();  
        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  
      
        Setcontentview (R.layout.main);  
        ImageView = (ImageView) This.findviewbyid (R.id.imageview); Imageview.setkeepscreenon (TRUE);//screen highlighting//Get system Services (Sensor_service) returns a Sensormanager object manager = (sensorm Anager) GetsystemservIce (Context.sensor_service); @Override protected void Onresume () {/** * get direction sensor * through Sensormanager object  
        Gets the corresponding Sensor type of object/Sensor Sensor = Manager.getdefaultsensor (sensor.type_orientation);  
        Application in the foreground when registering listener manager.registerlistener (listener, sensor, sensormanager.sensor_delay_game);  
    Super.onresume (); @Override protected void OnPause () {//application is not destroyed at the front of the listener Manager.unregisterlistener (li  
        Stener);  
    Super.onpause (); Private Final class Sensorlistener implements Sensoreventlistener {private float Predegre  
      
        e = 0; @Override public void onsensorchanged (Sensorevent event) {/** * Values[0]: X-axis direction Acceleration Values[1]: Y-axis direction acceleration values[2]: Z-axis direction acceleration/float deg REE = event.values[0];//Store the directional value/** animation effect * * rotateanimation animation = new Rotateanimation (Predegree, Degree,  
            animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);  
            Animation.setduration (200);  
            Imageview.startanimation (animation);  
                  
            Predegree=-degree; 
            /** float x=event.values[sensormanager.data_x]; 
            float y=event.values[sensormanager.data_y]; 
            float Z=event.values[sensormanager.data_z]; 
            LOG.I ("XYZ", "x=" + (int) x+ ", y=" + (int) y+ ", z=" + (int) z);  
      
        */} @Override public void onaccuracychanged (Sensor Sensor, int accuracy) { }  
      
    }  
}

Step 4:androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=  
"http://schemas.android.com/apk/res/" Android "
      package=" cn.roco.sensor "
      android:versioncode=" 1 "
      android:versionname=" 1.0 ">  
    < USES-SDK android:minsdkversion= "8"/>  
      
    <application android:icon= "@drawable/icon" android:label= "@string App_name ">  
        <activity android:name=" mainactivity "
                  android:label=" @string/app_name ">  
            < intent-filter>  
                <action android:name= "Android.intent.action.MAIN"/>  
                <category "Android.intent.category.LAUNCHER"/>  
            </intent-filter>  
        </activity>  
      
    </ Application>  
</manifest>

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.