Java Code Examples for android.view.View#onTouchEvent()

The following examples show how to use android.view.View#onTouchEvent() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * if the view (v) wants to handle this event.
 *
 * @param v
 * @param event
 * @return
 */
private boolean childNeedHandleTouchEvent(View v, MotionEvent event) {
    if (v == null)
        return false;

    int[] loc = new int[2];
    v.getLocationOnScreen(loc);
    int left = loc[0], top = loc[1];

    if (event.getRawX() > left && event.getRawX() < left + v.getWidth()
            && event.getRawY() > top
            && event.getRawY() < top + v.getHeight()) {
        return v.onTouchEvent(event);
    }

    return false;
}
 
Example 2
Source Project: ZListVIew   File: ZSwipeItem.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 判断View是否要去处理触摸事件
 * 
 * @param v
 * @param event
 * @return
 */
private boolean childNeedHandleTouchEvent(View v, MotionEvent event) {
	if (v == null)
		return false;

	int[] loc = new int[2];
	v.getLocationOnScreen(loc);
	int left = loc[0];
	int top = loc[1];

	if (event.getRawX() > left && event.getRawX() < left + v.getWidth()
			&& event.getRawY() > top
			&& event.getRawY() < top + v.getHeight()) {
		return v.onTouchEvent(event);
	}

	return false;
}
 
Example 3
Source Project: OpenHub   File: ViewUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static void virtualClick(final View view, int pressTime){
    long downTime = System.currentTimeMillis();
    int width = view.getWidth();
    int height = view.getHeight();
    float x = view.getX() + width / 2;
    float y = view.getY() + height / 2;

    MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
    long upTime = downTime + pressTime;
    final MotionEvent upEvent = MotionEvent.obtain(upTime, upTime, MotionEvent.ACTION_UP, x, y, 0);

    view.onTouchEvent(downEvent);
    view.onTouchEvent(upEvent);

    downEvent.recycle();
    upEvent.recycle();
}
 
Example 4
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    view.onTouchEvent(motionEvent);

    float xRaw = motionEvent.getRawX();

    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (canCancel(xRaw, halfWidthScreen) && isSpeakButtonLongPressed) {
                recordTouchEventListener.onCancelClick(view);
                isSpeakButtonLongPressed = false;
            }
            break;
        case MotionEvent.ACTION_UP:
            if (isSpeakButtonLongPressed) {
                recordTouchEventListener.onStopClick(view);
                isSpeakButtonLongPressed = false;
            }
            break;
    }
    return true;
}
 
Example 5
@Override
public boolean onTouch(View v, MotionEvent event) {
    final int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            onFingerDown(v, event.getX(), event.getY());
            v.onTouchEvent(event); // Fix for views, to handle clicks
            return true; // fix for scroll, when ACTION_UP & ACTION_CANCEL not come

        case MotionEvent.ACTION_MOVE:
            onFingerMove(event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            onFingerUp();
            break;
    }
    return false;
}
 
Example 6
@Override
public boolean onSingleTapUp(MotionEvent e) {
  int position = mDecor.findHeaderPositionUnder((int) e.getX(), (int) e.getY());
  if (position != -1) {
    View headerView = mDecor.getHeaderView(mRecyclerView, position);
    long headerId = getAdapter().getHeaderId(position);
    mOnHeaderClickListener.onHeaderClick(headerView, position, headerId);
    mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
    headerView.onTouchEvent(e);
    return true;
  }
  return false;
}
 
Example 7
Source Project: Memory-capsule   File: Means.java    License: Apache License 2.0 5 votes vote down vote up
public static void setViewonlick(View view,float X,float Y){
    long dowmTime= SystemClock.uptimeMillis();
    MotionEvent dowmEvent=MotionEvent.obtain(dowmTime,dowmTime,MotionEvent.ACTION_DOWN,X,Y,0);
    dowmTime+=1000;
    MotionEvent upEvent=MotionEvent.obtain(dowmTime,dowmTime,MotionEvent.ACTION_UP,X,Y,0);
    view.onTouchEvent(dowmEvent);
    view.onTouchEvent(upEvent);
    dowmEvent.recycle();
    upEvent.recycle();
}
 
Example 8
Source Project: UltimateAndroid   File: SwipeLayout.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * if the view (v) wants to handle this event.
 * @param v
 * @param event
 * @return
 */
private boolean childNeedHandleTouchEvent(View v, MotionEvent event){
    if(v == null)   return false;

    int[] loc = new int[2];
    v.getLocationOnScreen(loc);
    int left = loc[0], top = loc[1];

    if(event.getRawX() > left && event.getRawX() < left + v.getWidth()
            && event.getRawY() > top && event.getRawY() < top + v.getHeight()){
        return v.onTouchEvent(event);
    }

    return false;
}
 
