"device rotation sensor android examples" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "device rotation sensor android examples" answered properly. Developers are finding an appropriate answer about device rotation sensor android examples related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like device rotation sensor android examples. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on device rotation sensor android examples.
device rotation sensor android examples
MainActivity.java
package com.authorwjf.pitchandroll;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mRotationSensor;
private static final int SENSOR_DELAY = 500 * 1000; // 500ms
private static final int FROM_RADS_TO_DEGS = -57;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mSensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
mSensorManager.registerListener(this, mRotationSensor, SENSOR_DELAY);
} catch (Exception e) {
Toast.makeText(this, "Hardware compatibility issue", Toast.LENGTH_LONG).show();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mRotationSensor) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
update(truncatedRotationVector);
} else {
update(event.values);
}
}
}
private void update(float[] vectors) {
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
int worldAxisX = SensorManager.AXIS_X;
int worldAxisZ = SensorManager.AXIS_Z;
float[] adjustedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
float[] orientation = new float[3];
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
float pitch = orientation[1] * FROM_RADS_TO_DEGS;
float roll = orientation[2] * FROM_RADS_TO_DEGS;
((TextView)findViewById(R.id.pitch)).setText("Pitch: "+pitch);
((TextView)findViewById(R.id.roll)).setText("Roll: "+roll);
}
}
Source: www.techrepublic.com
All those coders who are working on the Whatever based application and are stuck on device rotation sensor android examples can get a collection of related answers to their query. Programmers need to enter their query on device rotation sensor android examples related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about device rotation sensor android examples for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of device rotation sensor android examples while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "device rotation sensor android examples". Visit this developer's friendly online web community, CodeProZone, and get your queries like device rotation sensor android examples resolved professionally and stay updated to the latest Whatever updates.