Example usage for android.view View getHitRect

List of usage examples for android.view View getHitRect

Introduction

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

Prototype

public void getHitRect(Rect outRect) 

Source Link

Document

Hit rectangle in parent's coordinates

Usage

From source file:com.daycle.daycleapp.custom.swipelistview.itemmanipulation.swipedismiss.SwipeTouchListener.java

/**
 * Returns the child {@link View} that was touched, by performing a hit test.
 *
 * @param motionEvent the {@link MotionEvent} to find the {@code View} for.
 *
 * @return the touched {@code View}, or {@code null} if none found.
 *///  w w  w .j  av a2  s .com
@Nullable
private View findDownView(@NonNull final MotionEvent motionEvent) {
    Rect rect = new Rect();
    int childCount = mListViewWrapper.getChildCount();
    int x = (int) motionEvent.getX();
    int y = (int) motionEvent.getY();
    View downView = null;
    for (int i = 0; i < childCount && downView == null; i++) {
        View child = mListViewWrapper.getChildAt(i);
        if (child != null) {
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                downView = child;
            }
        }
    }
    return downView;
}

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  w w w  .  java2 s  .c om*/
        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:org.faudroids.boredrudolf.ui.CustomSwipeRefreshLayout.java

private boolean canChildrenScrollUp(View view, MotionEvent event) {
    if (view instanceof ViewGroup) {
        final ViewGroup viewgroup = (ViewGroup) view;
        int count = viewgroup.getChildCount();
        for (int i = 0; i < count; ++i) {
            View child = viewgroup.getChildAt(i);
            Rect bounds = new Rect();
            child.getHitRect(bounds);
            if (bounds.contains((int) event.getX(), (int) event.getY())) {
                return canViewScrollUp(child, event);
            }/*from  w ww  .  j a  va  2  s . com*/
        }
    }

    return false;
}

From source file:org.faudroids.boredrudolf.ui.CustomSwipeRefreshLayout.java

private boolean canChildrenScrollHorizontally(View view, MotionEvent event, int direction) {
    if (view instanceof ViewGroup) {
        final ViewGroup viewgroup = (ViewGroup) view;
        int count = viewgroup.getChildCount();
        for (int i = 0; i < count; ++i) {
            View child = viewgroup.getChildAt(i);
            Rect bounds = new Rect();
            child.getHitRect(bounds);
            if (bounds.contains((int) event.getX(), (int) event.getY())) {
                if (DEBUG)
                    Log.d(TAG, "in child " + child.getClass().getName());
                return canViewScrollHorizontally(child, event, direction);
            }// w w  w .  j a v  a  2s .  c o m
        }
    }
    return false;
}

