How to Use Rotation Sensor in Android -
I have a ball (bitmap) in the center of the black surface.
I want to leave that ball on the screen, if we tilt the phone to the left, for the right side of the same way.
(Example: - We control the bike in any bike racing Android game.)
Which sensor to use and how to get the value of it.
Code yet ......
Public category ABC applied sensor event listener {public ABC (reference reference) {sensormanager = (sensor manager) reference .getSystemService (Context.SENSOR_SERVICE); Sensor = sensor .get default sensor (sensor.Tep_ACCLARMAMMR); SensorMonor.RestisterListore (this, sensor, sensor manager.SENSOR_DELAY_GAME); } Some code overwrite invalidity on public invalidity @SonorChanged (SensorEvent arg0) {}} You have to apply your code under the rectification.
View example here:
@ @SensorChanged @ override public over zero {float [] value = arg0.values; // X, Y, Z axis, from float x = value [0]; Float y = value [1]; Float z = value [2]; }
Every time the sensor detects movement, you get new values under the values [] array
onResume ()
It is a good practice to register the listener under and unemployed under onPause ()
, this is the way:
@ Override Resume at zero protected ) {Super.onResume (); // Register listener sensor. The Manager. Registry (this, sensor manager .getDefaultSensor (sensor.TYPE_ACCELEROMETER), sensor manager.SENSOR_DELAY_NORMAL); } @ Override Protected Zero at Pause () {// Listener Unchecked Super. Ultimate (); SensorManager.unregisterListener (this); }
Hope it has helped.
Comments
Post a Comment