2

I have a custom View and I'm overriding onTouchEvent(MotionEvent). I've implemented pinch-zoom in this view as described in the article Making Sense of Multitouch. The issue I'm having is that this view is ultimately used in a Gallery which needs touch events for scrolling. I've added logic to detect when my view has fully scrolled to the right/left and return false, but I think the Gallery needs the full MotionEvent motion in order to scroll. Here's where I return false:

case MotionEvent.ACTION_MOVE: {
    final int pointerIndex = event.findPointerIndex( getActivePointerId() );
    final float x = event.getX( pointerIndex );
    final float y = event.getY( pointerIndex );

    // Only move if the ScaleGestureDetector isn't processing a gesture.
    if ( !getScaleDetector().isInProgress() ) {
        if ( isDetectMovementX() ) {
            final float dx = x - getLastTouchX();
            setPosX( getPosX() + dx );
        }

        if ( isDetectMovementY() ) {
            final float dy = y - getLastTouchY();
            setPosY( getPosY() + dy );
        }

        invalidate();
    }

    setLastTouchX( x );
    setLastTouchY( y );

    if ( isAtXBound() ) {
        return false;
    }

    break;
}

The View zooms and translates, but the Gallery never scrolls. I'm wondering if there's a way to basically resend the MotionEvent as a "fresh" MotionEvent with the initial action being ACTION_DOWN. Thanks!

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131

1 Answers1

1

You can use MotionEvent's constructor to create a new one. See the article Android: How to create a MotionEvent?

Also, the obtain(...) method creates a new MotionEvent. Try MotionEvent e = MotionEvent.obtain(event);

See this link for more about obtain.

Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
  • If a `MotionEvent` is created, it is handled by your TouchListener. – Phil Aug 01 '11 at 21:00
  • Ah ha that's nice to know. I'm working with this to achieve what I want. I can already see it'll work, just need to do some tweaking. Thanks! – Jason Robinson Aug 01 '11 at 21:27
  • Actually it doesn't seem the the new `MotionEvent` is automatically signaled. In fact I never see that `MotionEvent` come through the onTouchEvent method. – Jason Robinson Aug 01 '11 at 21:43
  • That really surprises me. I just did some more looking around. I'm not sure this is the solution, but you could try using `dispatchTouchEvent`: http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent) – Phil Aug 01 '11 at 21:49
  • In fact, have you tried directly calling `onTouchEvent(event)` once you have made your copy of the `MotionEvent`? – Phil Aug 01 '11 at 21:50
  • That wouldn't work since I don't want it to go to "my" `onTouchEvent` in that case. From my observations `dispatchTouchEvent` also seems to just come back to my `onTouchEvent`. I'm seeing really odd behavior. I'm going to make a new question. Thanks for your help. – Jason Robinson Aug 02 '11 at 15:07