improve multitouch gesture detection

This commit is contained in:
Hannes Janetzek 2013-02-06 11:08:57 +01:00
parent 7c6ec614a2
commit 343a3bd1b3

View File

@ -21,15 +21,12 @@ import org.oscim.overlay.OverlayManager;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
@ -40,12 +37,13 @@ import android.widget.Scroller;
* - fix recognition of tilt/rotate/scale state...
*/
final class TouchHandler implements OnGestureListener, OnScaleGestureListener, OnDoubleTapListener {
final class TouchHandler implements OnGestureListener, OnDoubleTapListener {
//OnScaleGestureListener,
private static final String TAG = TouchHandler.class.getName();
private static final float SCALE_DURATION = 500;
private static final float ROTATION_DELAY = 200; // ms
//private static final float ROTATION_DELAY = 200; // ms
private static final int INVALID_POINTER_ID = -1;
@ -54,7 +52,7 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
private final OverlayManager mOverlayManager;
private final DecelerateInterpolator mInterpolator;
private final DecelerateInterpolator mLinearInterpolator;
//private final DecelerateInterpolator mLinearInterpolator;
private boolean mBeginScale;
private float mSumScale;
@ -62,17 +60,28 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
private boolean mBeginTilt;
private boolean mLongPress;
// private float mPosX;
private float mPosY;
private float mPrevX;
private float mPrevY;
private float mPrevX2;
private float mPrevY2;
private double mAngle;
private int mActivePointerId;
private final ScaleGestureDetector mScaleGestureDetector;
//private final ScaleGestureDetector mScaleGestureDetector;
private final GestureDetector mGestureDetector;
private final float dpi;
protected static final int JUMP_THRESHOLD = 100;
protected static final double PINCH_ZOOM_THRESHOLD = 5;
protected static final double PINCH_ROTATE_THRESHOLD = 0.02;
protected static final double PINCH_TILT_THRESHOLD = 0.002;
protected int mPrevPointerCount = 0;
protected double mPrevPinchWidth = -1;
/**
* @param context
* the Context
@ -86,14 +95,14 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
// ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
// mMapMoveDelta = viewConfiguration.getScaledTouchSlop();
mActivePointerId = INVALID_POINTER_ID;
mScaleGestureDetector = new ScaleGestureDetector(context, this);
//mScaleGestureDetector = new ScaleGestureDetector(context, this);
mGestureDetector = new GestureDetector(context, this);
mGestureDetector.setOnDoubleTapListener(this);
mInterpolator = new DecelerateInterpolator(2f);
mScroller = new Scroller(mMapView.getContext(), mInterpolator);
mLinearInterpolator = new DecelerateInterpolator(0.8f);//new android.view.animation.LinearInterpolator();
//mLinearInterpolator = new DecelerateInterpolator(0.8f);//new android.view.animation.LinearInterpolator();
DisplayMetrics metrics = mapView.getResources().getDisplayMetrics();
dpi = metrics.xdpi;
@ -112,7 +121,7 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
return true;
mGestureDetector.onTouchEvent(event);
mScaleGestureDetector.onTouchEvent(event);
//boolean scaling = false; //mScaleGestureDetector.onTouchEvent(event);
int action = getAction(event);
@ -149,106 +158,163 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
}
private boolean onActionDown(MotionEvent event) {
// mPosX = event.getX();
mPosY = event.getY();
mPrevX = event.getX();
mPrevY = event.getY();
// mMoveStart = false;
mBeginRotate = false;
mBeginTilt = false;
mBeginScale = false;
// save the ID of the pointer
mActivePointerId = event.getPointerId(0);
// Log.d("...", "set active pointer" + mActivePointerId);
return true;
}
private boolean mScaling = false;
//private boolean mScaling = false;
private boolean onActionMove(MotionEvent event) {
int id = event.findPointerIndex(mActivePointerId);
float py = event.getY(id);
float moveY = py - mPosY;
mPosY = py;
float x1 = event.getX(id);
float y1 = event.getY(id);
float mx = x1 - mPrevX;
float my = y1 - mPrevY;
float width = mMapView.getWidth();
float height = mMapView.getHeight();
// double-tap + hold
if (mLongPress) {
mMapPosition.scaleMap(1 - moveY / 100, 0, 0);
mMapPosition.scaleMap(1 - my / (height / 5), 0, 0);
mMapView.redrawMap(true);
mPrevX = x1;
mPrevY = y1;
return true;
}
if (mMulti == 0)
// return if detect a new gesture, as indicated by a large jump
if (Math.abs(mx) > JUMP_THRESHOLD || Math.abs(my) > JUMP_THRESHOLD)
return true;
if (event.getEventTime() - mMultiTouchDownTime < ROTATION_DELAY)
if (mMulti == 0) {
// reset pinch variables
mPrevPinchWidth = -1;
return true;
}
double x1 = event.getX(0);
double x2 = event.getX(1);
double y1 = event.getY(0);
double y2 = event.getY(1);
float x2 = event.getX(1);
float y2 = event.getY(1);
double dx = x1 - x2;
double dy = y1 - y2;
float dx = (x1 - x2);
float dy = (y1 - y2);
float slope = 0;
if (dx != 0)
slope = dy / dx;
double pinchWidth = Math.sqrt(dx * dx + dy * dy);
final double deltaPinchWidth = pinchWidth - mPrevPinchWidth;
double rad = Math.atan2(dy, dx);
double r = rad - mAngle;
if (!mBeginRotate && !mBeginScale) {
/* our naive gesture detector for rotation and tilt.. */
boolean startScale = (mPrevPinchWidth > 0 && Math.abs(deltaPinchWidth) > PINCH_ZOOM_THRESHOLD);
if (Math.abs(rad) < 0.30 || Math.abs(rad) > Math.PI - 0.30) {
boolean changed = false;
if (!mBeginTilt && (mBeginScale || startScale)) {
mBeginScale = true;
float scale = (float) (pinchWidth / mPrevPinchWidth);
mSumScale *= scale;
if (mSumScale < 0.95 || mSumScale > 1.05) {
mBeginRotate = false;
}
float fx = (x2 + x1) / 2 - width / 2;
float fy = (y2 + y1) / 2 - height / 2;
//Log.d(TAG, "zoom " + deltaPinchWidth + " " + scale + " " + mSumScale);
changed = mMapPosition.scaleMap(scale, fx, fy);
}
if (!mBeginScale && !mBeginRotate && Math.abs(slope) < 2) {
// normalize dx, dy with screen width and height, so they are in [0, 1]
// final double xVelocity = dx / width;
float my2 = y2 - mPrevY2;
final double yVelocity = my / height;
final double yVelocity2 = my2 / height;
if ((yVelocity > PINCH_TILT_THRESHOLD && yVelocity2 > PINCH_TILT_THRESHOLD)
|| (yVelocity < -PINCH_TILT_THRESHOLD && yVelocity2 < -PINCH_TILT_THRESHOLD))
{
mBeginTilt = true;
if (mMapPosition.tilt(moveY / 4)) {
mMapView.redrawMap(true);
}
return true;
changed = mMapPosition.tilt(my / 5);
}
}
if (!mBeginTilt) {
if (Math.abs(r) > 0.05) {
// Log.d(TAG, "begin rotate");
mAngle = rad;
mBeginRotate = true;
if (!mBeginTilt && (mBeginRotate || Math.abs(r) > PINCH_ROTATE_THRESHOLD)) {
//Log.d(TAG, "rotate: " + mBeginRotate + " " + Math.toDegrees(rad));
if (!mBeginRotate) {
mAngle = rad;
mSumScale = 1;
mBeginRotate = true;
mFocusX = (width / 2) - (x1 + x2) / 2;
mFocusY = (height / 2) - (y1 + y2) / 2;
} else {
double da = rad - mAngle;
if (Math.abs(da) > 0.01) {
double rsin = Math.sin(r);
double rcos = Math.cos(r);
float x = (float) (mFocusX * rcos + mFocusY * -rsin - mFocusX);
float y = (float) (mFocusX * rsin + mFocusY * rcos - mFocusY);
mMapPosition.rotateMap((float) Math.toDegrees(da), x, y);
changed = true;
}
}
}
if (mBeginRotate) {
double rsin = Math.sin(r);
double rcos = Math.cos(r);
// focus point relative to center
double cx = (mMapView.getWidth() >> 1) - (x1 + x2) / 2;
double cy = (mMapView.getHeight() >> 1) - (y1 + y2) / 2;
float x = (float) (cx * rcos + cy * -rsin - cx);
float y = (float) (cx * rsin + cy * rcos - cy);
mMapPosition.rotateMap((float) Math.toDegrees(rad - mAngle), x, y);
mAngle = rad;
if (changed)
mMapView.redrawMap(true);
}
mPrevX = x1;
mPrevY = y1;
mPrevX2 = x2;
mPrevY2 = y2;
mPrevPinchWidth = pinchWidth;
mAngle = rad;
return true;
}
private int mMulti = 0;
private boolean mWasMulti;
private long mMultiTouchDownTime;
// private long mMultiTouchDownTime;
private boolean onActionPointerDown(MotionEvent event) {
mMultiTouchDownTime = event.getEventTime();
// mMultiTouchDownTime = event.getEventTime();
mMulti++;
mWasMulti = true;
mSumScale = 1;
if (mMulti == 1) {
double dx = event.getX(0) - event.getX(1);
double dy = event.getY(0) - event.getY(1);
mPrevX2 = event.getX(1);
mPrevY2 = event.getY(1);
double dx = event.getX(0) - mPrevX2;
double dy = event.getY(0) - mPrevY2;
mAngle = Math.atan2(dy, dx);
}
// Log.d("...", "mMulti down " + mMulti);
@ -269,13 +335,14 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
pointerIndex = 0;
}
// save the position of the event
// mPosX = motionEvent.getX(pointerIndex);
mPosY = motionEvent.getY(pointerIndex);
// mPrevX = motionEvent.getX(pointerIndex);
mPrevY = motionEvent.getY(pointerIndex);
mActivePointerId = motionEvent.getPointerId(pointerIndex);
}
mMulti--;
mLongPress = false;
// Log.d("...", "mMulti up " + mMulti);
return true;
@ -288,11 +355,14 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
*/
private boolean onActionUp(MotionEvent motionEvent) {
mActivePointerId = INVALID_POINTER_ID;
mScaling = false;
//mScaling = false;
mLongPress = false;
mMulti = 0;
mPrevPinchWidth = -1;
mPrevPointerCount = 0;
return true;
}
@ -353,8 +423,8 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
return true;
}
if (mScaling)
return true;
// if (mScaling)
// return true;
if (mMulti == 0) {
mMapPosition.moveMap(-distanceX, -distanceY);
@ -368,9 +438,12 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (mScaling || mWasMulti)
if (mWasMulti)
return true;
// if (mScaling || mWasMulti)
// return true;
int w = Tile.TILE_SIZE * 6;
int h = Tile.TILE_SIZE * 6;
mScrollX = 0;
@ -474,118 +547,119 @@ final class TouchHandler implements OnGestureListener, OnScaleGestureListener, O
return false;
}
/******************* ScaleListener *******************/
private float mCenterX;
private float mCenterY;
private float mFocusX;
private float mFocusY;
private long mTimeStart;
private long mTimeEnd;
@Override
public boolean onScale(ScaleGestureDetector gd) {
if (mBeginTilt)
return true;
float scale = gd.getScaleFactor();
mFocusX = gd.getFocusX() - mCenterX;
mFocusY = gd.getFocusY() - mCenterY;
mSumScale *= scale;
mTimeEnd = SystemClock.elapsedRealtime();
if (!mBeginScale) {
if (mSumScale > 1.1 || mSumScale < 0.9) {
// Log.d("...", "begin scale " + mSumScale);
mBeginScale = true;
// scale = mSumScale;
}
}
if (mBeginScale && mMapPosition.scaleMap(scale, mFocusX, mFocusY))
mMapView.redrawMap(true);
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector gd) {
mScaling = true;
mBeginScale = false;
mTimeEnd = mTimeStart = SystemClock.elapsedRealtime();
mSumScale = 1;
mCenterX = mMapView.getWidth() >> 1;
mCenterY = mMapView.getHeight() >> 1;
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector gd) {
// Log.d("ScaleListener", "Sum " + mSumScale + " " + (mTimeEnd -
// mTimeStart));
if (mTimer == null && mTimeEnd - mTimeStart < 150
&& (mSumScale < 0.99 || mSumScale > 1.01)) {
mPrevScale = 0;
mZooutOut = mSumScale < 0.99;
mTimer = new CountDownTimer((int) SCALE_DURATION, 32) {
@Override
public void onTick(long tick) {
scaleAnim(tick);
}
@Override
public void onFinish() {
scaleAnim(0);
}
}.start();
} else {
mScaling = false;
}
mBeginScale = false;
}
// /******************* ScaleListener *******************/
private float mPrevScale;
private CountDownTimer mTimer;
boolean mZooutOut;
// private float mCenterX;
// private float mCenterY;
private float mFocusX;
private float mFocusY;
// private long mTimeStart;
// private long mTimeEnd;
//
// @Override
// public boolean onScale(ScaleGestureDetector gd) {
//
// if (mBeginTilt)
// return true;
//
// float scale = gd.getScaleFactor();
// mFocusX = gd.getFocusX() - mCenterX;
// mFocusY = gd.getFocusY() - mCenterY;
//
// mSumScale *= scale;
//
// mTimeEnd = SystemClock.elapsedRealtime();
//
// if (!mBeginScale) {
// if (mSumScale > 1.1 || mSumScale < 0.9) {
// // Log.d("...", "begin scale " + mSumScale);
// mBeginScale = true;
// // scale = mSumScale;
// }
// }
//
// if (mBeginScale && mMapPosition.scaleMap(scale, mFocusX, mFocusY))
// mMapView.redrawMap(true);
//
// return true;
// }
//
// @Override
// public boolean onScaleBegin(ScaleGestureDetector gd) {
// mScaling = true;
// mBeginScale = false;
//
// mTimeEnd = mTimeStart = SystemClock.elapsedRealtime();
// mSumScale = 1;
// mCenterX = mMapView.getWidth() >> 1;
// mCenterY = mMapView.getHeight() >> 1;
//
// if (mTimer != null) {
// mTimer.cancel();
// mTimer = null;
// }
// return true;
// }
//
// @Override
// public void onScaleEnd(ScaleGestureDetector gd) {
// // Log.d("ScaleListener", "Sum " + mSumScale + " " + (mTimeEnd -
// // mTimeStart));
//
// if (mTimer == null && mTimeEnd - mTimeStart < 150
// && (mSumScale < 0.99 || mSumScale > 1.01)) {
//
// mPrevScale = 0;
//
// mZooutOut = mSumScale < 0.99;
//
// mTimer = new CountDownTimer((int) SCALE_DURATION, 32) {
// @Override
// public void onTick(long tick) {
// scaleAnim(tick);
// }
//
// @Override
// public void onFinish() {
// scaleAnim(0);
// }
// }.start();
// } else {
// mScaling = false;
// }
//
// mBeginScale = false;
// }
//
boolean scaleAnim(long tick) {
if (mPrevScale >= 1) {
mTimer = null;
return false;
}
float adv = (SCALE_DURATION - tick) / SCALE_DURATION;
// adv = mInterpolator.getInterpolation(adv);
adv = mLinearInterpolator.getInterpolation(adv);
float scale = adv - mPrevScale;
mPrevScale += scale;
if (mZooutOut) {
mMapPosition.scaleMap(1 - scale, 0, 0);
} else {
mMapPosition.scaleMap(1 + scale, mFocusX, mFocusY);
}
mMapView.redrawMap(true);
if (tick == 0)
mTimer = null;
return true;
}
//
// boolean scaleAnim(long tick) {
//
// if (mPrevScale >= 1) {
// mTimer = null;
// return false;
// }
//
// float adv = (SCALE_DURATION - tick) / SCALE_DURATION;
// // adv = mInterpolator.getInterpolation(adv);
// adv = mLinearInterpolator.getInterpolation(adv);
//
// float scale = adv - mPrevScale;
// mPrevScale += scale;
//
// if (mZooutOut) {
// mMapPosition.scaleMap(1 - scale, 0, 0);
// } else {
// mMapPosition.scaleMap(1 + scale, mFocusX, mFocusY);
// }
//
// mMapView.redrawMap(true);
//
// if (tick == 0)
// mTimer = null;
//
// return true;
// }
}