Example 9
@Override
public boolean onSingleTapUp(MotionEvent e) {
  int position = mDecor.findHeaderPositionUnder((int) e.getX(), (int) e.getY());
  if (position != -1) {
    View headerView = mDecor.getHeaderView(mRecyclerView, position);
    long headerId = getAdapter().getHeaderId(position);
    mOnHeaderClickListener.onHeaderClick(headerView, position, headerId);
    mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
    headerView.onTouchEvent(e);
    return true;
  }
  return false;
}
 
Example 10
Source Project: delion   File: LocationBarTablet.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mTargets == null) return true;

    View selectedTarget = null;
    float selectedDistance = 0;
    // newX and newY are in the coordinates of the selectedTarget.
    float newX = 0;
    float newY = 0;
    for (View target : mTargets) {
        if (!target.isShown()) continue;

        mCachedTargetBounds.set(0, 0, target.getWidth(), target.getHeight());
        offsetDescendantRectToMyCoords(target, mCachedTargetBounds);
        float x = event.getX();
        float y = event.getY();
        float dx = distanceToRange(
                mCachedTargetBounds.left, mCachedTargetBounds.right, x);
        float dy = distanceToRange(
                mCachedTargetBounds.top, mCachedTargetBounds.bottom, y);
        float distance = Math.abs(dx) + Math.abs(dy);
        if (selectedTarget == null || distance < selectedDistance) {
            selectedTarget = target;
            selectedDistance = distance;
            newX = x + dx;
            newY = y + dy;
        }
    }

    if (selectedTarget == null) return false;

    event.setLocation(newX, newY);
    return selectedTarget.onTouchEvent(event);
}
 
Example 11
Source Project: android-notepad   File: RichEditWidgetView.java    License: MIT License 5 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event){
	v.onTouchEvent(event);
	if (event.getAction() == MotionEvent.ACTION_UP){
		if (lastSelectionStart != lastSelectionEnd){
			show();
			Utils.hideSoftKeyboard(v);
		}else{
			hide();
			Utils.showSoftKeyboard(v);
		}
	}
	return true;
}
 
Example 12
private boolean handleMoveEvent(@Nullable final View view, @NonNull final MotionEvent motionEvent) {
    if (mVelocityTracker == null || mCurrentView == null) {
        return false;
    }

    mVelocityTracker.addMovement(motionEvent);

    float deltaX = motionEvent.getX() - mDownX;
    float deltaY = motionEvent.getY() - mDownY;

    if (Math.abs(deltaX) > mSlop && Math.abs(deltaX) > Math.abs(deltaY)) {
        if (!mSwiping) {
            mActiveSwipeCount++;
            onStartSwipe(mCurrentView, mCurrentPosition);
        }
        mSwiping = true;
        mListViewWrapper.getListView().requestDisallowInterceptTouchEvent(true);

        /* Cancel ListView's touch (un-highlighting the item) */
        if (view != null) {
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
            view.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }
    }

    if (mSwiping) {
        if (mCanDismissCurrent) {
            ViewHelper.setTranslationX(mSwipingView, deltaX);
            ViewHelper.setAlpha(mSwipingView, Math.max(mMinimumAlpha, Math.min(1, 1 - 2 * Math.abs(deltaX) / mViewWidth)));
        } else {
            ViewHelper.setTranslationX(mSwipingView, deltaX * 0.1f);
        }
        return true;
    }
    return false;
}
 
Example 13
@Override
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
    boolean viewClosed = false;
    if (!(v instanceof SwipeableViewProvider) || ((SwipeableViewProvider) v).canBeSwiped() ||
            isMoving()) {
        viewClosed = handleTouchEvent(v, event);
    }

    // If the view is not being closed due to the touch event, give the touch event back to the
    // target view in case they have their own custom touch handling
    return viewClosed || v.onTouchEvent(event);
}
 
Example 14
Source Project: AndroidAutoClick   File: MainActivity.java    License: MIT License 5 votes vote down vote up
private void setSimulateClick(View view, float x, float y) {

		long downTime = SystemClock.uptimeMillis();
		final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
				MotionEvent.ACTION_DOWN, x, y, 0);
		downTime += 1000;
		final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
				MotionEvent.ACTION_UP, x, y, 0);
		view.onTouchEvent(downEvent);
		view.onTouchEvent(upEvent);
		downEvent.recycle();
		upEvent.recycle();
	}
 
Example 15
Source Project: AndroidAutoClick   File: MotionEventActivity.java    License: MIT License 5 votes vote down vote up
private void setSimulateClick(View view, float x, float y) {

        long downTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
        downTime += 1000;
        final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_UP, x, y, 0);
        view.onTouchEvent(downEvent);
        view.onTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }
 
