Android Google Maps: Disable Scroll on Zoom - android

I tried by creating custom class for mapview but it is not working. Can any one suggest me an approach. How to disable scroll when user tries to zoom. Scroll should be enable when zoom is not performed.

I Have solved the problem using below code.
Create variables in Class
private ScaleGestureDetector gestureDetector;
private long lastZoomTime = 0;
private float lastSpan = -1;
private long firstClick=0;
private long lastClick;
GoogleMap mMap;
In OnMapReady method add below code:
mMap = googleMap;
gestureDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleGestureDetector.OnScaleGestureListener() {
#Override
public boolean onScale(ScaleGestureDetector detector) {
Log.i("onScale ", " onScale ");
if (lastSpan == -1) {
lastSpan = detector.getCurrentSpan();
}
if (detector.getEventTime() - lastZoomTime >= 50) {
lastZoomTime = detector.getEventTime();
mMap.animateCamera(CameraUpdateFactory.zoomBy(getZoomValue(detector.getCurrentSpan(), lastSpan)), 50, null);
lastSpan = detector.getCurrentSpan();
}
return false;
}
#Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
lastSpan = -1;
return true;
}
#Override
public void onScaleEnd(ScaleGestureDetector detector) {
lastSpan = -1;
}
});
Add Below Methods in class
private float getZoomValue(float currentSpan, float lastSpan) {
Log.i("ongetZoomValue ", " getZoomValue ");
double value = (Math.log(currentSpan / lastSpan) / Math.log(1.55d));
return (float) value;
}
public boolean dispatchTouchEvent(MotionEvent ev) {
//Log.i("on dispatchTouchEvent ", " dispatchTouchEvent ");
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
fingers = fingers + 1;
break;
case MotionEvent.ACTION_POINTER_UP:
fingers = fingers - 1;
break;
case MotionEvent.ACTION_UP:
fingers = 0;
break;
case MotionEvent.ACTION_DOWN:
if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
Log.i("on count=0", " count=0 ");
count = 0;
}
count++;
if (count == 1) {
Log.i("on count ", " count=1 ");
firstClick = System.currentTimeMillis();
} else if (count == 2) {
Log.i("on count ", " count=2 ");
lastClick = System.currentTimeMillis();
if (lastClick - firstClick < 300) {
CameraUpdate getzoom = CameraUpdateFactory.zoomIn();
mMap.animateCamera(getzoom, 400, null);
Log.i("on double click ", " event ");
}
}
fingers=1;
break;
}
if (fingers >1) {
//Log.i("2fingersaction ", "2fingersaction ");
mMap.getUiSettings().setScrollGesturesEnabled(false);
//mMap.getUiSettings().setRotateGesturesEnabled(false); // disableScrolling()
}
else if (fingers < 1 )
{
//Log.i("onefinger ", "onefinger ");
mMap.getUiSettings().setScrollGesturesEnabled(true);
// enableScrolling();
}
if (fingers > 1) {
//Log.i("double if condition ", "doubleifcondition ");
return gestureDetector.onTouchEvent(ev);
} else
return super.dispatchTouchEvent(ev);
}

Related

Need to be able to pinch to zoom, but not to do a double Tap in my touch listener

