Java Code Examples for android.view.MotionEvent#offsetLocation()

The following examples show how to use android.view.MotionEvent#offsetLocation() . 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: AndroidXpraWindow.java    From xpra-client with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
	event.offsetLocation(v.getX(), v.getY());
	final int x = (int) Math.max(event.getX(), 0);
	final int y = (int) Math.max(event.getY(), 0);
	switch (event.getAction()) {
	case MotionEvent.ACTION_DOWN:
		setFocused(true);
		mouseAction(1, true, x, y);
		break;
	case MotionEvent.ACTION_MOVE:
		mouseAction(1, true, x, y);
		break;
	case MotionEvent.ACTION_UP:
		mouseAction(1, false, x, y);
		break;
	default:
		break;
	}
	return true;
}
 
Example 2
Source File: AlipayScrollView.java    From AlipayPullRefresh with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        flinging = false;
        lastProcessY = ev.getRawY();
        downTouchOffset = refreshing ? firstViewPosition - progressHeight : firstViewPosition;
        // 手指按下,动画结束
        scrollSpring.setAtRest();
    } else if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        if (parentView.getTouchingView() != this) {
            ev.offsetLocation(0, downTouchOffset);
        }
        onDragRelease();
        super.onInterceptTouchEvent(ev);
    }
    return super.onInterceptTouchEvent(ev);
}
 
Example 3
Source File: StickyNestedScrollView.java    From SwipeRecyclerView-master with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        redirectTouchesToStickyView = true;
    }
    if (redirectTouchesToStickyView) {
        redirectTouchesToStickyView = currentlyStickingView != null;
        if (redirectTouchesToStickyView) {
            redirectTouchesToStickyView =
                    ev.getY() <= (currentlyStickingView.getHeight() + stickyViewTopOffset) &&
                            ev.getX() >= getLeftForViewRelativeOnlyChild(currentlyStickingView) &&
                            ev.getX() <= getRightForViewRelativeOnlyChild(currentlyStickingView);
        }
    } else if (currentlyStickingView == null) {
        redirectTouchesToStickyView = false;
    }
    if (redirectTouchesToStickyView) {
        ev.offsetLocation(0, -1 * ((getScrollY() + stickyViewTopOffset)
                - getTopForViewRelativeOnlyChild(currentlyStickingView)));
    }
    return super.dispatchTouchEvent(ev);
}
 
Example 4
Source File: ViewUtils.java    From turbo-editor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Recursive helper method that applies transformations in post-order.
 *
 * @param ev the on-screen motion event
 */
private static void transformMotionEventToLocal(@NonNull View view, @NonNull MotionEvent ev) {
    final ViewParent parent = view.getParent();
    if (parent instanceof View) {
        final View vp = (View) parent;
        transformMotionEventToLocal(vp, ev);
        ev.offsetLocation(vp.getScrollX(), vp.getScrollY());
    } // TODO: Use reflections to access ViewRootImpl
    // else if (parent instanceof ViewRootImpl) {
    //    final ViewRootImpl vr = (ViewRootImpl) parent;
    //    ev.offsetLocation(0, vr.mCurScrollY);
    // }

    ev.offsetLocation(-view.getLeft(), -view.getTop());

    Matrix matrix = view.getMatrix();
    if (matrix != null) {
        ev.transform(matrix);
    }
}
 
Example 5
Source File: StickyScrollView.java    From MousePaint with MIT License 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
	if(ev.getAction()==MotionEvent.ACTION_DOWN){
		redirectTouchesToStickyView = true;
	}

	if(redirectTouchesToStickyView){
		redirectTouchesToStickyView = currentlyStickingView != null;
		if(redirectTouchesToStickyView){
			redirectTouchesToStickyView = 
				ev.getY()<=(currentlyStickingView.getHeight()+stickyViewTopOffset) && 
				ev.getX() >= getLeftForViewRelativeOnlyChild(currentlyStickingView) && 
				ev.getX() <= getRightForViewRelativeOnlyChild(currentlyStickingView);
		}
	}else if(currentlyStickingView == null){
		redirectTouchesToStickyView = false;
	}
	if(redirectTouchesToStickyView){
		ev.offsetLocation(0, -1*((getScrollY() + stickyViewTopOffset) - getTopForViewRelativeOnlyChild(currentlyStickingView)));
	}
	return super.dispatchTouchEvent(ev);
}
 
