Android-driven gyroscope code and Android-based gyroscope code

Source: Internet
Author: User

Android-driven gyroscope code and Android-based gyroscope code

 

Android-driven gyroscope code:

Source http://www.cnblogs.com/xiaobo-Linux/ qq463431476

Package zcd. functions; import zcd. netanything. r; import android. app. fragment; import android. content. context; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; import android. OS. bundle; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import Android. widget. textView; public class VR extends Fragment implements SensorEventListener {private SensorManager sensorManager; private Sensor magneticSensor; private TextView showTextView; private Sensor accelerometerSensor; private Sensor gyroscopeSensor; // convert the nanoseconds to private static final float NS2S = 1.0f/100001_0.0f; private float timestamp; private float angle [] = new float [3]; public View o NCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stub View view = inflater. inflate (R. layout. vr, container, false); showTextView = (TextView) view. findViewById (R. id. text); return view;} public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); sensorManager = (SensorManager) getActivity (). getSystemService (Context. SENSOR_SERVICE); magneticSensor = sensorManager. getdefasensensor (Sensor. TYPE_MAGNETIC_FIELD); accelerometerSensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER); gyroscopeSensor = sensorManager. getdefasensensor (Sensor. TYPE_GYROSCOPE); // register the gyroscope sensor and set the time interval that the sensor outputs to the application to be SensorManager. SENSOR_DELAY_GAME (20000 microseconds) // SensorManager. SENSOR_DELAY_FASTEST (0 microseconds): the fastest. The lowest latency is not recommended for processing especially sensitive data. This mode may consume a large amount of electricity on mobile phones, failure to handle litigation affects the game logic and UI performance // SensorManager. SENSOR_DELAY_GAME (20000 microseconds): game. Game latency. Generally, most games with high real-time performance use this level // SensorManager. SENSOR_DELAY_NORMAL (200000 microseconds): normal. Standard latency, which can be used for general games of the benefit or EASY level, but a low sampling rate may be frame-jumping for some racing games // SensorManager. SENSOR_DELAY_UI (60000 microseconds): user interface. Generally, it is used for automatic screen rotation, which saves power and logic processing. Generally, sensorManager is not used in game development. registerListener (this, gyroscopeSensor, SensorManager. SENSOR_DELAY_GAME); sensorManager. registerListener (this, magneticSensor, SensorManager. SENSOR_DELAY_GAME); sensorManager. registerListener (this, accelerometerSensor, SensorManager. SENSOR_DELAY_GAME);} // The coordinate axes indicate that the horizontal direction from the left to the right of the mobile phone is positive on the X axis, and that from the bottom to the top of the mobile phone is positive on the Y axis, vertical to the mobile phone screen up to the Z axis forward @ Override public void onSensorChanged (S EnsorEvent event) {if (event. sensor. getType () = Sensor. TYPE_ACCELEROMETER) {// x, y, and z respectively store the acceleration float x = event on the x, y, and z axes. values [0]; float y = event. values [1]; float z = event. values [2]; // The total acceleration value is a float a = (float) Math based on the acceleration in three directions. sqrt (x * x + y * y + z * z); System. out. println ("a ---------->" + a); // The time interval between sensors collecting data from the outside is 10000 microseconds. out. println ("magneticSensor. getMinDelay () --------> "+ magneticS Ensor. getMinDelay (); // maximum range of the accelerometer System. out. println ("event. sensor. getMaximumRange () --------> "+ event. sensor. getMaximumRange (); showTextView. setText ("x -->" + x + "\ ny -->" + y + "\ nz -->" + z);} else if (event. sensor. getType () = Sensor. TYPE_MAGNETIC_FIELD) {// electromagnetic intensity in the direction of the three coordinate axes, in the unit of micro-Tesla, expressed in uT, or Gaussian (Gauss ), 1 Tesla = 10000 Gauss float x = event. values [0]; float y = event. values [1]; float z = Event. values [2]; // The time interval for the mobile phone's magnetic field sensor to collect data from the outside is 10000 microseconds System. out. println ("magneticSensor. getMinDelay () --------> "+ magneticSensor. getMinDelay (); // maximum range of the magnetic sensor System. out. println ("event. sensor. getMaximumRange () -----------> "+ event. sensor. getMaximumRange (); System. out. println ("x ------------->" + x); System. out. println ("y ------------->" + y); System. out. println ("z ------------->" + z); // Log. d ("T AG "," x -------------> "+ x); // Log. d ("TAG", "y ------------>" + y); // Log. d ("TAG", "z ----------->" + z); // showTextView. setText ("x ---------->" + x + "\ ny -------------->" + // y + "\ nz ----------->" + z);} else if (event. sensor. getType () = Sensor. TYPE_GYROSCOPE) {// view the device in the original position from the forward position of the x, y, and z axes. if the device rotates counter-clockwise, it will receive a positive value. Otherwise, it is a negative value if (timestamp! = 0) {// obtain the time difference between the two detected mobile phone rotation (nanoseconds) and convert it to second final float dT = (event. timestamp-timestamp) * NS2S; // Add the Rotation angle of the mobile phone on each axis to get the arc angle of the current position relative to the initial position [0] + = event. values [0] * dT; angle [1] + = event. values [1] * dT; angle [2] + = event. values [2] * dT; // converts radians to degrees float anglex = (float) Math. toDegrees (angle [0]); float angley = (float) Math. toDegrees (angle [1]); float anglez = (float) Math. toDegrees (angle [2]); System. out. println ("anglex ------------>" + anglex); System. out. println ("angley ------------>" + angley); System. out. println ("anglez ------------>" + anglez); System. out. println ("gyroscopeSensor. getMinDelay () -----------> "+ gyroscopeSensor. getMinDelay ();} // assign the current time to timestamp = event. timestamp ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// TODO Auto-generated method stub} @ Override public void onPause () {// TODO Auto-generated method stub super. onPause (); sensorManager. unregisterListener (this );}}

 

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.