started paged-fling mode, not enabled yet

This commit is contained in:
Hannes Janetzek 2013-01-05 20:47:30 +01:00
parent cb9cd645af
commit 70b7c79a8e
3 changed files with 77 additions and 34 deletions

View File

@ -73,6 +73,7 @@ public class MapView extends RelativeLayout {
public boolean enableRotation = false; public boolean enableRotation = false;
public boolean enableCompass = false; public boolean enableCompass = false;
public boolean enablePagedFling = false;
private final MapViewPosition mMapViewPosition; private final MapViewPosition mMapViewPosition;
private final MapPosition mMapPosition; private final MapPosition mMapPosition;

View File

@ -423,13 +423,33 @@ public class MapViewPosition {
return true; return true;
} }
// JAVA...
class Point2D {
double x;
double y;
}
// only use in synchronized functions!
Point2D mMovePoint = new Point2D();
/** /**
* Moves this MapViewPosition by the given amount of pixels. * Moves this MapViewPosition by the given amount of pixels.
* @param mx the amount of pixels to move the map horizontally. * @param mx the amount of pixels to move the map horizontally.
* @param my the amount of pixels to move the map vertically. * @param my the amount of pixels to move the map vertically.
*/ */
public synchronized void moveMap(float mx, float my) { public synchronized void moveMap(float mx, float my) {
getMove(mx, my);
mLatitude = MercatorProjection.pixelYToLatitude(mPosY - mMovePoint.y, mZoomLevel);
mLatitude = MercatorProjection.limitLatitude(mLatitude);
mLongitude = MercatorProjection.pixelXToLongitude(mPosX - mMovePoint.x, mZoomLevel);
mLongitude = MercatorProjection.wrapLongitude(mLongitude);
updatePosition();
}
private void getMove(float mx, float my) {
double dx = mx / mScale; double dx = mx / mScale;
double dy = my / mScale; double dy = my / mScale;
@ -443,16 +463,8 @@ public class MapViewPosition {
dy = y; dy = y;
} }
dx = mPosX - dx; mMovePoint.x = dx;
dy = mPosY - dy; mMovePoint.y = dy;
mLatitude = MercatorProjection.pixelYToLatitude(dy, mZoomLevel);
mLatitude = MercatorProjection.limitLatitude(mLatitude);
mLongitude = MercatorProjection.pixelXToLongitude(dx, mZoomLevel);
mLongitude = MercatorProjection.wrapLongitude(mLongitude);
updatePosition();
} }
/** /**
@ -631,6 +643,19 @@ public class MapViewPosition {
mHandler.start((int) mDuration); mHandler.start((int) mDuration);
} }
public synchronized void animateTo(float dx, float dy, float duration) {
getMove(dx, dy);
mEndX = mPosX - mMovePoint.x;
mEndY = mPosY - mMovePoint.y;
mStartX = mPosX;
mStartY = mPosY;
mDuration = duration;
mHandler.start((int) mDuration);
}
synchronized void setMapPosition(double x, double y) { synchronized void setMapPosition(double x, double y) {
mLatitude = MercatorProjection.pixelYToLatitude(y, mZoomLevel); mLatitude = MercatorProjection.pixelYToLatitude(y, mZoomLevel);

View File

@ -22,6 +22,7 @@ import org.oscim.overlay.OverlayManager;
import android.content.Context; import android.content.Context;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.os.SystemClock; import android.os.SystemClock;
import android.util.Log;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener; import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener; import android.view.GestureDetector.OnGestureListener;
@ -40,7 +41,7 @@ import android.widget.Scroller;
final class TouchHandler implements OnGestureListener, OnScaleGestureListener, OnDoubleTapListener { final class TouchHandler implements OnGestureListener, OnScaleGestureListener, OnDoubleTapListener {
//private static final String TAG = TouchHandler.class.getSimpleName(); private static final String TAG = TouchHandler.class.getName();
private static final float SCALE_DURATION = 500; private static final float SCALE_DURATION = 500;
private static final float ROTATION_DELAY = 200; // ms private static final float ROTATION_DELAY = 200; // ms
@ -108,6 +109,8 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
int action = getAction(event); int action = getAction(event);
if (action == MotionEvent.ACTION_DOWN) { if (action == MotionEvent.ACTION_DOWN) {
mMulti = 0;
mWasMulti = false;
if (mOverlayManager.onDown(event, mMapView)) if (mOverlayManager.onDown(event, mMapView))
return true; return true;
@ -167,7 +170,7 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
return true; return true;
} }
if (multi == 0) if (mMulti == 0)
return true; return true;
if (event.getEventTime() - mMultiTouchDownTime < ROTATION_DELAY) if (event.getEventTime() - mMultiTouchDownTime < ROTATION_DELAY)
@ -224,21 +227,23 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
return true; return true;
} }
private int multi = 0; private int mMulti = 0;
private boolean mWasMulti;
private long mMultiTouchDownTime; private long mMultiTouchDownTime;
private boolean onActionPointerDown(MotionEvent event) { private boolean onActionPointerDown(MotionEvent event) {
mMultiTouchDownTime = event.getEventTime(); mMultiTouchDownTime = event.getEventTime();
multi++; mMulti++;
mWasMulti = true;
if (multi == 1) { if (mMulti == 1) {
double dx = event.getX(0) - event.getX(1); double dx = event.getX(0) - event.getX(1);
double dy = event.getY(0) - event.getY(1); double dy = event.getY(0) - event.getY(1);
mAngle = Math.atan2(dy, dx); mAngle = Math.atan2(dy, dx);
} }
// Log.d("...", "multi down " + multi); // Log.d("...", "mMulti down " + mMulti);
return true; return true;
} }
@ -260,10 +265,10 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
mPosY = motionEvent.getY(pointerIndex); mPosY = motionEvent.getY(pointerIndex);
mActivePointerId = motionEvent.getPointerId(pointerIndex); mActivePointerId = motionEvent.getPointerId(pointerIndex);
} }
multi--; mMulti--;
mLongPress = false; mLongPress = false;
// Log.d("...", "multi up " + multi); // Log.d("...", "mMulti up " + mMulti);
return true; return true;
} }
@ -278,7 +283,7 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
mScaling = false; mScaling = false;
mLongPress = false; mLongPress = false;
multi = 0; mMulti = 0;
return true; return true;
} }
@ -343,7 +348,7 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
if (mScaling) if (mScaling)
return true; return true;
if (multi == 0) { if (mMulti == 0) {
mMapPosition.moveMap(-distanceX, -distanceY); mMapPosition.moveMap(-distanceX, -distanceY);
mMapView.redrawMap(); mMapView.redrawMap();
} }
@ -355,7 +360,7 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) { float velocityY) {
if (mScaling) if (mScaling || mWasMulti)
return true; return true;
int w = Tile.TILE_SIZE * 20; int w = Tile.TILE_SIZE * 20;
@ -368,11 +373,23 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
mTimer = null; mTimer = null;
} }
if (mMapView.enablePagedFling) {
double a = Math.sqrt(velocityX * velocityX + velocityY * velocityY);
float vx = (float) (velocityX / a);
float vy = (float) (velocityY / a);
Log.d(TAG, "velocity: " + a + " " + velocityX + " " + velocityY + " - " + vx + " " + vy);
if (a < 500)
return true;
mMapPosition.animateTo(vx * Tile.TILE_SIZE * 2, vy * Tile.TILE_SIZE * 2, 250);
} else {
mScroller.fling(0, 0, Math.round(velocityX) / 2, Math.round(velocityY) / 2, mScroller.fling(0, 0, Math.round(velocityX) / 2, Math.round(velocityY) / 2,
-w, w, -h, h); -w, w, -h, h);
// animate for two seconds mTimer = new CountDownTimer(1000, 16) {
mTimer = new CountDownTimer(1500, 16) {
@Override @Override
public void onTick(long tick) { public void onTick(long tick) {
scroll(); scroll();
@ -383,7 +400,7 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
} }
}.start(); }.start();
fling = true; fling = true;
}
return true; return true;
} }