From source file:com.me.harris.androidanimations._06_touch.swipelistview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(View, MotionEvent)
 *///from w  ww.  j  av a  2  s. co m
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (paused) {
            return false;
        }
        swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE;

        int childCount = swipeListView.getChildCount();
        int[] listViewCoords = new int[2];
        swipeListView.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 = swipeListView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                setParentView(child);
                setFrontView(child.findViewById(swipeFrontView));
                downX = motionEvent.getRawX();
                downPosition = swipeListView.getPositionForView(child);

                frontView.setClickable(!opened.get(downPosition));
                frontView.setLongClickable(!opened.get(downPosition));

                velocityTracker = VelocityTracker.obtain();
                velocityTracker.addMovement(motionEvent);
                if (swipeBackView > 0) {
                    setBackView(child.findViewById(swipeBackView));
                }
                break;
            }
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (velocityTracker == null || !swiping) {
            break;
        }

        float deltaX = motionEvent.getRawX() - downX;
        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        if (!opened.get(downPosition)) {
            if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) {
                velocityX = 0;
            }
            if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && velocityTracker.getXVelocity() < 0) {
                velocityX = 0;
            }
        }
        float velocityY = Math.abs(velocityTracker.getYVelocity());
        boolean swap = false;
        boolean swapRight = false;
        if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY < velocityX) {
            swapRight = velocityTracker.getXVelocity() > 0;
            if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) {
                swap = false;
            } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) {
                swap = false;
            } else {
                swap = true;
            }
        } else if (Math.abs(deltaX) > viewWidth / 2) {
            swap = true;
            swapRight = deltaX > 0;
        }
        generateAnimate(frontView, swap, swapRight, downPosition);

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;
        // change clickable front view
        if (swap) {
            frontView.setClickable(opened.get(downPosition));
            frontView.setLongClickable(opened.get(downPosition));
        }
        frontView = null;
        backView = null;
        this.downPosition = ListView.INVALID_POSITION;
        swiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused) {
            break;
        }

        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        float velocityY = Math.abs(velocityTracker.getYVelocity());

        float deltaX = motionEvent.getRawX() - downX;
        float deltaMode = Math.abs(deltaX);
        if (swipeMode == SwipeListView.SWIPE_MODE_NONE) {
            deltaMode = 0;
        } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) {
            if (opened.get(downPosition)) {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) {
                    deltaMode = 0;
                }
            } else {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) {
                    deltaMode = 0;
                }
            }
        }
        if (deltaMode > slop && swipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE
                && velocityY < velocityX) {
            swiping = true;
            boolean swipingRight = (deltaX > 0);
            if (opened.get(downPosition)) {
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            } else {
                if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_CHECK) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHECK;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHECK) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHECK;
                } else {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                    | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
        }

        if (swiping) {
            if (opened.get(downPosition)) {
                deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset;
            }
            move(deltaX);
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:de.timroes.swipetodismiss.SwipeDismissList.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }/*from   ww w  .ja v  a  2s  . com*/

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused || !mEnabled) {
            return false;
        }
        resetSwipeState();

        // TODO: ensure this is a finger, and set a flag

        // 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)) {
                if (mSwipeLayout != 0) {
                    View swipeView = child.findViewById(mSwipeLayout);
                    if (swipeView != null) {
                        mDownView = swipeView;
                        break;
                    }
                }
                mDownView = child;
                break;
            }
        }

        if (mDownView != null) {
            mDownX = motionEvent.getRawX();
            mDownY = motionEvent.getRawY();
            mDownPosition = mListView.getPositionForView(mDownView);

            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX
                && mSwiping && isDirectionValid(mVelocityTracker.getXVelocity())
                && deltaX >= mViewWidth * 0.2f) {
            dismiss = true;
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss) {
            // dismiss
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            ++mDismissAnimationRefCount;
            mPaused = true;
            animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss(downView, downPosition);
                        }
                    });
        } else {
            // cancel
            animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        resetSwipeState();
        break;
    }

    case MotionEvent.ACTION_MOVE: {

        if (mUndoPopup.isShowing()) {
            // Send a delayed message to hide popup
            mHandler.sendMessageDelayed(mHandler.obtainMessage(mDelayedMsgId), mAutoHideDelay);
        }

        if (mVelocityTracker == null || mPaused || !mEnabled) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        // Only start swipe in correct direction
        if (isDirectionValid(deltaX)) {
            if (Math.abs(deltaX) > mSlop && Math.abs(deltaX) > Math.abs(deltaY)) {
                mSwiping = true;
                mListView.requestDisallowInterceptTouchEvent(true);

                // Cancel ListView's touch (un-highlighting the item)
                MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
                cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                        .getActionIndex(motionEvent) << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                mListView.onTouchEvent(cancelEvent);
                cancelEvent.recycle();
            }
        } else {
            // If we swiped into wrong direction, act like this was the new
            // touch down point
            mDownX = motionEvent.getRawX();
            deltaX = 0;
        }

        if (mSwiping) {
            setTranslationX(mDownView, deltaX);
            setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:gaobq.android.easyslidingmenu.CustomViewAbove.java

private boolean isInIgnoredView(MotionEvent ev) {
    Rect rect = new Rect();
    for (View v : mIgnoredViews) {
        v.getHitRect(rect);
        if (rect.contains((int) ev.getX(), (int) ev.getY()))
            return true;
    }/*from w  w  w .j a va 2  s  . c  o m*/
    return false;
}

From source file:nz.ac.otago.psyanlab.common.designer.program.stage.StageView.java

/**
 * Maps a point to a position in the list.
 *
 * @param x X in local coordinate//from  w  w  w  .  j a v a 2  s  .  c om
 * @param y Y in local coordinate
 * @return The position of the item which contains the specified point, or {@link
 * #INVALID_POSITION} if the point does not intersect an item.
 */
private int pointToPosition(int x, int y) {
    Rect frame = mTouchFrame;
    if (frame == null) {
        mTouchFrame = new Rect();
        frame = mTouchFrame;
    }

    final int count = getChildCount();
    for (int i = count - 1; i >= 0; i--) {
        final View child = getChildAt(i);

        child.getHitRect(frame);
        if (frame.contains(x, y)) {
            return i;
        }
    }
    return NO_MATCHED_CHILD;
}

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

private void init(Context context) {
    if (!isInEditMode()) {
        mOverScroller = new OverScroller(context);
        mGestureListener = new GestureDetector.SimpleOnGestureListener() {
            @Override//from  w w w .j  a  v a  2  s. c  o m
            public boolean onDown(MotionEvent e) {
                if (DEBUG) {
                    Log.d(TAG, "onDown() y=" + mListY);
                }

                releaseEdgeEffects();
                mOverScroller.forceFinished(true);
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (DEBUG) {
                    Log.d(TAG, "onFling() y=" + mListY);
                }

                // Fling isn't needed
                if (mDeltaHeight < 0) {
                    return true;
                }

                mScrollDirection = velocityY > 0 ? 1 : -1;
                mOverScroller.fling(0, mListY, 0, (int) velocityY, 0, 0, -mDeltaHeight, 0);
                if (!awakenScrollBars()) {
                    ViewCompat.postInvalidateOnAnimation(ScheduleView.this);
                }
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                for (int i = 0; i < getChildCount(); i++) {
                    getChildAt(i).setPressed(false);
                }

                mListY -= (int) distanceY;
                recalculateOffset();

                positionItemViews();

                if (mListY == 0) {
                    mTopEdgeEffect.onPull(distanceY / (float) getHeight());
                    mTopEdgeEffectActive = true;
                }
                if (mListY == -mDeltaHeight) {
                    mBottomEdgeEffect.onPull(distanceY / (float) getHeight());
                    mBottomEdgeEffectActive = true;
                }

                if (!awakenScrollBars()) {
                    invalidate();
                }

                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                if (DEBUG) {
                    Log.d(TAG, "onLongPress() y=" + mListY);
                }

                View child;
                for (int i = 0; i < getChildCount(); i++) {
                    child = getChildAt(i);
                    child.getHitRect(mClickedViewBounds);
                    if (mClickedViewBounds.contains((int) e.getX(), (int) e.getY())) {
                        if (!mIsActionMode) {
                            mActionMode = startActionMode(mActionModeCallback);
                            mIsActionMode = true;
                        }

                        if (!child.isSelected()) {
                            mSelectedIds.add(mAdapter.getItemId(i));
                            child.setSelected(true);
                        } else {
                            mSelectedIds.remove(mAdapter.getItemId(i));
                            child.setSelected(false);
                        }

                        if (mSelectedIds.isEmpty()) {
                            finishActionMode();
                        }

                        invalidate();
                        return;
                    }
                }
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                if (DEBUG) {
                    Log.d(TAG, "onSingleTapConfirmed() y=" + mListY);
                }

                View child;
                for (int i = 0; i < getChildCount(); i++) {
                    child = getChildAt(i);
                    child.getHitRect(mClickedViewBounds);
                    if (mClickedViewBounds.contains((int) e.getX(), (int) e.getY())) {
                        if (!mIsActionMode) {
                            OnItemClickListener callback = getOnItemClickListener();

                            if (callback != null) {
                                callback.onItemClick(ScheduleView.this, child, i, mAdapter.getItemId(i));
                            }
                        } else {
                            if (!child.isSelected()) {
                                mSelectedIds.add(mAdapter.getItemId(i));
                                child.setSelected(true);
                            } else {
                                mSelectedIds.remove(mAdapter.getItemId(i));
                                child.setSelected(false);
                            }

                            if (mSelectedIds.isEmpty()) {
                                finishActionMode();
                            }

                            invalidate();
                        }
                        break;
                    }
                }
                return true;
            }
        };

        mGestureDetector = new GestureDetector(context, mGestureListener);
    }
}

From source file:com.aretha.slidemenu.SlideMenu.java

private boolean isTapContent(float x, float y) {
    final View content = mContent;
    if (null != content) {
        content.getHitRect(mContentHitRect);
        return mContentHitRect.contains((int) x, (int) y);
    }//w w  w.j  a v  a 2  s.  co m
    return false;
}