简体   繁体   中英

Angle of phone rotated along X axis

I am working on an Android app wherein I want to scroll a large image horizontally. I used the accelerometer (Sensor.TYPE_ACCELEROMETER) and magnetic field (Sensor.TYPE_MAGNETIC_FIELD) data to get the angle of rotation. This data being to frequent infested with noise I am not able to implement a smooth motion effect.

@Override
public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
        case Sensor.TYPE_MAGNETIC_FIELD:
            mags = event.values.clone();
            break;
        case Sensor.TYPE_ACCELEROMETER:
            accels = event.values.clone();
            break;
    }

    if (mags != null && accels != null) {
        gravity = new float[16];
        boolean success = SensorManager.getRotationMatrix(gravity, null, accels, mags);
        if (success) {
            float[] outGravity = new float[16];
            SensorManager.remapCoordinateSystem(gravity, SensorManager.AXIS_X, SensorManager.AXIS_Z, outGravity);
            SensorManager.getOrientation(outGravity, values);

            rollingAverage[0] = roll(rollingAverage[0], values[0]);
            rollingAverage[1] = roll(rollingAverage[1], values[1]);
            rollingAverage[2] = roll(rollingAverage[2], values[2]);

            azimuth = Math.toDegrees(values[0]);
            pitch = Math.toDegrees(values[1]);
            roll = Math.toDegrees(values[2]);
            mags = null;
            accels = null;

            double diffRoll = lastRoll - roll;
            double diffPitch = lastPitch - pitch;
            long curTime = System.currentTimeMillis();
            if (Math.abs(diffRoll) >= 2) {
                if (diffRoll > 0)
                    imageView.panLeft();
                else
                    imageView.panRight();
                lastRoll = roll;
            }
        }
    }
}

Any ideas on achieving this using other methods?

You have to implement sensor fusion techniques based on Kalman filter or other filters. You can use open source libraries if needed. Refer this bitbucket repository . If you want to do yourself, read the tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM