[android-developers] Replicating the results of the deprecated Sensor.TYPE_ORIENTATION
Hi All,
I've been using SensorManager with Sensor.TYPE_ORIENTATION for its
pitch/roll values. I want to update the code to support multiple
screen orientations (and while I'm at it get rid of some deprecated
code).
My current solution is below. The main problem with it, is that if I
set the phone down on the table, the roll does not go to 0. With some
phones, it stays were it last was before being set down. With others,
it fluctuates (noticeably) around some value.
I've tried looking for the deprecated source in the Android git
source, but could not find exactly where things were being stabilized.
Any help would be appreciated! Maybe somebody at Google could post the
deprecated code?
(please excuse the messy/unoptimized code below:)
public void onSensorChanged( SensorEvent event )
{
switch( event.sensor.getType() )
{
case Sensor.TYPE_ACCELEROMETER:
if( gravity == null )
gravity = event.values.clone();
// would it help to calculate alpha
every time?
final float alpha = 0.8f;
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
boolean b = SensorManager.getRotationMatrix( R, null, gravity,
geomagnetic );
if( b )
{
switch( rotation )
{
case Surface.ROTATION_0:
case Surface.ROTATION_180:
SensorManager.remapCoordinateSystem( R,
SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Z, outR );
break;
case Surface.ROTATION_90:
case Surface.ROTATION_270:
SensorManager.remapCoordinateSystem( R, SensorManager.AXIS_Z,
SensorManager.AXIS_X, outR );
break;
}
SensorManager.getOrientation( outR,
values );
roll = (float) Math.toDegrees( values[2] );
}
break;
case Sensor.TYPE_MAGNETIC_FIELD:
geomagnetic = event.values.clone();
break;
}
}
Thanks!
-Sheado
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home