Example usage for android.view View dispatchTouchEvent

List of usage examples for android.view View dispatchTouchEvent

Introduction

In this page you can find the example usage for android.view View dispatchTouchEvent.

Prototype

public boolean dispatchTouchEvent(MotionEvent event) 

Source Link

Document

Pass the touch screen motion event down to the target view, or this view if it is the target.

Usage

From source file:Main.java

public static void dispatchCancelEvent(View view) {
    view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
            MotionEvent.ACTION_CANCEL, 0, 0, 0));
}

From source file:Main.java

public static void dispatchTouch(final View view, final long duration) {
    final long downTime = SystemClock.uptimeMillis();
    final long eventTime = SystemClock.uptimeMillis();
    final float x = view.getWidth() / 3;//0.0f;
    final float y = view.getHeight() / 3;//0.0f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    final int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState);

    // Dispatch touch event to view
    view.dispatchTouchEvent(motionEvent);

    new Handler().postDelayed(new Runnable() {
        @Override//from  w  w w .  j  a  v  a2  s  .  c o m
        public void run() {
            MotionEvent motionEvent = MotionEvent.obtain(downTime + duration, eventTime + duration,
                    MotionEvent.ACTION_UP, x, y, metaState);
            view.dispatchTouchEvent(motionEvent);
        }
    }, duration);
}

From source file:com.acbelter.scheduleview.ScheduleView.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    View child;
    for (int i = 0; i < getChildCount(); i++) {
        child = getChildAt(i);//from   ww  w.  ja va2  s .c o  m
        child.getHitRect(mClickedViewBounds);
        if (mClickedViewBounds.contains((int) e.getX(), (int) e.getY())) {
            if (DEBUG) {
                Log.d(TAG, "dispatchTouchEvent() to child " + i);
            }
            // FIXME Add fade animation
            child.dispatchTouchEvent(e);
        }
    }
    return mGestureDetector.onTouchEvent(e);
}

From source file:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    final float xf = ev.getX();
    final float yf = ev.getY();
    final RectF frame = mTouchRect;

    if (action == MotionEvent.ACTION_DOWN) {
        if (mMotionTarget != null) {
            // this is weird, we got a pen down, but we thought it was
            // already down!
            // We should probably send an ACTION_UP to the current
            // target.
            mMotionTarget = null;/*from   w ww  . j  a v a 2 s .  co m*/
        }
        // If we're disallowing intercept or if we're allowing and we didn't
        // intercept
        if (!onInterceptTouchEvent(ev)) {
            // reset this event's action (just to protect ourselves)
            ev.setAction(MotionEvent.ACTION_DOWN);
            // We know we want to dispatch the event down, find a child
            // who can handle it, start with the front-most child.

            final int count = getChildCount();
            final int[] childOrder = new int[count];

            for (int i = 0; i < count; i++) {
                childOrder[i] = getChildDrawingOrder(count, i);
            }

            for (int i = count - 1; i >= 0; i--) {
                final View child = getChildAt(childOrder[i]);
                if (child.getVisibility() == VISIBLE || child.getAnimation() != null) {

                    getScrolledTransformedChildRectangle(child, frame);

                    if (frame.contains(xf, yf)) {
                        // offset the event to the view's coordinate system
                        final float xc = xf - frame.left;
                        final float yc = yf - frame.top;
                        ev.setLocation(xc, yc);
                        if (child.dispatchTouchEvent(ev)) {
                            // Event handled, we have a target now.
                            mMotionTarget = child;
                            mTargetTop = frame.top;
                            mTargetLeft = frame.left;
                            return true;
                        }

                        break;
                    }
                }
            }
        }
    }

    boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || (action == MotionEvent.ACTION_CANCEL);

    // The event wasn't an ACTION_DOWN, dispatch it to our target if
    // we have one.
    final View target = mMotionTarget;
    if (target == null) {
        // We don't have a target, this means we're handling the
        // event as a regular view.
        ev.setLocation(xf, yf);
        return onTouchEvent(ev);
    }

    // if have a target, see if we're allowed to and want to intercept its
    // events
    if (onInterceptTouchEvent(ev)) {
        final float xc = xf - mTargetLeft;
        final float yc = yf - mTargetTop;
        ev.setAction(MotionEvent.ACTION_CANCEL);
        ev.setLocation(xc, yc);
        if (!target.dispatchTouchEvent(ev)) {
            // target didn't handle ACTION_CANCEL. not much we can do
            // but they should have.
        }
        // clear the target
        mMotionTarget = null;
        // Don't dispatch this event to our own view, because we already
        // saw it when intercepting; we just want to give the following
        // event to the normal onTouchEvent().
        return true;
    }

    if (isUpOrCancel) {
        mMotionTarget = null;
        mTargetTop = -1;
        mTargetLeft = -1;
    }

    // finally offset the event to the target's coordinate system and
    // dispatch the event.
    final float xc = xf - mTargetLeft;
    final float yc = yf - mTargetTop;
    ev.setLocation(xc, yc);

    return target.dispatchTouchEvent(ev);
}