Example usage for android.hardware SensorManager AXIS_MINUS_X

List of usage examples for android.hardware SensorManager AXIS_MINUS_X

Introduction

In this page you can find the example usage for android.hardware SensorManager AXIS_MINUS_X.

Prototype

int AXIS_MINUS_X

To view the source code for android.hardware SensorManager AXIS_MINUS_X.

Click Source Link

Document

see #remapCoordinateSystem

Usage

From source file:Main.java

public static void sensorRotation2Matrix(float[] gravity, float[] geomagnetic, int rotation, float[] output) {
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrix(output, null, gravity, geomagnetic);
        break;/* w  w w  .j ava2s. c  om*/
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrix(mTmp, null, gravity, geomagnetic);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrix(mTmp, null, gravity, geomagnetic);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    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;/*  w ww .  ja  v a  2 s. c o  m*/
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrixFromVector(mTmp, values);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrixFromVector(mTmp, values);
        SensorManager.remapCoordinateSystem(mTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}

From source file:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    if (!sIsTruncated) {
        try {/* w w  w.ja  va2s . c  om*/
            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);
}

From source file:com.metinkale.prayerapp.compass.Main.java

@Override
public void onRotationUpdate(float[] newMatrix) {
    if (mMode == Mode.Map) {
        return;/*from w  w w  . j  a v a  2 s.c  o  m*/
    }
    // remap matrix values according to display rotation, as in
    // SensorManager documentation.
    switch (mDisplayRotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180:
        break;
    case Surface.ROTATION_90:
        SensorManager.remapCoordinateSystem(newMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                newMatrix);
        break;
    case Surface.ROTATION_270:
        SensorManager.remapCoordinateSystem(newMatrix, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X,
                newMatrix);
        break;
    default:
        break;
    }
    mRotationMatrix.set(newMatrix);
    mOrientationCalculator.getOrientation(mRotationMatrix, mDisplayRotation, mDerivedDeviceOrientation);

    updateFrag((mDerivedDeviceOrientation[1] > -55f) ? Mode.ThreeDim : Mode.TwoDim);

    mList.onUpdateSensors(mDerivedDeviceOrientation);
}

From source file:com.tritop.androsense2.fragments.GpsFragment.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mAccel = event.values;// w  w  w. j  a  v a 2  s . co  m
    }
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mMag = event.values;
    }
    if ((mAccel != null) && (mMag != null)) {
        float R[] = new float[9];
        float I[] = new float[9];
        float Rot[] = new float[9];
        if (SensorManager.getRotationMatrix(R, I, mAccel, mMag)) {
            float orientation[] = new float[3];
            int axisX = 0, axisY = 0;
            switch (display.getRotation()) {
            case Surface.ROTATION_0:
                axisX = SensorManager.AXIS_X;
                axisY = SensorManager.AXIS_Y;
                break;
            case Surface.ROTATION_90:
                axisX = SensorManager.AXIS_Y;
                axisY = SensorManager.AXIS_MINUS_X;
                break;
            case Surface.ROTATION_180:
                axisX = SensorManager.AXIS_MINUS_X;
                axisY = SensorManager.AXIS_MINUS_Y;
                break;
            case Surface.ROTATION_270:
                axisX = SensorManager.AXIS_MINUS_Y;
                axisY = SensorManager.AXIS_X;
                break;
            default:
                break;
            }

            SensorManager.remapCoordinateSystem(R, axisX, axisY, Rot);
            SensorManager.getOrientation(Rot, orientation);
            aRotation = orientation[0];
            aRotation = (int) Math.toDegrees(aRotation);
            satView.setAzimutRotation(-aRotation);
            satView.invalidate();
        }
    }
}

