//TODO: Step 5 - Subscribe to touch events in the view public override bool OnTouchEvent(MotionEvent ev) { Log.Debug(GetType().FullName, "Number of touches: {0}", ev.PointerCount); Log.Debug(GetType().FullName, "Touch Type: {0}", ev.Action); float width = _icon.IntrinsicWidth, height = _icon.IntrinsicHeight; //TODO: Step 6 - Detect scale events _scaleDetector.OnTouchEvent(ev); MotionEventActions action = ev.Action & MotionEventActions.Mask; int pointerIndex; switch (action) { case MotionEventActions.Down: _lastTouchX = ev.GetX (); _lastTouchY = ev.GetY (); _activePointerId = ev.GetPointerId (0); break; case MotionEventActions.Move: pointerIndex = ev.FindPointerIndex (_activePointerId); float x = ev.GetX (pointerIndex); float y = ev.GetY (pointerIndex); if (!_scaleDetector.IsInProgress) { // Only move the ScaleGestureDetector isn't already processing a gesture. float deltaX = x - _lastTouchX; float deltaY = y - _lastTouchY; _posX += deltaX; _posY += deltaY; Invalidate (); } _lastTouchX = x; _lastTouchY = y; break; case MotionEventActions.Up: case MotionEventActions.Cancel: // This events occur when something cancels the gesture (for example the // activity going in the background) or when the pointer has been lifted up. // We no longer need to keep track of the active pointer. _activePointerId = InvalidPointerId; break; case MotionEventActions.PointerUp: // We only want to update the last touch position if the the appropriate pointer // has been lifted off the screen. pointerIndex = (int)(ev.Action & MotionEventActions.PointerIndexMask) >> (int)MotionEventActions.PointerIndexShift; int pointerId = ev.GetPointerId (pointerIndex); if (pointerId == _activePointerId) { // This was our active pointer going up. Choose a new // action pointer and adjust accordingly int newPointerIndex = pointerIndex == 0 ? 1 : 0; _lastTouchX = ev.GetX (newPointerIndex); _lastTouchY = ev.GetY (newPointerIndex); _activePointerId = ev.GetPointerId (newPointerIndex); } break; } //Determine if the image is within the touch area if (Math.Abs(_scaleFactor - 1.0f) > 0.001) { width *= _scaleFactor; height *= _scaleFactor; } var rc = new RectF(_posX, _posY, _posX + width, _posY + height); float touchX = ev.GetX(), touchY = ev.GetY(); bool contains = rc.Contains(touchX, touchY); Log.Debug(GetType().FullName, "{0} - ({1},{2}) - CONTAINS: {3}", rc, touchX, touchY, contains); return true; }