So I have a view under my view, that I need to pass to the pinch to zoom. BUT, if the user double taps, than I do not want to pass the double tap to my second view. Because I need the functionality that is set on a double tap to be ignored.
The only problem is that that functionality is set to be called on 2 ACTION_DOWN events received from the touch listener.
Now I tried to make a logic to prevent from doubletapping to work, but make the pinch to zoom work, but it still isn't perfect. IF I tap with 2 fingers, 1 in 1 place of the screen, and then the other in another place, a bit further it will get it as a double tap, and not consume the Touch, as I need.
This is the code for my touchEventListener:
viewTop.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("","double touch event action: ===========================");
Log.i("","double touch event test action:" + event.getPointerId(0));
if(event.getPointerId(0) == 0) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (secondPressed) {
Log.i("", "double touch test : FIRST FINGER: CONSUMED TRUE");
timeLastTouch = System.currentTimeMillis();
return true;
}else {
Log.i("", "double touch DOWN : " + timeLastTouch + "... time passed: " + (System.currentTimeMillis() - timeLastTouch) + "..... location diff: " + (event.getX() - xLastTouch));
Log.i("", "double touch test XLAST : " + xLastTouch + "/" + yLastTouch + " ... XLAST 2 : " + xLastTouch2 + "/" + yLastTouch2);
if (System.currentTimeMillis() - timeLastTouch < 1000 && Math.abs(event.getX() - xLastTouch) < 150 && Math.abs(event.getY() - yLastTouch) < 150) {
Log.i("", "double touch test TRUE DOWN");
secondPressed = true;
timeLastTouch = System.currentTimeMillis();
xLastTouch = event.getX();
yLastTouch = event.getY();
return true;
} else if (System.currentTimeMillis() - timeLastTouch < 1000 && Math.abs(event.getX() - xLastTouch2) < 150 && Math.abs(event.getY() - yLastTouch2) < 150) {
Log.i("", "double touch test TRUE DOWN");
secondPressed = true;
timeLastTouch = System.currentTimeMillis();
return true;
} else {
timeLastTouch = System.currentTimeMillis();
if(xLastTouch2 == -1) {
xLastTouch2 = event.getX();
yLastTouch2 = event.getY();
}else {
xLastTouch = event.getX();
yLastTouch = event.getY();
}
Log.i("", "double touch test FALSE DOWN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! X : " + event.getX() + "/" + event.getY() + ".... TIME: " + (System.currentTimeMillis() - timeLastTouch));
Log.i("", "double touch test FALSE DOWN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DIFF LAST TOUCH: " + Math.abs(event.getX() - xLastTouch) + "/" + Math.abs(event.getY() - yLastTouch));
Log.i("", "double touch test FALSE DOWN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DIFF LAST TOUCH2: " + Math.abs(event.getX() - xLastTouch2) + "/" + Math.abs(event.getY() - yLastTouch2));
return false;
}
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
handlerTouch.removeCallbacksAndMessages(null);
Log.i("", "double touch test FALSE UP");
timeLastTouch = System.currentTimeMillis();
handlerTouch.postDelayed(new Runnable() {
#Override
public void run() {
resetTouches();
Log.i("", "double touch test : SECOND PRESSED BECOMES FALSE");
}
}, 300);
return true;
} else {
timeLastTouch = System.currentTimeMillis();
xLastTouch = event.getX();
yLastTouch = event.getY();
Log.i("", "double touch ELSE FALSE");
return false;
}
}else if(event.getPointerId(0) == 1){
timeLastTouch = System.currentTimeMillis();
if(event.getAction() == MotionEvent.ACTION_DOWN) {
xLastTouch2 = event.getX();
yLastTouch2 = event.getY();
secondPressed = true;
return false;
}else if(event.getAction() == MotionEvent.ACTION_UP){
Log.i("","double touch test : SECONDARY FINGER: SECOND PRESSED BECOMES FALSE");
handlerTouch.postDelayed(new Runnable() {
#Override
public void run() {
resetTouches();
Log.i("", "double touch test : SECOND PRESSED BECOMES FALSE");
}
}, 300);
}else {
Log.i("","double touch test : SECONDARY FINGER: SECOND PRESSED BECOMES TRUE");
secondPressed = true;
}
return false;
}else {
timeLastTouch = System.currentTimeMillis();
secondPressed = true;
return false;
}
}
});
Where resetTouches is:
private void resetTouches() {
xLastTouch = -1f;
yLastTouch = -1f;
xLastTouch2 = -1f;
yLastTouch2 = -1f;
secondPressed = false;
}
Now I'm kinda blocked here, and don't know what or how to change in order to prevent the double tap to work. Is there by any change any implementation of this that might be easier? What could I change to improve on this?
This did it for me:
final ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(InCallActivity.this, new ScaleGestureDetector.OnScaleGestureListener() {
#Override
public void onScaleEnd(ScaleGestureDetector detector) {
Log.i("", "double touch test2 onScaleEnd: " + detector.getScaleFactor());
app.TouchEvent(0, 1, (int) xFinger1, (int) yFinger1);
if (xFinger2 != 0) {
app.TouchEvent(1, 1, (int) xFinger2, (int) yFinger2);
} else scale(1, (int) (yFinger1));
}
#Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
Log.i("", "double touch test2 onScaleBegin: " + detector.getScaleFactor());
app.TouchEvent(0, 0, (int) xFinger1, (int) yFinger1);
if (xFinger2 != 0) {
app.TouchEvent(1, 0, (int) xFinger2, (int) yFinger2);
} else app.TouchEvent(1, 0, (int) xFinger1, (int) yFinger1);
return true;
}
#Override
public boolean onScale(ScaleGestureDetector detector) {
Log.i("", "double touch test2 zoom ongoing, scale: " + detector.getScaleFactor());
app.TouchEvent(0, 2, (int) xFinger1, (int) yFinger1);
if (xFinger2 != 0) {
app.TouchEvent(1, 2, (int) xFinger2, (int) yFinger2);
} else {
scaleValue = detector.getScaleFactor();
scale(2, (int) (yFinger1));
}
return false;
}
});
viewTop.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("", "double touch test2 ON TOUCH: " + event.getPointerId(0) + "x/y: " + event.getX() + "/" + event.getY());
xFinger1 = event.getX(0);
yFinger1 = event.getY(0);
try {
xFinger2 = event.getX(1);
yFinger2 = event.getY(1);
mScaleDetector.onTouchEvent(event);
} catch (Exception e) {
return logicFor1Finger(event, e);
}
return true;
}
});