From source file:com.shadowmaps.example.GpsTestActivity.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override//from  ww  w .j  a  v a  2  s.com
public void onSensorChanged(SensorEvent event) {

    double orientation = Double.NaN;
    double tilt = Double.NaN;

    switch (event.sensor.getType()) {
    case Sensor.TYPE_ROTATION_VECTOR:
        // Modern rotation vector sensors
        if (!mTruncateVector) {
            try {
                SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
            } catch (IllegalArgumentException e) {
                // On some Samsung devices, an exception is thrown if this vector > 4 (see #39)
                // Truncate the array, since we can deal with only the first four values
                Log.e(TAG, "Samsung device error? Will truncate vectors - " + e);
                mTruncateVector = true;
                // Do the truncation here the first time the exception occurs
                getRotationMatrixFromTruncatedVector(event.values);
            }
        } else {
            // Truncate the array to avoid the exception on some devices (see #39)
            getRotationMatrixFromTruncatedVector(event.values);
        }

        int rot = getWindowManager().getDefaultDisplay().getRotation();
        switch (rot) {
        case Surface.ROTATION_0:
            // No orientation change, use default coordinate system
            SensorManager.getOrientation(mRotationMatrix, mValues);
            // Log.d(TAG, "Rotation-0");
            break;
        case Surface.ROTATION_90:
            // Log.d(TAG, "Rotation-90");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_Y,
                    SensorManager.AXIS_MINUS_X, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        case Surface.ROTATION_180:
            // Log.d(TAG, "Rotation-180");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_MINUS_X,
                    SensorManager.AXIS_MINUS_Y, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        case Surface.ROTATION_270:
            // Log.d(TAG, "Rotation-270");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_MINUS_Y,
                    SensorManager.AXIS_X, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        default:
            // This shouldn't happen - assume default orientation
            SensorManager.getOrientation(mRotationMatrix, mValues);
            // Log.d(TAG, "Rotation-Unknown");
            break;
        }
        orientation = Math.toDegrees(mValues[0]); // azimuth
        tilt = Math.toDegrees(mValues[1]);
        break;
    case Sensor.TYPE_ORIENTATION:
        // Legacy orientation sensors
        orientation = event.values[0];
        break;
    default:
        // A sensor we're not using, so return
        return;
    }

    // Correct for true north, if preference is set
    if (mFaceTrueNorth && mGeomagneticField != null) {
        orientation += mGeomagneticField.getDeclination();
    }

    for (GpsTestListener listener : mGpsTestListeners) {
        listener.onOrientationChanged(orientation, tilt);
    }
}

From source file:com.android.gpstest.GpsTestActivity.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override/*from  w  w  w  . j a v a 2  s .co m*/
public void onSensorChanged(SensorEvent event) {

    double orientation = Double.NaN;
    double tilt = Double.NaN;

    switch (event.sensor.getType()) {
    case Sensor.TYPE_ROTATION_VECTOR:
        // Modern rotation vector sensors
        if (!mTruncateVector) {
            try {
                SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
            } catch (IllegalArgumentException e) {
                // On some Samsung devices, an exception is thrown if this vector > 4 (see #39)
                // Truncate the array, since we can deal with only the first four values
                Log.e(TAG, "Samsung device error? Will truncate vectors - " + e);
                mTruncateVector = true;
                // Do the truncation here the first time the exception occurs
                getRotationMatrixFromTruncatedVector(event.values);
            }
        } else {
            // Truncate the array to avoid the exception on some devices (see #39)
            getRotationMatrixFromTruncatedVector(event.values);
        }

        int rot = getWindowManager().getDefaultDisplay().getRotation();
        switch (rot) {
        case Surface.ROTATION_0:
            // No orientation change, use default coordinate system
            SensorManager.getOrientation(mRotationMatrix, mValues);
            // Log.d(TAG, "Rotation-0");
            break;
        case Surface.ROTATION_90:
            // Log.d(TAG, "Rotation-90");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_Y,
                    SensorManager.AXIS_MINUS_X, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        case Surface.ROTATION_180:
            // Log.d(TAG, "Rotation-180");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_MINUS_X,
                    SensorManager.AXIS_MINUS_Y, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        case Surface.ROTATION_270:
            // Log.d(TAG, "Rotation-270");
            SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_MINUS_Y,
                    SensorManager.AXIS_X, mRemappedMatrix);
            SensorManager.getOrientation(mRemappedMatrix, mValues);
            break;
        default:
            // This shouldn't happen - assume default orientation
            SensorManager.getOrientation(mRotationMatrix, mValues);
            // Log.d(TAG, "Rotation-Unknown");
            break;
        }
        orientation = Math.toDegrees(mValues[0]); // azimuth
        tilt = Math.toDegrees(mValues[1]);
        break;
    case Sensor.TYPE_ORIENTATION:
        // Legacy orientation sensors
        orientation = event.values[0];
        break;
    default:
        // A sensor we're not using, so return
        return;
    }

    // Correct for true north, if preference is set
    if (mFaceTrueNorth && mGeomagneticField != null) {
        orientation += mGeomagneticField.getDeclination();
        // Make sure value is between 0-360
        orientation = MathUtils.mod((float) orientation, 360.0f);
    }

    for (GpsTestListener listener : mGpsTestListeners) {
        listener.onOrientationChanged(orientation, tilt);
    }
}

