How do I "restart" a touch event in Android?
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:开发者_如何学JAVA {
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!
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
.
精彩评论