How do I “restart” a touch event in Android?

折月煮酒 提交于 2019-12-24 03:03:48

问题


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!


回答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.



来源:https://stackoverflow.com/questions/6904222/how-do-i-restart-a-touch-event-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!