add animating move

git-svn-id: https://osmand.googlecode.com/svn/trunk@317 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-07-11 09:06:38 +00:00
parent feaf5f9d17
commit 421f12a35a
10 changed files with 242 additions and 61 deletions

View file

@ -19,7 +19,6 @@ public class ToDoConstants {
// 60. Audio guidance for routing !
// 68. Implement service to app work with screen offline
// (introduce special settings how often update location to monitoring & audio guidance)
// 69. Multitouch zoom, animated zoom, animate map shift (when select some point to see)!
// Improvement : Show stops in the transport route on the map
// Not clear if it is really needed
@ -55,6 +54,7 @@ public class ToDoConstants {
// 33. Build transport locations. Create transport index (transport-stops) (investigate)
// Not implemented : show key/transit stops on map, follow mode (show next stop)
// 58. Upload/Download zip-index from site & unzip them on phone
// 69. Multitouch zoom, animated zoom, animate map shift (when select some point to see)!
// DONE SWING

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="sd_unmounted">Карточка SD недоступна. \nВы не сможете работать с картой.</string>
<string name="sd_mounted_ro">Карточка SD доступна только для чтения. \nВы не сможете загружать карты из интернета.</string>
<string name="unzipping_file">Файл распаковывается</string>
<string name="route_tr">Поверните направо и двигайтесь</string>
<string name="route_tshr">Поверните резко направо и двигайтесь</string>

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="sd_unmounted">SD card is not accessible. \nYou can\'t see map and find anything.</string>
<string name="sd_mounted_ro">SD card is read-only accessible. \nYou can see only preloaded map and can\'t download from internet.</string>
<string name="unzipping_file">File is unzipping</string>
<string name="route_tr">Turn right and go</string>
<string name="route_tshr">Turn sharply right and go</string>

View file

@ -278,6 +278,10 @@ public class OsmandSettings {
public static final String IS_MAP_SYNC_TO_GPS_LOCATION = "is_map_sync_to_gps_location"; //$NON-NLS-1$
public static final String LAST_KNOWN_MAP_ZOOM = "last_known_map_zoom"; //$NON-NLS-1$
public static final String MAP_LAT_TO_SHOW = "map_lat_to_show"; //$NON-NLS-1$
public static final String MAP_LON_TO_SHOW = "map_lon_to_show"; //$NON-NLS-1$
public static final String MAP_ZOOM_TO_SHOW = "map_zoom_to_show"; //$NON-NLS-1$
public static LatLon getLastKnownMapLocation(Context ctx) {
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
float lat = prefs.getFloat(LAST_KNOWN_MAP_LAT, 0);
@ -286,14 +290,35 @@ public class OsmandSettings {
}
public static void setMapLocationToShow(Context ctx, double latitude, double longitude) {
setMapLocationToShow(ctx, latitude, longitude, getLastKnownMapZoom(ctx), null);
}
public static void setMapLocationToShow(Context ctx, double latitude, double longitude, int zoom) {
setMapLocationToShow(ctx, latitude, longitude, null);
}
public static void setMapLocationToShow(Context ctx, double latitude, double longitude, String historyDescription) {
public static LatLon getAndClearMapLocationToShow(Context ctx){
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
if(!prefs.contains(MAP_LAT_TO_SHOW)){
return null;
}
float lat = prefs.getFloat(MAP_LAT_TO_SHOW, 0);
float lon = prefs.getFloat(MAP_LON_TO_SHOW, 0);
prefs.edit().remove(MAP_LAT_TO_SHOW).commit();
return new LatLon(lat, lon);
}
public static int getMapZoomToShow(Context ctx) {
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
return prefs.getInt(MAP_ZOOM_TO_SHOW, 5);
}
public static void setMapLocationToShow(Context ctx, double latitude, double longitude, int zoom, String historyDescription) {
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
Editor edit = prefs.edit();
edit.putFloat(LAST_KNOWN_MAP_LAT, (float) latitude);
edit.putFloat(LAST_KNOWN_MAP_LON, (float) longitude);
edit.putFloat(MAP_LAT_TO_SHOW, (float) latitude);
edit.putFloat(MAP_LON_TO_SHOW, (float) longitude);
edit.putInt(MAP_ZOOM_TO_SHOW, zoom);
edit.putBoolean(IS_MAP_SYNC_TO_GPS_LOCATION, false);
edit.commit();
if(historyDescription != null){
@ -301,6 +326,10 @@ public class OsmandSettings {
}
}
public static void setMapLocationToShow(Context ctx, double latitude, double longitude, String historyDescription) {
setMapLocationToShow(ctx, latitude, longitude, getLastKnownMapZoom(ctx), historyDescription);
}
// Do not use that method if you want to show point on map. Use setMapLocationToShow
public static void setLastKnownMapLocation(Context ctx, double latitude, double longitude) {
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);

View file

@ -31,6 +31,7 @@ import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
@ -68,6 +69,7 @@ import com.osmand.data.preparation.MapTileDownloader.IMapDownloaderCallback;
import com.osmand.map.IMapLocationListener;
import com.osmand.osm.LatLon;
import com.osmand.osm.MapUtils;
import com.osmand.views.AnimateDraggingMapThread;
import com.osmand.views.MapInfoLayer;
import com.osmand.views.OsmBugsLayer;
import com.osmand.views.OsmandMapTileView;
@ -228,6 +230,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
zoomControls.setOnZoomInClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mapView.getAnimatedDraggingThread().stopAnimatingSync();
mapView.getAnimatedDraggingThread().startZooming(mapView.getZoom(), mapView.getZoom() + 1);
showAndHideMapPosition();
// user can preview map manually switch off auto zoom while user don't press back to location
@ -239,6 +242,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mapView.getAnimatedDraggingThread().stopAnimatingSync();
mapView.getAnimatedDraggingThread().startZooming(mapView.getZoom(), mapView.getZoom() - 1);
showAndHideMapPosition();
// user can preview map manually switch off auto zoom while user don't press back to location
@ -256,7 +260,11 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
OsmandSettings.setSyncMapToGpsLocation(MapActivity.this, true);
if(locationLayer.getLastKnownLocation() != null){
Location lastKnownLocation = locationLayer.getLastKnownLocation();
mapView.setLatLon(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
AnimateDraggingMapThread thread = mapView.getAnimatedDraggingThread();
int fZoom = mapView.getZoom() < 15 ? 15 : mapView.getZoom();
thread.startMoving(mapView.getLatitude(), mapView.getLongitude(),
lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude(), mapView.getZoom(), fZoom,
mapView.getSourceTileSize(), false);
}
}
}
@ -589,14 +597,10 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
locationLayer.setAppMode(OsmandSettings.getApplicationMode(this));
routingHelper.setAppMode(OsmandSettings.getApplicationMode(this));
poiMapLayer.setFilter(OsmandSettings.getPoiFilterForMap(this));
mapView.setMapPosition(OsmandSettings.getPositionOnMap(this));
SharedPreferences prefs = getSharedPreferences(OsmandSettings.SHARED_PREFERENCES_NAME, MODE_WORLD_READABLE);
if(prefs != null && prefs.contains(OsmandSettings.LAST_KNOWN_MAP_LAT)){
LatLon l = OsmandSettings.getLastKnownMapLocation(this);
mapView.setLatLon(l.getLatitude(), l.getLongitude());
mapView.setZoom(OsmandSettings.getLastKnownMapZoom(this));
}
backToLocation.setVisibility(View.INVISIBLE);
@ -642,9 +646,34 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "com.osmand.map"); //$NON-NLS-1$
wakeLock.acquire();
}
SharedPreferences prefs = getSharedPreferences(OsmandSettings.SHARED_PREFERENCES_NAME, MODE_WORLD_READABLE);
if(prefs != null && prefs.contains(OsmandSettings.LAST_KNOWN_MAP_LAT)){
LatLon l = OsmandSettings.getLastKnownMapLocation(this);
mapView.setLatLon(l.getLatitude(), l.getLongitude());
mapView.setZoom(OsmandSettings.getLastKnownMapZoom(this));
LatLon latLon = OsmandSettings.getAndClearMapLocationToShow(this);
LatLon cur = new LatLon(mapView.getLatitude(), mapView.getLongitude());
if(latLon != null && !latLon.equals(cur)){
mapView.getAnimatedDraggingThread().startMoving(cur.getLatitude(), cur.getLongitude(),
latLon.getLatitude(), latLon.getLongitude(),
mapView.getZoom(), OsmandSettings.getMapZoomToShow(this), mapView.getSourceTileSize(), true);
}
}
checkExternalStorage();
showAndHideMapPosition();
}
public void checkExternalStorage(){
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
// ok
} else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
Toast.makeText(this, R.string.sd_mounted_ro, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, R.string.sd_unmounted, Toast.LENGTH_LONG).show();
}
}
public void showAndHideMapPosition(){
mapView.setShowMapPosition(true);

View file

@ -218,8 +218,7 @@ public class SearchAddressActivity extends Activity {
if(navigateTo){
OsmandSettings.setPointToNavigate(SearchAddressActivity.this, l.getLatitude(), l.getLongitude());
} else {
OsmandSettings.setMapLocationToShow(SearchAddressActivity.this, l.getLatitude(), l.getLongitude(), historyName);
OsmandSettings.setLastKnownMapZoom(SearchAddressActivity.this, zoom);
OsmandSettings.setMapLocationToShow(SearchAddressActivity.this, l.getLatitude(), l.getLongitude(), zoom, historyName);
}
startActivity(new Intent(SearchAddressActivity.this, MapActivity.class));

View file

@ -123,8 +123,7 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
OsmandSettings.setPoiFilterForMap(SearchPOIActivity.this, filter.getFilterId());
OsmandSettings.setShowPoiOverMap(SearchPOIActivity.this, true);
if(searchNearBy && location != null){
OsmandSettings.setMapLocationToShow(SearchPOIActivity.this, location.getLatitude(), location.getLongitude());
OsmandSettings.setLastKnownMapZoom(SearchPOIActivity.this, 15);
OsmandSettings.setMapLocationToShow(SearchPOIActivity.this, location.getLatitude(), location.getLongitude(), 15);
}
Intent newIntent = new Intent(SearchPOIActivity.this, MapActivity.class);
startActivity(newIntent);
@ -356,9 +355,8 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
OsmandSettings.setShowPoiOverMap(SearchPOIActivity.this, true);
}
Amenity amenity = ((AmenityAdapter) getListAdapter()).getItem(position);
OsmandSettings.setMapLocationToShow(this, amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude(),
OsmandSettings.setMapLocationToShow(this, amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude(), 16,
getString(R.string.poi)+" : " + amenity.getSimpleFormat(OsmandSettings.usingEnglishNames(this))); //$NON-NLS-1$
OsmandSettings.setLastKnownMapZoom(this, 16);
Intent newIntent = new Intent(SearchPOIActivity.this, MapActivity.class);
startActivity(newIntent);
}

View file

@ -1,5 +1,7 @@
package com.osmand.views;
import com.osmand.osm.MapUtils;
/**
* Thread for animated dragging.
* Defines accelerator to stop dragging screen.
@ -7,15 +9,14 @@ package com.osmand.views;
public class AnimateDraggingMapThread implements Runnable {
public interface AnimateDraggingCallback {
public void dragTo(float curX, float curY, float newX, float newY);
public void dragTo(float curX, float curY, float newX, float newY, boolean notify);
public void zoomTo(float zoom, boolean notify);
public void zoomTo(float zoom);
}
private int endZ;
private float curZ;
private int timeZ;
private boolean animateDrag = true;
private float curX;
private float curY;
private float vx;
@ -24,57 +25,115 @@ public class AnimateDraggingMapThread implements Runnable {
private float ay;
private byte dirX;
private byte dirY;
private byte dirZ;
private final float a = 0.001f;
private long time;
private volatile boolean stopped;
private final float a = 0.001f;
// 0 - zoom out, 1 - moving, 2 - zoom in
private byte phaseOfMoving ;
private int endZ;
private byte dirZ;
private int intZ;
private byte dirIntZ;
private float curZ;
private int timeZEnd;
private int timeZInt;
private int timeMove;
private float moveX;
private float moveY;
private volatile Thread currentThread = null;
private AnimateDraggingCallback callback = null;
private boolean notifyListener;
@Override
public void run() {
currentThread = Thread.currentThread();
try {
while (!stopped && (vx > 0 || vy > 0 || curZ != endZ)) {
boolean conditionToCountinue = true;
while (!stopped && conditionToCountinue) {
// calculate sleep
long sleep = 0;
if(vx > 0 || vy > 0){
sleep = (long) (40d / (Math.max(vx, vy) + 0.45));
if(animateDrag){
// sleep = (long) (40d / (Math.max(vx, vy) + 0.45));
sleep = 80;
} else {
sleep = 80;
}
Thread.sleep(sleep);
long curT = System.currentTimeMillis();
int dt = (int) (curT - time);
float newX = vx > 0 ? curX + dirX * vx * dt : curX;
float newY = vy > 0 ? curY + dirY * vy * dt : curY;
float newX = animateDrag && vx > 0 ? curX + dirX * vx * dt : curX;
float newY = animateDrag && vy > 0 ? curY + dirY * vy * dt : curY;
float newZ = curZ;
if(timeZ > 0){
newZ = newZ + dirZ * (float)dt / timeZ;
if(dirZ > 0 == newZ > endZ){
newZ = endZ;
if(!animateDrag){
if (phaseOfMoving == 0 || phaseOfMoving == 2) {
byte dir = phaseOfMoving == 2 ? dirZ : dirIntZ;
int time = phaseOfMoving == 2 ? timeZEnd : timeZInt;
float end = phaseOfMoving == 2 ? endZ : intZ;
if (time > 0) {
newZ = newZ + dir * (float) dt / time;
}
if (dir > 0 == newZ > end) {
newZ = end;
}
} else {
if(timeMove > 0){
newX = newX + moveX * (float) dt / timeMove;
newY = newY + moveY * (float) dt / timeMove;
if(moveX > 0 == newX > moveX){
newX = moveX;
}
if(moveY > 0 == newY > moveY){
newY = moveY;
}
}
}
}
if (!stopped && callback != null) {
if (vx > 0 || vy > 0) {
callback.dragTo(curX, curY, newX, newY);
}
if(newZ > 0){
callback.zoomTo(newZ);
if (animateDrag || phaseOfMoving == 1) {
callback.dragTo(curX, curY, newX, newY, notifyListener);
} else {
callback.zoomTo(newZ, notifyListener);
}
}
time = curT;
if(animateDrag){
vx -= ax * dt;
vy -= ay * dt;
time = curT;
curX = newX;
curY = newY;
conditionToCountinue = vx > 0 || vy > 0;
} else {
if(phaseOfMoving == 0){
curZ = newZ;
if(curZ == intZ){
curX = 0;
curY = 0;
phaseOfMoving ++;
}
} else if(phaseOfMoving == 2){
curZ = newZ;
conditionToCountinue = curZ != endZ;
} else {
curX = newX;
curY = newY;
if(curX == moveX && curY == moveY){
phaseOfMoving ++;
}
}
}
}
if(curZ != ((int) Math.round(curZ))){
if(Math.abs(curZ - endZ) > 3){
callback.zoomTo(Math.round(curZ), notifyListener);
} else {
callback.zoomTo(endZ, notifyListener);
}
if(curZ != endZ && endZ > 0){
callback.zoomTo(endZ);
}
} catch (InterruptedException e) {
}
@ -105,36 +164,74 @@ public class AnimateDraggingMapThread implements Runnable {
public void startZooming(int zoomStart, int zoomEnd){
stopAnimatingSync();
this.notifyListener = false;
if(zoomStart < zoomEnd){
dirZ = 1;
} else {
dirZ = -1;
}
vx = 0;
curZ = zoomStart;
endZ = zoomEnd;
timeZ = 600;
timeZEnd = 600;
phaseOfMoving = 2;
animateDrag = false;
time = System.currentTimeMillis();
stopped = false;
Thread thread = new Thread(this,"Animatable dragging"); //$NON-NLS-1$
thread.start();
}
public void startDragging(float dTime, float startX, float startY, float endX, float endY){
vx = Math.abs((endX - startX)/dTime);
vy = Math.abs((endY - startY)/dTime);
startDragging(vx, vy, startX, startY, endX, endY);
public void startMoving(double curLat, double curLon, double finalLat, double finalLon, int curZoom, int endZoom, int tileSize, boolean notifyListener){
stopAnimatingSync();
this.notifyListener = notifyListener;
intZ = curZoom;
moveX = (float) ((MapUtils.getTileNumberX(intZ, curLon) - MapUtils.getTileNumberX(intZ, finalLon)) * tileSize);
moveY = (float) ((MapUtils.getTileNumberY(intZ, curLat) - MapUtils.getTileNumberY(intZ, finalLat)) * tileSize);
// todo calculate right with rotated map!!!
while (Math.abs(moveX) + Math.abs(moveY) > 1200 && intZ > 4) {
intZ--;
moveX = (float) ((MapUtils.getTileNumberX(intZ, curLon) - MapUtils.getTileNumberX(intZ, finalLon)) * tileSize);
moveY = (float) ((MapUtils.getTileNumberY(intZ, curLat) - MapUtils.getTileNumberY(intZ, finalLat)) * tileSize);
}
if(curZoom < intZ){
dirIntZ = 1;
} else {
dirIntZ = -1;
}
if(intZ < endZoom){
dirZ = 1;
} else {
dirZ = -1;
}
endZ = endZoom;
timeZInt = Math.abs(curZoom - intZ) * 300;
timeZEnd = 500;
timeMove = (int) (Math.abs(moveX) + Math.abs(moveY) * 4);
if(timeMove > 2000){
timeMove = 2000;
}
animateDrag = false;
phaseOfMoving = (byte) (intZ == curZoom ? 1 : 0);
curX = 0;
curY = 0;
time = System.currentTimeMillis();
stopped = false;
Thread thread = new Thread(this,"Animatable dragging"); //$NON-NLS-1$
thread.start();
}
public void startDragging(float velocityX, float velocityY, float startX, float startY, float endX, float endY){
stopAnimatingSync();
this.notifyListener = true;
vx = velocityX;
vy = velocityY;
dirX = (byte) (endX > startX ? 1 : -1);
dirY = (byte) (endY > startY ? 1 : -1);
curZ = 0;
endZ = 0;
timeZ = 0;
animateDrag = true;
ax = vx * a;
ay = vy * a;
time = System.currentTimeMillis();

View file

@ -16,9 +16,11 @@ import android.util.FloatMath;
import com.osmand.Algoritms;
import com.osmand.Messages;
import com.osmand.OsmandSettings;
import com.osmand.activities.MapActivity;
import com.osmand.activities.RoutingHelper.RouteDirectionInfo;
import com.osmand.activities.RoutingHelper.TurnType;
import com.osmand.osm.LatLon;
import com.osmand.osm.MapUtils;
public class MapInfoLayer implements OsmandMapLayer {
@ -438,6 +440,15 @@ public class MapInfoLayer implements OsmandMapLayer {
view.refreshMap();
return true;
}
if(cachedDistString != null && boundsForDist.contains(point.x, point.y)){
AnimateDraggingMapThread thread = view.getAnimatedDraggingThread();
LatLon pointToNavigate = OsmandSettings.getPointToNavigate(view.getContext());
if(pointToNavigate != null){
int fZoom = view.getZoom() < 15 ? 15 : view.getZoom();
thread.startMoving(view.getLatitude(), view.getLongitude(), pointToNavigate.getLatitude(), pointToNavigate.getLongitude(),
view.getZoom(), fZoom, view.getSourceTileSize(), true);
}
}
return false;
}

View file

@ -65,7 +65,6 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
/**
* zoom level - could be float to show zoomed tiles
*/
// TODO rotated zoom calculation check where it is could be needed???
private float zoom = 3;
private double longitude = 0d;
@ -206,6 +205,10 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
return m * tileSize;
}
public int getSourceTileSize() {
return map == null ? 256 : map.getTileSize();
}
public float getXTile(){
return (float) MapUtils.getTileNumberX(zoom, longitude);
@ -226,10 +229,13 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
// for internal usage
@Override
public void zoomTo(float zoom) {
public void zoomTo(float zoom, boolean notify) {
if (map == null || (map.getMaximumZoomSupported() >= zoom && map.getMinimumZoomSupported() <= zoom)) {
this.zoom = zoom;
refreshMap();
if(notify && locationListener != null){
locationListener.locationChanged(latitude, longitude, this);
}
}
}
@ -622,21 +628,29 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
}
@Override
public void dragTo(float fromX, float fromY, float toX, float toY){
public void dragTo(float fromX, float fromY, float toX, float toY, boolean notify){
float dx = (fromX - toX) ;
float dy = (fromY - toY);
moveTo(dx, dy);
if(locationListener != null && notify){
locationListener.locationChanged(latitude, longitude, this);
}
}
public void moveTo(float dx, float dy) {
float fy = calcDiffTileY(dx, dy);
float fx = calcDiffTileX(dx, dy);
this.latitude = MapUtils.getLatitudeFromTile(zoom, getYTile() + fy);
this.longitude = MapUtils.getLongitudeFromTile(zoom, getXTile() + fx);
refreshMap();
if(locationListener != null){
locationListener.locationChanged(latitude, longitude, this);
}
// do not notify here listener
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
@ -738,7 +752,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
dragTo(e2.getX() + distanceX, e2.getY() + distanceY, e2.getX(), e2.getY());
dragTo(e2.getX() + distanceX, e2.getY() + distanceY, e2.getX(), e2.getY(), true);
return true;
}