How to add multiple pin marker on image dyanamically

I want to put marker on image with below functionality
-Dynamically adding multiple markers on image with zoom and scroll functionality on image.
-Blink animation on marker after added on image.
-Marker can be click-able and drag & drop on image.
-Marker's x & y position on image to sync same with other devices
I have used https://github.com/davemorrissey/subsampling-scale-image-view library
Java Code :
public class PinMarkerView extends SubsamplingScaleImageView implements View.OnTouchListener {
private PointF sPin;
private Bitmap pin;
private int resId;
public float vX;
public float vY;
private PointF vPrevious;
private PointF vStart;
private boolean drawing = false;
private boolean markerTouch = false;
private int strokeWidth;
private ArrayList<MapPins> allPins = new ArrayList<MapPins>();
private ArrayList<DrawPins> drawnPins = new ArrayList<DrawPins>();
public PinView(Context context) {
this(context, null);
}
public PinView(Context context, AttributeSet attr) {
super(context, attr);
// initialise();
}
public void setPin(PointF sPin, #DrawableRes int resid, String extras) {
MapPins mapPins = new MapPins();
mapPins.setPointF(sPin);
mapPins.setResId(resid);
mapPins.setExtrasData(extras);
mapPins.setX(sPin.x);
mapPins.setY(sPin.y);
allPins.add(mapPins);
//this.sPin = sPin;
// this.resId = resid;
initialise();
invalidate();
}
public PointF getPin() {
return sPin;
}
public Bitmap getMarker() {
return pin;
}
private void initialise() {
setOnTouchListener(this);
float density = getResources().getDisplayMetrics().densityDpi;
strokeWidth = (int) (density / 60f);
for (int i = 0; i < allPins.size(); i++) {
MapPins mapPins = allPins.get(i);
Bitmap localpin = BitmapFactory.decodeResource(this.getResources(), mapPins.getResId());
float w = (density / 100f) * localpin.getWidth();
float h = (density / 100f) * localpin.getHeight();
//pin = Bitmap.createScaledBitmap(pin, (int) w, (int) h, true);
pin = Bitmap.createScaledBitmap(localpin, localpin.getWidth(), localpin.getHeight(), true);
mapPins.setCreatedBitmap(pin);
allPins.set(i, mapPins);
//-------
}
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Don't draw pin before image is ready so it doesn't move around during setup.
if (!isReady()) {
return;
}
Paint paint = new Paint();
paint.setAntiAlias(true);
for (int i = 0; i < allPins.size(); i++) {
MapPins mapPins = allPins.get(i);
if (mapPins.getPointF() != null && mapPins.getCreatedBitmap() != null) {
PointF vPin = sourceToViewCoord(mapPins.getPointF());
vX = vPin.x - (mapPins.getCreatedBitmap().getWidth() / 2);
vY = vPin.y - mapPins.getCreatedBitmap().getHeight();
canvas.drawBitmap(mapPins.getCreatedBitmap(), vX, vY, paint);
}
}
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
setMaxScale(5f);
return false;
}
#Override
public boolean onTouchEvent(#NonNull MotionEvent event) {
/* if (isZoomEnabled()) {
return super.onTouchEvent(event);
}*/
boolean consumed = false;
int touchCount = event.getPointerCount();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_1_DOWN:
vStart = new PointF(event.getX(), event.getY());
vPrevious = new PointF(event.getX(), event.getY());
getPinIdByPoint(vStart, viewToSourceCoord(event.getX(), event.getY()));
handleActionDown((int) event.getX(), (int) event.getY());
break;
case MotionEvent.ACTION_POINTER_2_DOWN:
// Abort any current drawing, user is zooming
vStart = null;
vPrevious = null;
break;
case MotionEvent.ACTION_MOVE:
if (markerTouch) {
setPanEnabled(false);
PointF sCurrentF = viewToSourceCoord(event.getX(), event.getY());
PointF sCurrent = new PointF(sCurrentF.x, sCurrentF.y);
PointF sStart = vStart == null ? null : new PointF(viewToSourceCoord(vStart).x, viewToSourceCoord(vStart).y);
if (touchCount == 1 && vStart != null) {
float vDX = Math.abs(event.getX() - vPrevious.x);
float vDY = Math.abs(event.getY() - vPrevious.y);
if (vDX >= strokeWidth * 5 || vDY >= strokeWidth * 5) {
if (sPin == null) {
sPin = sStart;
}
sPin = sCurrent;
vPrevious.x = event.getX();
vPrevious.y = event.getY();
drawing = true;
}
consumed = true;
invalidate();
} else if (touchCount == 1) {
// Consume all one touch drags to prevent odd panning effects handled by the superclass.
consumed = true;
}
} else {
return super.onTouchEvent(event);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
invalidate();
setPanEnabled(true);
drawing = false;
markerTouch = false;
vPrevious = null;
vStart = null;
}
// Use parent to handle pinch and two-finger pan.
return consumed || super.onTouchEvent(event);
}
public void handleActionDown(int eventX, int eventY) {
if (eventX >= (sPin.x - pin.getWidth()) && (eventX <= (sPin.x + pin.getWidth()))) {
if (eventY >= (sPin.y - pin.getHeight()) && (sPin.y <= (sPin.y + pin.getHeight()))) {
markerTouch = true;
} else {
markerTouch = false;
}
} else {
markerTouch = false;
}
}
public int getPinIdByPoint(PointF tappedCoordinate, PointF deeplinkCoordinate) {
for (int i = allPins.size() - 1; i >= 0; i--) {
MapPins dPin = allPins.get(i);
int blockWidth = dPin.getCreatedBitmap().getWidth();
int blockHeight = dPin.getCreatedBitmap().getHeight();
int deeplinkX = (int) (deeplinkCoordinate.x - (dPin.getCreatedBitmap().getWidth() / 2));
int deeplinkY = (int) (deeplinkCoordinate.y - dPin.getCreatedBitmap().getHeight());
// center coordinate -/+ blockWidth actually sets touchable area to 2x icon size
if (tappedCoordinate.x >= deeplinkX - blockWidth && tappedCoordinate.x <= deeplinkX + blockWidth &&
tappedCoordinate.y >= deeplinkY - blockHeight && tappedCoordinate.y <= deeplinkY + blockHeight) {
sPin = dPin.getPointF();
pin = dPin.getCreatedBitmap();
return dPin.getId();
}
}
return -1; //negative no means no pin selected
}
}
Currently, I am able to add multiple marker, but I am facing issue of marker click and dragging on image.