Example 6
Source File: ViewUtils.java    From turbo-editor with GNU General Public License v3.0 5 votes vote down vote up
@Override
boolean toLocalMotionEvent(@NonNull View view, @NonNull MotionEvent ev) {
    final int[] windowPosition = getWindowPosition(view);
    if (windowPosition == null) {
        return false;
    }

    ev.offsetLocation(-windowPosition[0], -windowPosition[1]);
    transformMotionEventToLocal(view, ev);
    return true;
}
 
Example 7
Source File: SmoothRefreshLayout.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
protected final boolean dispatchTouchEventSuper(MotionEvent ev) {
    if (!isEnabledOldTouchHandling()) {
        final int index = ev.findPointerIndex(mTouchPointerId);
        if (index < 0) {
            return super.dispatchTouchEvent(ev);
        }
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
            mOffsetConsumed = 0;
            mOffsetTotal = 0;
            mOffsetRemaining = mTouchSlop * 3;
        } else {
            if (!mIndicator.isAlreadyHere(IIndicator.START_POS)
                    && mIndicator.getRawOffset() != 0) {
                if (mOffsetRemaining > 0) {
                    mOffsetRemaining -= mTouchSlop;
                    if (isMovingHeader()) {
                        mOffsetTotal -= mOffsetRemaining;
                    } else if (isMovingFooter()) {
                        mOffsetTotal += mOffsetRemaining;
                    }
                }
                mOffsetConsumed +=
                        mIndicator.getRawOffset() < 0
                                ? mIndicator.getLastPos() - mIndicator.getCurrentPos()
                                : mIndicator.getCurrentPos() - mIndicator.getLastPos();
                mOffsetTotal += mIndicator.getRawOffset();
            }
            if (isVerticalOrientation()) {
                ev.offsetLocation(0, mOffsetConsumed - mOffsetTotal);
            } else {
                ev.offsetLocation(mOffsetConsumed - mOffsetTotal, 0);
            }
        }
    }
    return super.dispatchTouchEvent(ev);
}
 
Example 8
Source File: VerticalElasticityBounceEffect.java    From elasticity with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    protected void translateViewAndEvent(View view, boolean dir, float offset, MotionEvent event) {
        Log.d("wxy-motion", String.format("translateViewAndEvent setTag %s", offset));
        translateView(view, dir, offset);

//        view.setTranslationY(offset);
        event.offsetLocation(offset - event.getY(0), 0f);
    }
 
Example 9
Source File: FloatingView.java    From FloatingView with Apache License 2.0 5 votes vote down vote up
/**
 * Call addMovement and restore MotionEvent coordinate
 *
 * @param event {@link MotionEvent}
 */
private void addMovement(@NonNull MotionEvent event) {
    final float deltaX = event.getRawX() - event.getX();
    final float deltaY = event.getRawY() - event.getY();
    event.offsetLocation(deltaX, deltaY);
    mVelocityTracker.addMovement(event);
    event.offsetLocation(-deltaX, -deltaY);
}
 
Example 10
Source File: TimePickerClockDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    final int actionMasked = motionEvent.getActionMasked();
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        if (view instanceof ViewGroup) {
            mInitialTouchTarget = findNearestChild((ViewGroup) view,
                    (int) motionEvent.getX(), (int) motionEvent.getY());
        } else {
            mInitialTouchTarget = null;
        }
    }

    final View child = mInitialTouchTarget;
    if (child == null) {
        return false;
    }

    final float offsetX = view.getScrollX() - child.getLeft();
    final float offsetY = view.getScrollY() - child.getTop();
    motionEvent.offsetLocation(offsetX, offsetY);
    final boolean handled = child.dispatchTouchEvent(motionEvent);
    motionEvent.offsetLocation(-offsetX, -offsetY);

    if (actionMasked == MotionEvent.ACTION_UP
            || actionMasked == MotionEvent.ACTION_CANCEL) {
        mInitialTouchTarget = null;
    }

    return handled;
}
 
Example 11
Source File: WindowManagerContainer.java    From springy-heads with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    event.offsetLocation(getContainerX(v), getContainerY(v));
    HostFrameLayout frameLayout = getFrameLayout();
    if (frameLayout != null) {
        return frameLayout.dispatchTouchEvent(event);
    } else {
        return false;
    }
}
 