Example 16
@Override
public boolean onSingleTapUp(MotionEvent e) {
  int position = mDecor.findHeaderPositionUnder((int) e.getX(), (int) e.getY());
  if (position != -1) {
    View headerView = mDecor.getHeaderView(mRecyclerView, position);
    long headerId = getAdapter().getHeaderId(position);
    mOnHeaderClickListener.onHeaderClick(headerView, position, headerId);
    mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
    headerView.onTouchEvent(e);
    return true;
  }
  return false;
}
 
Example 17
private boolean handleDownEvent(@Nullable final View view, @NonNull final MotionEvent motionEvent) {
    if (!mSwipeEnabled) {
        return false;
    }

    View downView = findDownView(motionEvent);
    if (downView == null) {
        return false;
    }

    int downPosition = AdapterViewUtil.getPositionForView(mListViewWrapper, downView);
    mCanDismissCurrent = isDismissable(downPosition);

    /* Check if we are processing the item at this position */
    if (mCurrentPosition == downPosition || downPosition >= mVirtualListCount) {
        return false;
    }

    if (view != null) {
        view.onTouchEvent(motionEvent);
    }

    disableHorizontalScrollContainerIfNecessary(motionEvent, downView);

    mDownX = motionEvent.getX();
    mDownY = motionEvent.getY();

    mCurrentView = downView;
    mSwipingView = getSwipeView(downView);
    mCurrentPosition = downPosition;

    mVelocityTracker = VelocityTracker.obtain();
    mVelocityTracker.addMovement(motionEvent);
    return true;
}
 
Example 18
private boolean handleDownEvent(final View view, final MotionEvent motionEvent) {
    mDisallowSwipe = false;
    if (mPaused) {
        return false;
    }

    // Find the child view that was touched (perform a hit test)
    Rect rect = new Rect();
    int childCount = mListView.getChildCount();
    int[] listViewCoords = new int[2];
    mListView.getLocationOnScreen(listViewCoords);
    int x = (int) motionEvent.getRawX() - listViewCoords[0];
    int y = (int) motionEvent.getRawY() - listViewCoords[1];
    View child;
    for (int i = 0; i < childCount; i++) {
        child = mListView.getChildAt(i);
        child.getHitRect(rect);
        if (rect.contains(x, y)) {
            mDownView = child;
            break;
        }
    }

    if (mDownView != null && mDownView instanceof ContextualUndoView) {
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        int downPosition = AdapterViewUtil.getPositionForView(mListView, mDownView);

        if (mDismissableManager != null) {
            long downId = mListView.getAdapter().getItemId(downPosition);
            if (!mDismissableManager.isDismissable(downId, downPosition)) {
                /* Cancel, not dismissable */
                return false;
            }
        }

        mTouchChildTouched = !mIsParentHorizontalScrollContainer && mResIdOfTouchChild == 0;

        if (mResIdOfTouchChild != 0) {
            mIsParentHorizontalScrollContainer = false;

            final View childView = mDownView.findViewById(mResIdOfTouchChild);
            if (childView != null) {
                final Rect childRect = getChildViewRect(mListView, childView);
                if (childRect.contains((int) motionEvent.getX(), (int) motionEvent.getY())) {
                    mTouchChildTouched = true;
                    mListView.requestDisallowInterceptTouchEvent(true);
                }
            }
        }

        if (mIsParentHorizontalScrollContainer) {
            // Do it now and don't wait until the user moves more than
            // the slop factor.
            mTouchChildTouched = true;
            mListView.requestDisallowInterceptTouchEvent(true);
        }

        mDownY = motionEvent.getRawY();
        mDownPosition = AdapterViewUtil.getPositionForView(mListView, mDownView);

        if (mTouchChildTouched) {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        } else {
            mVelocityTracker = null;
        }
    }
    view.onTouchEvent(motionEvent);
    return true;
}
 
Example 19
/**
 * Called when a touch event is dispatched to a view. This allows listeners to
 * get a chance to respond before the target view.
 *
 * @param v     The view the touch event has been dispatched to.
 * @param event The MotionEvent object containing full information about
 *              the event.
 * @return True if the listener has consumed the event, false otherwise.
 */
@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.d("SCANNER", "fragment got touch");
    boolean g = gestureDetector.onTouchEvent(event);

    return g || v.onTouchEvent(event);
}
 
Example 20
/**
 * Called when a touch event is dispatched to a view. This allows listeners to
 * get a chance to respond before the target view.
 *
 * @param v     The view the touch event has been dispatched to.
 * @param event The MotionEvent object containing full information about
 *              the event.
 * @return True if the listener has consumed the event, false otherwise.
 */
@Override
public boolean onTouch(View v, MotionEvent event) {
    boolean b = scaleGestureDetector.onTouchEvent(event);

    boolean c = gestureDetector.onTouchEvent(event);

    return b || c || v.onTouchEvent(event);
}