Android_ Direction Sensor

Source: Internet
Author: User

Android Direction sensor small case, the main code is as follows:

 Packagecom.hb.direction;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.hardware.Sensor;Importandroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;Importandroid.view.animation.Animation;Importandroid.view.animation.RotateAnimation;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {PrivateSensormanager SM; PrivateImageView iv_compress; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Iv_compress=(ImageView) Findviewbyid (R.id.iv_compass); SM=(Sensormanager) Getsystemservice (Context.sensor_service); //Ground magnetic fieldSensor Magnetic =sm.getdefaultsensor (Sensor.type_magnetic_field); //AccelerationSensor Acceleromter =sm.getdefaultsensor (Sensor.type_accelerometer);        Sm.registerlistener (Listener, magnetic, sensormanager.sensor_delay_game);    Sm.registerlistener (Listener, Acceleromter, sensormanager.sensor_delay_game); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); if(sm!=NULL) {Sm.unregisterlistener (listener); }    }    PrivateSensoreventlistener listener=NewSensoreventlistener () {float[] magneticvalues=New float[3]; float[] acceleromtervalues=New float[3]; Private floatLastrotatedegree; @Override Public voidonsensorchanged (Sensorevent event) {//determine whether the current is an accelerometer or a ground field sensor            if(Event.sensor.getType () = =sensor.type_accelerometer) {Acceleromtervalues=Event.values.clone (); }Else if(Event.sensor.getType () = =Sensor.type_magnetic_field) {Magneticvalues=Event.values.clone (); }            float[] r=New float[9]; float[] values=New float[3]; Sensormanager.getrotationmatrix (R,NULL, Acceleromtervalues, magneticvalues);            Sensormanager.getorientation (R, values); //todegreess conversion to degrees//Tv_direction.settext ("Values is" +math.todegrees (values[0));//Toast.maketext (mainactivity.this, "Values is" +math.todegrees (Values[0]), 0). Show (); //reverse the calculated rotation angle to the compass background            floatrotetedegree=-(float) Math.todegrees (values[0]); if((Math.Abs (rotetedegree)-lastrotatedegree) >1) {rotateanimation animation=Newrotateanimation (Lastrotatedegree, Rotetedegree,animation.relative_to_self,0.5f,animation.relative_to_self,0.5f); Animation.setfillafter (true);                Iv_compress.startanimation (animation); Lastrotatedegree=Rotetedegree; }} @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy)        {        }    }; }

Layout file xml:

1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5tools:context= "${relativepackage}.${activityclass}" >6 7<ImageView8Android:id= "@+id/iv_arrow"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" OneAndroid:layout_centerinparent= "true" Aandroid:src= "@drawable/arrow3"/> -  -<ImageView theAndroid:id= "@+id/iv_compass" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -Android:layout_centerinparent= "true" +android:src= "@drawable/compass"/> -  +</RelativeLayout>

Source: Http://pan.baidu.com/s/1i4HIMSP

Android_ Direction Sensor

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.