From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.BusStopDetailsFragment.java

/**
 * Update the direction needle so that it is pointing towards the bus stop,
 * based on the device location and the direction it is facing.
 */// w  w w . j  a  va2s.co m
private void updateDirectionNeedle() {
    // We need values for location, the accelerometer and magnetometer to
    // continue.
    if (lastLocation == null || accelerometerValues == null || magnetometerValues == null) {
        // Make sure the needle isn't showing.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);

        return;
    }

    // Calculating the rotation matrix may fail, for example, if the device
    // is in freefall. In that case we cannot continue as the values will
    // be unreliable.
    if (!SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerValues, magnetometerValues)) {
        return;
    }

    // The screen rotation was obtained earlier.
    switch (screenRotation) {
    // There's lots of information about this elsewhere, but briefly;
    // The values from the sensors are in the device's coordinate system
    // which may be correct if the device is in its natural orientation,
    // but it needs to be remapped if the device is rotated.
    case Surface.ROTATION_0:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z,
                rotationMatrix);
        break;
    case Surface.ROTATION_90:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X,
                rotationMatrix);
        break;
    case Surface.ROTATION_180:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X,
                SensorManager.AXIS_MINUS_Z, rotationMatrix);
        break;
    case Surface.ROTATION_270:
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Z, SensorManager.AXIS_X,
                rotationMatrix);
        break;
    }

    // Get the X, Y and Z orientations, which are in radians. Covert this
    // in to degrees East of North.
    SensorManager.getOrientation(rotationMatrix, headings);
    double heading = Math.toDegrees(headings[0]);

    // If there's a GeomagneticField value, then adjust the heading to take
    // this in to account.
    if (geoField != null) {
        heading -= geoField.getDeclination();
    }

    // The orientation is in the range of -180 to +180. Convert this in to
    // a range of 0 to 360.
    final float bearingTo = distance[1] < 0 ? distance[1] + 360 : distance[1];

    // This is the heading to the bus stop.
    heading = bearingTo - heading;

    // The above calculation may come out as a negative number again. Put
    // this back in to the range of 0 to 360.
    if (heading < 0) {
        heading += 360;
    }

    // This 'if' statement is required to prevent a crash during device
    // rotation. It ensured that the Fragment is still part of the Activity.
    if (isAdded()) {
        // Get the arrow bitmap from the resources.
        final Bitmap needleIn = BitmapFactory.decodeResource(getResources(), R.drawable.heading_arrow);
        // Get an identity matrix and rotate it by the required amount.
        final Matrix m = new Matrix();
        m.setRotate((float) heading % 360, (float) needleIn.getWidth() / 2, (float) needleIn.getHeight() / 2);
        // Apply the rotation matrix to the Bitmap, to create a new Bitmap.
        final Bitmap needleOut = Bitmap.createBitmap(needleIn, 0, 0, needleIn.getWidth(), needleIn.getHeight(),
                m, true);

        // Recycle the needle read in if it's not the same as the rotated
        // needle.
        if (needleIn != needleOut) {
            needleIn.recycle();
        }

        // This Bitmap needs to be converted to a Drawable type.
        final BitmapDrawable drawable = new BitmapDrawable(getResources(), needleOut);
        // Set the new needle to be on the right hand side of the TextView.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        recycleNeedleBitmapIfNotNull(needleOut);
    } else {
        // If the Fragment is not added to the Activity, then make sure
        // there's no needle.
        txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        recycleNeedleBitmapIfNotNull(null);
    }
}