0

I am trying to bring a motion parallax effect to the view using Sensor type accelerometer as it support most of the android devices.

I have the following code in my onSensorChanged() method:

 float[] g = new float[3];
        g = sensorEvent.values.clone();
        float norm_Of_g = (float) Math.sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2]);
        // Normalize the accelerometer vector
        g[0] = g[0] / norm_Of_g;
        g[1] = g[1] / norm_Of_g;
        g[2] = g[2] / norm_Of_g;
        if (startingAngle == 999) {
            startingAngle = (float) (Math.acos(g[0]) * 180 / Math.PI);
            currentAngle = (float) (Math.acos(g[0]) * 180 / Math.PI);
            lowerLimitAngle = currentAngle - kRotationAngleLimit;
            upperLimitAngle = currentAngle + kRotationAngleLimit;
            initialScrollX = scrollView.getScrollX();
            pixelsPerDegree = ((simpleDraweeView.getDrawable().getIntrinsicWidth()/ (2 * kRotationAngleLimit)));

        }

        currentAngle = (float) (Math.acos(g[0]) * 180 / Math.PI);
        if (currentAngle >= lowerLimitAngle && currentAngle <= upperLimitAngle) {
            xOffset = (startingAngle - currentAngle) * pixelsPerDegree + initialScrollX;
        }
//
        scrollView.setScrollX((int) xOffset);

This works perfectly when i use TYPE_ROTATION_VECTOR.

On using TYPE_ACCELEROMETER, i get a fluctuate reading with it. Due to which the scroll is jerky.

Please help me on this.

How to measure the tilt of the phone in XY plane using accelerometer in Android

Community
  • 1
  • 1
Fahim
  • 12,198
  • 5
  • 39
  • 57
  • Accelerometers are by their nature noisy input sources, subject to drift over time. There's no way to compensate for that in software, the only way is to combine input from other sensors (e.g. compass, gps, camera) in order to constantly re-calibrate the accelerometer. – Aaron D Feb 16 '16 at 05:43
  • @AaronD ok... you have any working approach that can work for non-gyroscope phones? – Fahim Feb 16 '16 at 05:55

0 Answers0