getOrientation (float[] R, float[] values)
getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text=" XY" /> <TextView android:id="@+id/xyValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text="0" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView3" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text=" XZ" /> /> <TextView android:id="@+id/xzValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text="0" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView5" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text=" ZY" /> <TextView android:id="@+id/zyValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text="0" /> </LinearLayout> </LinearLayout>
private final SensorManager msensorManager; // private float[] rotationMatrix; // private float[] accelData; // private float[] magnetData; // private float[] OrientationData; // private TextView xyView; private TextView xzView; private TextView zyView;
public class Main extends Activity implements SensorEventListener{
msensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); rotationMatrix = new float[16]; accelData = new float[3]; magnetData = new float[3]; OrientationData = new float[3]; xyView = (TextView) findViewById(R.id.xyValue); // xzView = (TextView) findViewById(R.id.xzValue); // zyView = (TextView) findViewById(R.id.zyValue); //
@Override protected void onResume() { super.onResume(); msensorManager.registerListener(this, msensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI ); msensorManager.registerListener(this, msensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI ); }
@Override protected void onPause() { super.onPause(); msensorManager.unregisterListener(this); }
private void loadNewSensorData(SensorEvent event) { final int type = event.sensor.getType(); // if (type == Sensor.TYPE_ACCELEROMETER) { // accelData = event.values.clone(); } if (type == Sensor.TYPE_MAGNETIC_FIELD) { // magnetData = event.values.clone(); } }
public void onSensorChanged(SensorEvent event) { loadNewSensorData(event); // SensorManager.getRotationMatrix(rotationMatrix, null, accelData, magnetData); // SensorManager.getOrientation(rotationMatrix, OrientationData); // if((xyView==null)||(xzView==null)||(zyView==null)){ // . xyView = (TextView) findViewById(R.id.xyValue); xzView = (TextView) findViewById(R.id.xzValue); zyView = (TextView) findViewById(R.id.zyValue); } // xyView.setText(String.valueOf(Math.round(Math.toDegrees(OrientationData[0])))); xzView.setText(String.valueOf(Math.round(Math.toDegrees(OrientationData[1])))); zyView.setText(String.valueOf(Math.round(Math.toDegrees(OrientationData[2])))); }
Rearrange setContentView and lines with findViewById in onCreate
Source: https://habr.com/ru/post/137820/
All Articles