(Create Compass app using sensor)

Source: Internet
Author: User

Because the real machine is rotten, after acquiring all the available sensors there is no direction sensor and no magnetic field sensor, so it was aborted and therefore not fully developed, but according to some things in the document added the process of using sensor,

First set up a getter in CustomView, which is to get Rotateangle:

     Public void setrotateangle (float  rotateangle) {        this. Rotateangle= Rotateangle ;        Invalidate ();    }

This allows us to set the rotation by calling this method of view externally:

To set up the sensor in mainactivity:

1  Packagecom.example.androidviewtest;2 3 Importandroid.app.Activity;4 ImportAndroid.hardware.Sensor;5 Importandroid.hardware.SensorEvent;6 ImportAndroid.hardware.SensorEventListener;7 ImportAndroid.hardware.SensorManager;8 ImportAndroid.os.Bundle;9 Ten ImportCom.example.androidviewtest.view.CustomView; One  A  -  Public classMainactivityextendsActivityImplementssensoreventlistener{ -  the     PrivateSensormanager Msensormanager; -     PrivateSensor msensor; -  - CustomView View1; + CustomView View2; - CustomView view3; +  A @Override at     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -  -View1 =(CustomView) Findviewbyid (r.id.view1); -View2 =(CustomView) Findviewbyid (R.ID.VIEW2); inVIEW3 =(CustomView) Findviewbyid (R.ID.VIEW3); -  toMsensormanager =(Sensormanager) Getsystemservice (sensor_service); +Msensor =msensormanager.getdefaultsensor (sensor.type_orientation); -     } the  * @Override $     protected voidOnStart () {Panax Notoginseng         Super. OnStart (); -Msensormanager.registerlistener ( This, msensor,sensormanager.sensor_delay_ui); the     } +  A @Override the     protected voidOnPause () { +         Super. OnPause (); -Msensormanager.unregisterlistener ( This); $     } $  - @Override -      Public voidonsensorchanged (Sensorevent event) { the         float[] values =event.values; -View1.setrotateangle (values[0]);WuyiView2.setrotateangle (values[1]); theView2.setrotateangle (values[2]); -     } Wu  - @Override About      Public voidOnaccuracychanged (sensor sensor,intaccuracy) { $  -     } -}

I will not continue this view of the study, the next stage to understand the use and implementation of Circleimageview.

(Create Compass app using 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.