Example 12
Source File: ListPopupWindow.java    From material with Apache License 2.0 5 votes vote down vote up
/**
 * Emulates View.toLocalMotionEvent(). This implementation does not handle transformations
 * (scaleX, scaleY, etc).
 */
private boolean toLocalMotionEvent(View view, MotionEvent event) {
    final int[] loc = mTmpLocation;
    view.getLocationOnScreen(loc);
    event.offsetLocation(-loc[0], -loc[1]);
    return true;
}
 
Example 13
Source File: TwinklingRefreshLayout.java    From TwinklingRefreshLayout with Apache License 2.0 4 votes vote down vote up
private boolean detectNestedScroll(MotionEvent e) {
    final MotionEvent vtev = MotionEvent.obtain(e);
    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsets[0] = mNestedOffsets[1] = 0;
    }
    vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = e.getPointerId(0);
            mLastTouchX = (int) e.getX();
            mLastTouchY = (int) e.getY();
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            break;
        case MotionEventCompat.ACTION_POINTER_DOWN:
            mActivePointerId = e.getPointerId(actionIndex);
            mLastTouchX = (int) e.getX(actionIndex);
            mLastTouchY = (int) e.getY(actionIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            final int index = e.findPointerIndex(mActivePointerId);
            if (index < 0) {
                Log.e("TwinklingRefreshLayout", "Error processing scroll; pointer index for id " +
                        mActivePointerId + " not found. Did any MotionEvents get skipped?");
                return false;
            }

            final int x = (int) e.getX(index);
            final int y = (int) e.getY(index);

            int dx = mLastTouchX - x;
            int dy = mLastTouchY - y;

            if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) {
                dx -= mScrollConsumed[0];
                dy -= mScrollConsumed[1];
                vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
                // Updated the nested offsets
                mNestedOffsets[0] += mScrollOffset[0];
                mNestedOffsets[1] += mScrollOffset[1];
            }

            if (!mIsBeingDragged && Math.abs(dy) > mTouchSlop) {
                final ViewParent parent = getParent();
                if (parent != null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                mIsBeingDragged = true;
                if (dy > 0) {
                    dy -= mTouchSlop;
                } else {
                    dy += mTouchSlop;
                }
            }

            if (mIsBeingDragged) {
                mLastTouchY = y - mScrollOffset[1];

                final int scrolledDeltaY = 0;
                final int unconsumedY = dy - scrolledDeltaY;
                if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                    mLastTouchX -= mScrollOffset[0];
                    mLastTouchY -= mScrollOffset[1];
                    vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
                    mNestedOffsets[0] += mScrollOffset[0];
                    mNestedOffsets[1] += mScrollOffset[1];
                }
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            stopNestedScroll();
            mIsBeingDragged = false;
            mActivePointerId = INVALID_POINTER;
            break;
    }
    vtev.recycle();
    return true;
}
 
Example 14
Source File: NestedWebView.java    From firefox-echo-show with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    final MotionEvent event = MotionEvent.obtain(ev);
    final int action = ev.getActionMasked();

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsetY = 0;
    }

    final int eventY = (int) event.getY();
    event.offsetLocation(0, mNestedOffsetY);

    switch (action) {
        case MotionEvent.ACTION_MOVE:
            int deltaY = mLastY - eventY;

            if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                deltaY -= mScrollConsumed[1];
                event.offsetLocation(0, -mScrollOffset[1]);
                mNestedOffsetY += mScrollOffset[1];
            }

            mLastY = eventY - mScrollOffset[1];

            if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {
                mLastY -= mScrollOffset[1];
                event.offsetLocation(0, mScrollOffset[1]);
                mNestedOffsetY += mScrollOffset[1];
            }
            break;

        case MotionEvent.ACTION_DOWN:
            mLastY = eventY;
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            stopNestedScroll();
            break;

        default:
            // We don't care about other touch events
    }

    // Execute event handler from parent class in all cases
    boolean eventHandled = super.onTouchEvent(event);

    // Recycle previously obtained event
    event.recycle();

    return eventHandled;
}
 
