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