How to swipe from both side in ListView?

I am trying to do swipe from both side in Listview but now i only able to do from right side, but i want to do swipe from both left and right please can any one tell me how to do that.
private void swipe(int dis) {
if (dis > mMenuView.getWidth()) {
dis = mMenuView.getWidth();
}
if (dis < 0) {
dis = 0;
}
mContentView.layout(-dis, mContentView.getTop(), mContentView.getWidth() - dis, getMeasuredHeight());
mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(),
mContentView.getWidth() + mMenuView.getWidth() - dis,
mMenuView.getBottom());
}
/onSwipe Function/
public boolean onSwipe(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = (int) event.getX();
isFling = false;
break;
case MotionEvent.ACTION_MOVE:
// Log.i("byz", "downX = " + mDownX + ", moveX = " + event.getX());
int dis = (int) (mDownX - event.getX());
if (state == STATE_OPEN) {
dis += mMenuView.getWidth();
}
swipe(dis);
break;
case MotionEvent.ACTION_UP:
if (isFling
|| (mDownX - event.getX()) > (mMenuView.getWidth() / 1.5)) {
// open
smoothOpenMenu();
} else {
// close
smoothCloseMenu();
return false;
}
break;
}
return true;
}
Please go through the above code and suggest me some solution.

redraw bitmap on the android canvas