Example 15
Source File: NestedWebView.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
@SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent ev) {
    boolean returnValue;
    MotionEvent event = MotionEvent.obtain(ev);
    final int action = MotionEventCompat.getActionMasked(event);
    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsetY = 0;
    }
    int eventY = (int) event.getY();
    event.offsetLocation(0, mNestedOffsetY);
    switch (action) {
        case MotionEvent.ACTION_MOVE:
            int deltaY = mLastY - eventY;
            // NestedPreScroll
            if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                deltaY -= mScrollConsumed[1];
                mLastY = eventY - mScrollOffset[1];
                event.offsetLocation(0, -mScrollOffset[1]);
                mNestedOffsetY += mScrollOffset[1];
            }
            returnValue = super.onTouchEvent(event);

            // NestedScroll
            if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {
                event.offsetLocation(0, mScrollOffset[1]);
                mNestedOffsetY += mScrollOffset[1];
                mLastY -= mScrollOffset[1];
            }
            break;
        case MotionEvent.ACTION_DOWN:
            returnValue = super.onTouchEvent(event);
            if (firstScroll) {
                mLastY = eventY - 5;
                firstScroll = false;
            } else {
                mLastY = eventY;
            }
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            break;
        default:
            returnValue = super.onTouchEvent(event);
            // end NestedScroll
            stopNestedScroll();
            break;
    }
    return returnValue;
}
 
Example 16
Source File: NestedWebView.java    From ForPDA with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent e) {
    final MotionEvent vtev = MotionEvent.obtain(e);
    final int action = MotionEventCompat.getActionMasked(e);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsets[0] = mNestedOffsets[1] = 0;
    }
    vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]);
    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            //Log.e("SUKA", "ACT DWN " + mScrollState);
            mLastTouchX = (int) (e.getX() + 0.5f);
            mLastTouchY = (int) (e.getY() + 0.5f);

            int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE;
            nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL;
            nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL;
            startNestedScroll(nestedScrollAxis);
            super.onTouchEvent(e);
        }
        break;

        case MotionEvent.ACTION_MOVE: {
            final int x = (int) (e.getX() + 0.5f);
            final int y = (int) (e.getY() + 0.5f);
            int dx = mLastTouchX - x;
            int dy = mLastTouchY - y;

            if (mScrollState == SCROLL_STATE_IDLE) {
                if (Math.abs(dx) < mTouchSlop && Math.abs(dy) < mTouchSlop) {
                    break;
                }
            }
            //Log.d("SUKA", "PREMOVE " + dy + " : " + mScrollState);
            final boolean preScrollConsumed = dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset);

            if (preScrollConsumed) {
                dx -= mScrollConsumed[0];
                dy -= mScrollConsumed[1];
                vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
                mNestedOffsets[0] += mScrollOffset[0];
                mNestedOffsets[1] += mScrollOffset[1];
            }

            if (preScrollConsumed) {
                setScrollState(SCROLL_STATE_NESTED_SCROLL);
            } else {
                mLastTouchX = x - mScrollOffset[0];
                mLastTouchY = y - mScrollOffset[1];

                if (dy < 0 && getScrollY() == 0) {
                    final boolean scrollConsumed = dispatchNestedScroll(0, 0, dx, dy, mScrollOffset);
                    if (scrollConsumed) {
                        mLastTouchX -= mScrollOffset[0];
                        mLastTouchY -= mScrollOffset[1];
                        vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]);
                        mNestedOffsets[0] += mScrollOffset[0];
                        mNestedOffsets[1] += mScrollOffset[1];
                        setScrollState(SCROLL_STATE_NESTED_SCROLL);
                    }
                } else {
                    if (dy != 0) {
                        setScrollState(SCROLL_STATE_SCROLL);
                    }
                    super.onTouchEvent(e);
                }
            }
            if (mScrollState != SCROLL_STATE_IDLE) {
                if (isLongClickable()) {
                    changeLongClickable(false);
                }
            }
            //Log.d("SUKA", "Move " + dy + " : " + mScrollState + " : ");
        }
        break;


        case MotionEvent.ACTION_UP: {
            //long dt = System.currentTimeMillis() - lastTouchTime;
            //Log.e("SUKA", "ACT UP " + mScrollState + " : dt=" + dt);
            if (mScrollState == SCROLL_STATE_NESTED_SCROLL) {
                e.setAction(MotionEvent.ACTION_CANCEL);
            }
            super.onTouchEvent(e);
            resetTouch();
            changeLongClickable(true);
        }
        break;

        case MotionEvent.ACTION_CANCEL: {
            //Log.e("SUKA", "ACT CANCEL " + mScrollState);
            super.onTouchEvent(e);
            resetTouch();
            changeLongClickable(true);
        }
        break;
    }
    vtev.recycle();
    return true;
}
 
