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

The following examples show how to use android.view.View#onTouchEvent() . 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 check out the related API usage on the sidebar.
Example 1
Source File: ViewUtils.java    From OpenHub with GNU General Public License v3.0 7 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 2
Source File: BaseRippleDrawable.java    From android-base-mvp with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: QBRecordAudioButton.java    From ChatMessagesAdapter-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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 4
Source File: SwipeLayout.java    From o2oa with GNU Affero General Public License v3.0 6 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 5
Source File: ZSwipeItem.java    From ZListVIew with 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 6
Source File: RichEditWidgetView.java    From android-notepad with 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 7
Source File: SwipeTouchListener.java    From ListViewAnimations with Apache License 2.0 5 votes vote down vote up
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 8
Source File: StickyRecyclerHeadersTouchListener.java    From IndexRecyclerView with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: MotionEventActivity.java    From AndroidAutoClick with 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 10
Source File: MainActivity.java    From AndroidAutoClick with 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 11
Source File: SwipeToDismissTouchListener.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: SwipeTouchListener.java    From ListViewAnimations with Apache License 2.0 5 votes vote down vote up
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
Source File: LocationBarTablet.java    From delion with 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 14
Source File: StickyRecyclerHeadersTouchListener.java    From ZhiHu-TopAnswer with Apache License 2.0 5 votes vote down vote up
@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 15
Source File: SwipeLayout.java    From UltimateAndroid with 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 16
Source File: Means.java    From Memory-capsule with 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 17
Source File: StickyRecyclerHeadersTouchListener.java    From sticky-headers-recyclerview with Apache License 2.0 5 votes vote down vote up
@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 18
Source File: ContextualUndoListViewTouchListener.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
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
Source File: BarcodeCaptureFragment.java    From MVBarcodeReader with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: DocumentScannerFragment.java    From CVScanner with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 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);
}