0

I am experimenting with the accelerometer and the gyroscope. I have defined a simple line View in my layout.xml and i want to calculate the current angle of the phone so i can use View.SetRotation() to have the line be parallel to the floor. I have found many tutorials but i can't get it to work.

Here is my code:

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.main.camera3doit.TestActivity">

    <TextView
        android:text="angle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/angleText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffff0000"
        android:textSize="30dp" />

    <View
        android:id="@+id/lineView"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:background="#ff01eb00" />

</RelativeLayout>

TestActivity.java

public class TestActivity extends Activity {
    TextView angleText;
    View line;

    private SensorManager sensorManager;
    private Sensor accelerometer;
    private Sensor magnetometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);


        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        line = findViewById(R.id.lineView);
        angleText = (TextView) findViewById(R.id.angleText);
    }

    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(sensorEventListenerer, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(sensorEventListenerer, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListenerer);
    }


    private SensorEventListener sensorEventListenerer = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {

            /*
            *
            *
            *
            * Here i want to do my calculations 
            * so i can find what rotation the line should have
            *
            *
            *
            *
             */

            angleText.setText(phoneRotationDegrees);
            line.setRotation(DegreesSoItIsParallelToTheGround);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };
}
Christos Baziotis
  • 5,845
  • 16
  • 59
  • 80
  • You seem to have copied some code for a compass. You don't need the magnetometer if you're only interested in finding out which way is down. – Kevin Krumwiede Jul 26 '14 at 17:58
  • @KevinKrumwiede Maybe i have misunderstood it but according to this answer i need both: http://stackoverflow.com/a/10291428/2442740 – Christos Baziotis Jul 26 '14 at 18:00
  • I think the reason for using both sensors is that you can only calculate the gravity vector from the accelerometer alone if you assume that gravity is the only acceleration acting on the device. I assumed that's what you wanted, but maybe not. Not an answer, but some related discussion here: http://stackoverflow.com/questions/2986628/accelerometer-gravity-components?rq=1 – Kevin Krumwiede Jul 26 '14 at 18:09

0 Answers0