Example 17
Source File: MyImageView.java    From AndroidUI with MIT License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent e) {

    final boolean canScrollVertically = true;

    final MotionEvent vtev = MotionEvent.obtain(e);
    final int action = MotionEventCompat.getActionMasked(e);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsets[0] = mNestedOffsets[1] = 0;
    }
    vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]);

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
            mLastTouchY = (int) (e.getY() + 0.5f);

            int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE;
            if (canScrollVertically) {
                nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL;
            }
            startNestedScroll(nestedScrollAxis);
        } break;
        case MotionEvent.ACTION_MOVE: {
            final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
            if (index < 0) {
                return false;
            }
            final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
            int dx = 0;
            int dy = mLastTouchY - y;

            dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset);

        } break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            stopNestedScroll();
            break;
    }

    vtev.recycle();
    return true;
}
 
Example 18
Source File: NestedWebView.java    From fingerpoetry-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    boolean returnValue = false;

    MotionEvent event = MotionEvent.obtain(ev);
    final int action = MotionEventCompat.getActionMasked(event);
    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsetY = 0;
    }
    int eventY = (int) event.getY();
    event.offsetLocation(0, mNestedOffsetY);
    switch (action) {
        case MotionEvent.ACTION_MOVE:
            int deltaY = mLastY - eventY;
            // NestedPreScroll
            if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                deltaY -= mScrollConsumed[1];
                mLastY = eventY - mScrollOffset[1];
                event.offsetLocation(0, -mScrollOffset[1]);
                mNestedOffsetY += mScrollOffset[1];
            }
            returnValue = super.onTouchEvent(event);

            // NestedScroll
            if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {
                event.offsetLocation(0, mScrollOffset[1]);
                mNestedOffsetY += mScrollOffset[1];
                mLastY -= mScrollOffset[1];
            }
            break;
        case MotionEvent.ACTION_DOWN:
            returnValue = super.onTouchEvent(event);
            mLastY = eventY;
            // start NestedScroll
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            returnValue = super.onTouchEvent(event);
            // end NestedScroll
            stopNestedScroll();
            break;
    }
    return returnValue;
}
 
Example 19
Source File: SlideLayout.java    From SlideLayout with GNU General Public License v2.0 4 votes vote down vote up
private boolean dispatchTouchEventStateShowBottomMenu(MotionEvent event){
	if(mBottomMenuStyle.mMenuBorder == 0){
		if(event.getAction() ==  MotionEvent.ACTION_DOWN){
			if(event.getY() < mBottomMenuStyle.mDragEdge){
				mDownX = event.getX();
				mDownY = event.getY();
				mPrevX = mDownX;
				mPrevY = mDownY;
				mDisX = 0f;
				mDisY = 0f;
			}
		}
		else if(event.getAction() ==  MotionEvent.ACTION_UP){
			mDownX = -1;		
			mDownY = -1;
		}
		else if(event.getAction() == MotionEvent.ACTION_MOVE && mDownX > 0 && mDownY > 0){
			if(event.getY() >= mPrevY){
				mDisY += event.getY() - mPrevY;
				mPrevY = event.getY();
				mPrevX = event.getX();
				
				if(mDisY > mBottomMenuStyle.mTouchSlop){
					mOffsetY = Math.max(-mBottomMenuStyle.mSize, Math.min(0, (int)(event.getY() - mViewHeight)));
					offsetViewY(mOffsetY);
					
					setState(mOffsetY > -mBottomMenuStyle.mSize ? ACTION_DRAG : ACTION_SHOW, TARGET_BOTTOM, OP.START_DRAG_BOTTOM_FROM_CONTENT);
					boolean result = cancelMotionEvent(event, getBottomMenuView());
											
					dispatchOffsetChangedEvent(0f, (float)-mOffsetY / (float)mBottomMenuStyle.mSize);
					
					return result;	
				}
			}
			else
				mDownY = -1;
		}
	}
	
	if(!mDragEnable || !mGestureDetector.onTouchEvent(event)){
		event.offsetLocation(0, -mBottomMenuStyle.mMenuBorder);
		
		if(!getBottomMenuView().dispatchTouchEvent(event)){
			event.offsetLocation(0, mBottomMenuStyle.mMenuBorder);
			return super.dispatchTouchEvent(event);
		}
	}		
	else{
		event.offsetLocation(0, -mBottomMenuStyle.mMenuBorder);
		getBottomMenuView().dispatchTouchEvent(event);
		event.offsetLocation(0, mBottomMenuStyle.mMenuBorder);
	}
	
	return true; 
}
 
