Search code examples
androidandroid-viewpagerandroid-animation

Android TouchEvent Modify


I am working with the ViewPager trying to accomplish some animations. One of them is trying to slide from left to right (default transition of view pager is from right to left). I have done that.

My problem is that I want to "hack" the touch event so I don't need to modify the view pager. For example, for the left to right transition, I will want to make some kind of mirroring with the X in the touch event passed to the view pager.

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    event.setX(Math.abs(event.getX() - getWidth());
    return super.onInterceptTouchEvent(event);
}

Solution

  • I finally did it.

    public boolean dispatchTouchEvent(MotionEvent event) {
        MotionEvent hackedEvent = MotionEvent.obtain(event.getDownTime(),
                event.getEventTime(), event.getAction(), (event.getX() - getWidth()) * -1,
                event.getY(), event.getMetaState());
        boolean result = super.dispatchTouchEvent(hackedEvent);
        hackedEvent.recycle();
        return result;
    };