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)