Example 20
Source File: ChatHead.java    From springy-heads with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    super.onTouchEvent(event);

    if(xPositionSpring==null || yPositionSpring==null) return false;
    //Chathead view will set the correct active springs on touch
    Spring activeHorizontalSpring = xPositionSpring;
    Spring activeVerticalSpring = yPositionSpring;

    int action = event.getAction();
    float rawX = event.getRawX();
    float rawY = event.getRawY();
    float offsetX = rawX - downX;
    float offsetY = rawY - downY;
    boolean showCloseButton = manager.getActiveArrangement().shouldShowCloseButton(this);
    event.offsetLocation(manager.getChatHeadContainer().getViewX(this), manager.getChatHeadContainer().getViewY(this));
    if (action == MotionEvent.ACTION_DOWN) {
        if (velocityTracker == null) {
            velocityTracker = VelocityTracker.obtain();
        } else {
            velocityTracker.clear();

        }
        activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
        activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
        setState(ChatHead.State.FREE);
        downX = rawX;
        downY = rawY;
        downTranslationX = (float) activeHorizontalSpring.getCurrentValue();
        downTranslationY = (float) activeVerticalSpring.getCurrentValue();
        scaleSpring.setEndValue(.9f);
        activeHorizontalSpring.setAtRest();
        activeVerticalSpring.setAtRest();
        velocityTracker.addMovement(event);


    } else if (action == MotionEvent.ACTION_MOVE) {
        if (Math.hypot(offsetX, offsetY) > touchSlop) {
            isDragging = true;
            if (showCloseButton) {
                manager.getCloseButton().appear();
            }
        }
        velocityTracker.addMovement(event);

        if (isDragging) {
            manager.getCloseButton().pointTo(rawX, rawY);
            if (manager.getActiveArrangement().canDrag(this)) {
                double distanceCloseButtonFromHead = manager.getDistanceCloseButtonFromHead(rawX, rawY);
                if (distanceCloseButtonFromHead < CLOSE_ATTRACTION_THRESHOLD && showCloseButton) {
                    setState(ChatHead.State.CAPTURED);
                    activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                    activeVerticalSpring.setSpringConfig(SpringConfigsHolder.NOT_DRAGGING);
                    int[] coords = manager.getChatHeadCoordsForCloseButton(this);
                    activeHorizontalSpring.setEndValue(coords[0]);
                    activeVerticalSpring.setEndValue(coords[1]);
                    manager.getCloseButton().onCapture();

                } else {
                    setState(ChatHead.State.FREE);
                    activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
                    activeVerticalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
                    activeHorizontalSpring.setCurrentValue(downTranslationX + offsetX);
                    activeVerticalSpring.setCurrentValue(downTranslationY + offsetY);
                    manager.getCloseButton().onRelease();
                }

                velocityTracker.computeCurrentVelocity(1000);
            }

        }

    } else {
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            boolean wasDragging = isDragging;
            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
            activeHorizontalSpring.setSpringConfig(SpringConfigsHolder.DRAGGING);
            isDragging = false;
            scaleSpring.setEndValue(1);
            int xVelocity = (int) velocityTracker.getXVelocity();
            int yVelocity = (int) velocityTracker.getYVelocity();
            velocityTracker.recycle();
            velocityTracker = null;
            if(xPositionSpring!=null && yPositionSpring!=null) {
                boolean touchUpHandled = manager.getActiveArrangement().handleTouchUp(this, xVelocity, yVelocity, activeHorizontalSpring, activeVerticalSpring, wasDragging);
            }
        }
    }

    return true;
}