In my app, I want to switch a bitmap image from one to another when I touch on that image. I keep getting NULLPOINTEREXCEPTION whenever I touch the image.
Is there any method that allows to switch from a bitmap to other bitmap?
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
this.cVas = canvas;
for(int i = 0; i < BSIZE; i++) {
for(int j = 0; j < BSIZE; j++) {
gameBoard[i][j] = new Cell(i*wCell, j*wCell+hCell);
if(gameBoard[i][j].getBitmap() == null) {
gameBoard[i][j].setBitmap(b3);
}
/*if(gameBoard[i][j].getTouch()) {
gameBoard[i][j].setBitmap(b4);
}else{
gameBoard[i][j].setBitmap(b3);
}*/
gameBoard[i][j].drawCell(canvas);
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() != MotionEvent.ACTION_DOWN) {
return super.onTouchEvent(event);
}
// Get the Cell's position.
selX = (int)((event.getX()) / wCell);
selY = (int)((event.getY() - hCell) / wCell);
if( selX > BSIZE || selY > BSIZE || selY < 0) {
return false;
}
else {
Log.d(TAG, "selX: " + selX + " selY: " + selY);
cellSelection(selX, selY);
}
return true;
}
// Redraw the image (switch to other bitmap).
private void cellSelection( int x, int y) {
if(gameBoard[x][y].getTouch()) {
gameBoard[x][y].setTouch(false);
gameBoard[x][y].setBitmap(b3);
} else {
gameBoard[x][y].setTouch(true);
gameBoard[x][y].setBitmap(b4);
}
gameBoard[x][y].drawCell(cVas);
invalidate();
}

Stop OnLongClickListener from firing while dragging

