The Compass for Android sensor development

Source: Internet
Author: User

The code is very simple, mainly some common sensor monitoring, the compass is very useful.


Layout code

<?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:background= "#fff" ><imageviewandroid:id= "@+id/znzimage" android:layout_width= "Fill_parent" Android: layout_height= "Fill_parent" android:scaletype= "Fitcenter" android:src= "@drawable/compass"/></linearlayout >



Program code:

Package Zhinanzheng.com;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;/** * @author Coderwry [email protected] * @version 1.0 */public class Zhinanzheng E  Xtends Activity implements Sensoreventlistener{imageview image; Compass Picture float currentdegree = 0f; Compass picture turned angle sensormanager msensormanager; Manager/** Called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.main);        Image = (ImageView) Findviewbyid (r.id.znzimage); Msensormanager = (Sensormanager) getsystemservice (Sensor_service);  Get Management Service} @Override protected void Onresume () {super.onresume ();  Register Listener Msensormanager.registerlistener (this, msensormanager.getdefaultsensor (sensor.type_orientation), SensorMan Ager.    Sensor_delay_game);    }//unregister @Override protected void OnPause () {Msensormanager.unregisterlistener (this);        Super.onpause ();    } @Override protected void OnStop () {Msensormanager.unregisterlistener (this);        Super.onstop (); }//sensor value changed @overridepublic void onaccuracychanged (sensor sensor, int accuracy) {//TODO auto-generated method stub}/ /accuracy Change @overridepublic void onsensorchanged (Sensorevent event) {//TODO auto-generated method stub//get sensor type to trigger event int SensorType = Event.sensor.getType (); switch (sensortype) {case Sensor.TYPE_ORIENTATION:float degree = event.values[0]; /Get z-turn angle//wear pieces rotate animation rotateanimation ra = new Rotateanimation (currentdegree,-degree,animation.relative_to_self,0.5f, animation.relative_to_self,0.5f); Ra.setduration (100);//Animation duration image.startanimation (RA); Currentdegree =-degree; Break;}}}


The Compass for Android sensor development

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.