diff options
Diffstat (limited to 'main')
| -rw-r--r-- | main/src/cgeo/geocaching/DirectionProvider.java | 11 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/CacheListAdapter.java | 4 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/CompassMiniView.java | 3 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/ui/CompassView.java | 40 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/AngleUtils.java | 28 |
5 files changed, 52 insertions, 34 deletions
diff --git a/main/src/cgeo/geocaching/DirectionProvider.java b/main/src/cgeo/geocaching/DirectionProvider.java index d8ee782..2f90b74 100644 --- a/main/src/cgeo/geocaching/DirectionProvider.java +++ b/main/src/cgeo/geocaching/DirectionProvider.java @@ -70,15 +70,4 @@ public class DirectionProvider extends MemorySubject<Float> implements SensorEve return Compatibility.getDirectionNow(direction, activity) % 360; } - /** - * Return the angle to turn of to go from an angle to the other - * - * @param from the origin angle in degrees, in the [0, 360[ range - * @param to the target angle in degreees, in the [0, 360[ range - * @return a value in degrees, in the [-180, 180[ range - */ - public static double difference(final double from, final double to) { - return (to - from + 360 + 180) % 360 - 180; - } - } diff --git a/main/src/cgeo/geocaching/ui/CacheListAdapter.java b/main/src/cgeo/geocaching/ui/CacheListAdapter.java index b599290..c6d3404 100644 --- a/main/src/cgeo/geocaching/ui/CacheListAdapter.java +++ b/main/src/cgeo/geocaching/ui/CacheListAdapter.java @@ -14,6 +14,7 @@ import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.sorting.CacheComparator; import cgeo.geocaching.sorting.DistanceComparator; import cgeo.geocaching.sorting.VisitComparator; +import cgeo.geocaching.utils.AngleUtils; import cgeo.geocaching.utils.Log; import org.apache.commons.collections.CollectionUtils; @@ -301,8 +302,7 @@ public class CacheListAdapter extends ArrayAdapter<cgCache> { } public void setActualHeading(final float direction) { - final float delta = (360 + azimuth - direction) % 360; - if (delta < 10 || delta > 350) { + if (Math.abs(AngleUtils.difference(azimuth, direction)) < 10) { return; } diff --git a/main/src/cgeo/geocaching/ui/CompassMiniView.java b/main/src/cgeo/geocaching/ui/CompassMiniView.java index 1c96a09..44fb8e2 100644 --- a/main/src/cgeo/geocaching/ui/CompassMiniView.java +++ b/main/src/cgeo/geocaching/ui/CompassMiniView.java @@ -3,6 +3,7 @@ package cgeo.geocaching.ui; import cgeo.geocaching.R; import cgeo.geocaching.Settings; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.utils.AngleUtils; import android.content.Context; import android.graphics.Bitmap; @@ -159,7 +160,7 @@ final public class CompassMiniView extends View { } private float calculateAzimuthRelative() { - return (azimuth - heading + 360) % 360; + return AngleUtils.normalize(azimuth - heading); } @Override diff --git a/main/src/cgeo/geocaching/ui/CompassView.java b/main/src/cgeo/geocaching/ui/CompassView.java index 703f96f..b328527 100644 --- a/main/src/cgeo/geocaching/ui/CompassView.java +++ b/main/src/cgeo/geocaching/ui/CompassView.java @@ -1,7 +1,7 @@ package cgeo.geocaching.ui; -import cgeo.geocaching.DirectionProvider; import cgeo.geocaching.R; +import cgeo.geocaching.utils.AngleUtils; import cgeo.geocaching.utils.PeriodicHandler; import android.content.Context; @@ -23,19 +23,19 @@ public class CompassView extends View { /** * North direction currently SHOWN on compass (not measured) */ - private double azimuthShown = 0.0; + private float azimuthShown = 0; /** * cache direction currently SHOWN on compass (not measured) */ - private double cacheHeadingShown = 0.0; + private float cacheHeadingShown = 0; /** * cache direction measured from device, or 0.0 */ - private double cacheHeadingMeasured = 0.0; + private float cacheHeadingMeasured = 0; /** * North direction measured from device, or 0.0 */ - private double northMeasured = 0.0; + private float northMeasured = 0; private PaintFlagsDrawFilter setfil = null; private PaintFlagsDrawFilter remfil = null; private int compassUnderlayWidth = 0; @@ -103,7 +103,7 @@ public class CompassView extends View { } } - public synchronized void updateNorth(double northHeadingIn, double cacheHeadingIn) { + public synchronized void updateNorth(float northHeadingIn, float cacheHeadingIn) { if (initialDisplay) { // We will force the compass to move brutally if this is the first // update since it is visible. @@ -128,20 +128,20 @@ public class CompassView extends View { * the actual value * @return the new value */ - static protected double smoothUpdate(double goal, double actual) { - final double diff = DirectionProvider.difference(actual, goal); + static protected float smoothUpdate(float goal, float actual) { + final float diff = AngleUtils.difference(actual, goal); - double offset = 0.0; + float offset = 0; // If the difference is smaller than 1 degree, do nothing as it // causes the arrow to vibrate. Round away from 0. if (diff > 1.0) { - offset = Math.ceil(diff / 10.0); // for larger angles, rotate faster + offset = (float) Math.ceil(diff / 10.0); // for larger angles, rotate faster } else if (diff < 1.0) { - offset = Math.floor(diff / 10.0); + offset = (float) Math.floor(diff / 10.0); } - return (actual + offset + 360) % 360; + return AngleUtils.normalize(actual + offset); } private class RedrawHandler extends PeriodicHandler { @@ -152,10 +152,10 @@ public class CompassView extends View { @Override public void act() { - final double newAzimuthShown = smoothUpdate(northMeasured, azimuthShown); - final double newCacheHeadingShown = smoothUpdate(cacheHeadingMeasured, cacheHeadingShown); - if (Math.abs(DirectionProvider.difference(azimuthShown, newAzimuthShown)) >= 2 || - Math.abs(DirectionProvider.difference(cacheHeadingShown, newCacheHeadingShown)) >= 2) { + final float newAzimuthShown = smoothUpdate(northMeasured, azimuthShown); + final float newCacheHeadingShown = smoothUpdate(cacheHeadingMeasured, cacheHeadingShown); + if (Math.abs(AngleUtils.difference(azimuthShown, newAzimuthShown)) >= 2 || + Math.abs(AngleUtils.difference(cacheHeadingShown, newCacheHeadingShown)) >= 2) { synchronized(CompassView.this) { azimuthShown = newAzimuthShown; cacheHeadingShown = newCacheHeadingShown; @@ -169,16 +169,16 @@ public class CompassView extends View { @Override protected void onDraw(Canvas canvas) { // use local synchronized variables to avoid them being changed from the device during drawing - double azimuthDrawn; - double headingDrawn; + float azimuthDrawn; + float headingDrawn; synchronized (this) { azimuthDrawn = azimuthShown; headingDrawn = cacheHeadingShown; } - double azimuthTemp = azimuthDrawn; - final double azimuthRelative = (azimuthTemp - headingDrawn + 360) % 360; + float azimuthTemp = azimuthDrawn; + final float azimuthRelative = AngleUtils.normalize(azimuthTemp - headingDrawn); // compass margins int canvasCenterX = (compassRoseWidth / 2) + ((getWidth() - compassRoseWidth) / 2); diff --git a/main/src/cgeo/geocaching/utils/AngleUtils.java b/main/src/cgeo/geocaching/utils/AngleUtils.java new file mode 100644 index 0000000..e2b4a66 --- /dev/null +++ b/main/src/cgeo/geocaching/utils/AngleUtils.java @@ -0,0 +1,28 @@ +package cgeo.geocaching.utils; + +public class AngleUtils { + + private AngleUtils() { + // Do not instantiate + } + + /** + * Return the angle to turn of to go from an angle to the other + * + * @param from the origin angle in degrees + * @param to the target angle in degreees + * @return a value in degrees, in the [-180, 180[ range + */ + public static float difference(final float from, final float to) { + return normalize(to - from + 180) - 180; + } + + /** + * Normalize an angle so that it belongs to the [0, 360[ range. + * @param angle the angle in degrees + * @return the same angle in the [0, 360[ range + */ + public static float normalize(final float angle) { + return (angle >= 0 ? angle : (360 - ((-angle) % 360))) % 360; + } +} |
