Java tutorial
//package com.java2s; //License from project: Apache License import android.hardware.SensorEvent; import android.hardware.SensorManager; import android.opengl.Matrix; import android.util.Log; import android.view.Surface; public class Main { private static final String TAG = "VRUtil"; private static float[] sUIThreadTmp = new float[16]; private static float[] sTruncatedVector = new float[4]; private static boolean sIsTruncated = false; public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) { if (!sIsTruncated) { try { SensorManager.getRotationMatrixFromVector(sUIThreadTmp, event.values); } catch (Exception e) { // On some Samsung devices, SensorManager#getRotationMatrixFromVector throws an exception // if the rotation vector has more than 4 elements. Since only the four first elements are used, // we can truncate the vector without losing precision. Log.e(TAG, "maybe Samsung bug, will truncate vector"); sIsTruncated = true; } } if (sIsTruncated) { System.arraycopy(event.values, 0, sTruncatedVector, 0, 4); SensorManager.getRotationMatrixFromVector(sUIThreadTmp, sTruncatedVector); } float[] values = event.values; switch (rotation) { case Surface.ROTATION_0: case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */ SensorManager.getRotationMatrixFromVector(output, values); break; case Surface.ROTATION_90: SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values); SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, output); break; case Surface.ROTATION_270: SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values); SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, output); break; } Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F); } }