I have a custom View with bitmaps on it that the user can drag about.
I want to make it so when they long click one of them I can pop up a context menu with options such as reset position etc.
In the custom View I add my OnLongClickListener:
this.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// show context menu..
return true;
}
});
And override onTouchEvent to look something like this:
public boolean onTouchEvent(MotionEvent event) {
handleDrag(event);
super.onTouchEvent(event);
return true;
}
The handleDrag function finds what object is been pressed, and handles updating it's position.
My problem is that when I start to drag an image the OnLongClickListener fires also. I'm not sure the best way around this.
I've tried adding a threshold to handleDrag to return false if user touches down but doesn't attempt to drag, but I'm finding it still difficult to get the correct handler fired.
Can anyone suggest a way to skip the OnLongClickListener while dragging?
I think I have this solved through tweaking my threshold approach.
First, I changed my onTouchEvent to look like this:
public boolean onTouchEvent(MotionEvent event) {
mMultiTouchController.handleDrag(event);
return super.onTouchEvent(event);
}
They now both fire, so I then changed my OnLongClickListener to the following:
this.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (!mMultiTouchController.has_moved) {
// Pop menu and done...
return false;
}
return true;
}
});
(mMultiTouchController is the class containing all my gesture detection code).
The key here is within this class, I added the bool 'has_moved'. When I go to start a drag I then compute the delta:
float diffX = Math.abs(mCurrPtX - mPrevPt.getX());
float diffY = Math.abs(mCurrPtY - mPrevPt.getY());
if (diffX < threshold && diffY < threshold) {
has_moved = false;
return;
}
Now when the onLongClick fires I know whether to take action or not.
The final piece was to set:
setHapticFeedbackEnabled(false);
in my View so that the user doesn't get a vibrate every time the longClick fires but no action is taken. I plan to do the vibration manually as a next step.
This seems to be ok so far, hope that helps anyone who has come across a similar situation as this one.
I would stop using the onLongClickListener and just implement your own, which is pretty easy to do. Then you have the control you need to keep them from interfering with each other.
The following code implements the following gestures: drag, tap, double tap, long click, and pinch.
static final short NONE = 0;
static final short DRAG = 1;
static final short ZOOM = 2;
static final short TAP = 3;
static final short DOUBLE_TAP = 4;
static final short POST_GESTURE = 5;
short mode = NONE;
static final float MIN_PINCH_DISTANCE = 30f;
static final float MIN_DRAG_DISTANCE = 5f;
static final float DOUBLE_TAP_MAX_DISTANCE = 30f;
static final long MAX_DOUBLE_TAP_MS = 1000;
static final long LONG_PRESS_THRESHOLD_MS = 2000;
public class Vector2d {
public float x;
public float y;
public Vector2d() {
x = 0f;
y = 0f;
}
public void set(float newX, float newY) {
x = newX;
y = newY;
}
public Vector2d avgVector(Vector2d remote) {
Vector2d mid = new Vector2d();
mid.set((remote.x + x)/2, (remote.y + y)/2);
return mid;
}
public float length() {
return (float) Math.sqrt(x * x + y * y);
}
public float distance(Vector2d remote) {
float deltaX = remote.x - x;
float deltaY = remote.y - y;
return (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
}
}
private Vector2d finger1 = new Vector2d();
private Vector2d finger2 = new Vector2d();
private Vector2d pinchStartDistance = new Vector2d();
private Vector2d pinchMidPoint;
private Vector2d fingerStartPoint = new Vector2d();
private long gestureStartTime;
private Marker selectedMarker;
#Override
public boolean onTouch(View v, MotionEvent event) {
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
finger1.set(event.getX(), event.getY());
if (mode == TAP) {
if (finger1.distance(fingerStartPoint) < DOUBLE_TAP_MAX_DISTANCE) {
mode = DOUBLE_TAP;
} else {
mode = NONE;
gestureStartTime = SystemClock.uptimeMillis();
}
} else {
gestureStartTime = SystemClock.uptimeMillis();
}
fingerStartPoint.set(event.getX(), event.getY());
break;
case MotionEvent.ACTION_POINTER_DOWN:
finger2.set(event.getX(1), event.getY(1));
pinchStartDistance.set(Math.abs(finger1.x - finger2.x), Math.abs(finger1.y - finger2.y));
Log.d(TAG, String.format("pinch start distance = %f, %f", pinchStartDistance.x, pinchStartDistance.y));
if (pinchStartDistance.length() > MIN_PINCH_DISTANCE) {
if (pinchStartDistance.x < MIN_PINCH_DISTANCE) {
pinchStartDistance.x = MIN_PINCH_DISTANCE;
}
if (pinchStartDistance.y < MIN_PINCH_DISTANCE) {
pinchStartDistance.y = MIN_PINCH_DISTANCE;
}
pinchMidPoint = finger1.avgVector(finger2);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
if (mode == ZOOM) {
Vector2d pinchEndDistance = new Vector2d();
pinchEndDistance.set(Math.abs(finger1.x - finger2.x), Math.abs(finger1.y - finger2.y));
if (pinchEndDistance.x < MIN_PINCH_DISTANCE) {
pinchEndDistance.x = MIN_PINCH_DISTANCE;
}
if (pinchEndDistance.y < MIN_PINCH_DISTANCE) {
pinchEndDistance.y = MIN_PINCH_DISTANCE;
}
Log.d(TAG, String.format("pinch end distance = %f, %f", pinchEndDistance.x, pinchEndDistance.y));
zoom(pinchMidPoint, pinchStartDistance.x/pinchEndDistance.x, pinchStartDistance.y/pinchEndDistance.y);
// Set mode to "POST_GESTURE" so that when the other finger lifts the handler won't think it was a
// tap or something.
mode = POST_GESTURE;
} else if (mode == NONE) {
// The finger wasn't moved enough for it to be considered a "drag", so it is either a tap
// or a "long press", depending on how long it was down.
if ((SystemClock.uptimeMillis() - gestureStartTime) < LONG_PRESS_THRESHOLD_MS) {
Log.d(TAG, "mode=TAP");
mode = TAP;
selectedMarker = checkForMarker(finger1);
if (selectedMarker != null) {
Log.d(TAG, "Selected marker, mode=NONE");
mode = NONE;
((Activity) parent).showDialog(ResultsActivity.DIALOG_MARKER_ID);
}
}
else {
Log.d(TAG, "mode=LONG_PRESS");
addMarker(finger1);
requestRender();
}
} else if (mode == DOUBLE_TAP && (SystemClock.uptimeMillis() - gestureStartTime) < MAX_DOUBLE_TAP_MS) {
// The finger was again not moved enough for it to be considered a "drag", so it is
// a double-tap. Change the center point and zoom in.
Log.d(TAG, "mode=DOUBLE_TAP");
zoom(fingerStartPoint, 0.5f, 0.5f);
mode = NONE;
} else {
mode = NONE;
Log.d(TAG, "mode=NONE" );
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == NONE || mode == TAP || mode == DOUBLE_TAP) {
finger1.set(event.getX(), event.getY());
if (finger1.distance(fingerStartPoint) > MIN_DRAG_DISTANCE) {
Log.d(TAG, "mode=DRAG" );
mode = DRAG;
scroll(fingerStartPoint.x - finger1.x, fingerStartPoint.y - finger1.y);
}
}
else if (mode == DRAG) {
scroll(finger1.x - event.getX(), finger1.y - event.getY());
finger1.set(event.getX(), event.getY());
}
else if (mode == ZOOM) {
for (int i=0; i<event.getPointerCount(); i++) {
if (event.getPointerId(i) == 0) {
finger1.set(event.getX(i), event.getY(i));
}
else if (event.getPointerId(i) == 1) {
finger2.set(event.getX(i), event.getY(i));
}
else {
Log.w(TAG, String.format("Unknown motion event pointer id: %d", event.getPointerId(i)));
}
}
}
break;
}
return true;
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(MotionEvent event) {
String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_" ).append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid " ).append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")" );
}
sb.append("[" );
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#" ).append(i);
sb.append("(pid " ).append(event.getPointerId(i));
sb.append(")=" ).append((int) event.getX(i));
sb.append("," ).append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";" );
}
sb.append("]" );
Log.d(TAG, sb.toString());
}
//This code is to handle the gestures detection
final Handler handler = new Handler();
private Runnable mLongPressRunnable;
detector = new GestureDetector(this, new MyGestureDectector());
view.setOnTouchListener(new OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#SuppressWarnings("deprecation")
#Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
handler.postDelayed(mLongPressRunnable, 1000);
}
if ((event.getAction() == MotionEvent.ACTION_MOVE)
|| (event.getAction() == MotionEvent.ACTION_UP)) {
handler.removeCallbacks(mLongPressRunnable);
}
}
return true;
}
});
mLongPressRunnable = new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "long", Toast.LENGTH_SHORT)
.show();
}
};
class MyGestureDectector implements GestureDetector.OnDoubleTapListener,
OnGestureListener {
//Implement all the methods
}

Categories

Resources