Example usage for android.view MotionEvent getY

List of usage examples for android.view MotionEvent getY

Introduction

In this page you can find the example usage for android.view MotionEvent getY.

Prototype

public final float getY() 

Source Link

Document

#getY(int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

From source file:com.android.mail.browse.ConversationContainer.java

/**
 * Touch slop code was copied from {@link ScrollView#onInterceptTouchEvent(MotionEvent)}.
 *//*from  ww  w.j a  v  a2 s .  c  om*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mTouchInitialized) {
        mTouchInitialized = true;
    }

    // no interception when WebView handles the first DOWN
    if (mWebView.isHandlingTouch()) {
        return false;
    }

    boolean intercept = false;
    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_POINTER_DOWN:
        LogUtils.d(TAG, "Container is intercepting non-primary touch!");
        intercept = true;
        mMissedPointerDown = true;
        requestDisallowInterceptTouchEvent(true);
        break;

    case MotionEvent.ACTION_DOWN:
        mLastMotionY = ev.getY();
        mActivePointerId = ev.getPointerId(0);
        break;

    case MotionEvent.ACTION_MOVE:
        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
        final float y = ev.getY(pointerIndex);
        final int yDiff = (int) Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop) {
            mLastMotionY = y;
            intercept = true;
        }
        break;
    }

    //        LogUtils.v(TAG, "in Container.InterceptTouch. action=%d x/y=%f/%f pointers=%d result=%s",
    //                ev.getActionMasked(), ev.getX(), ev.getY(), ev.getPointerCount(), intercept);
    return intercept;
}

From source file:com.example.SmartBoard.DrawingView.java

/***********************************REMOVE OBJECT************************************/

//removes an object touched from the canvas
public void onRemoveObjectEvent(MotionEvent event) {
    int xRemove = (int) event.getX();
    int yRemove = (int) event.getY();

    JSONObject touchedObject = getTouchedObject(xRemove, yRemove);

    if (touchedObject != null) {
        removeObject(touchedObject.optString("id"));
        mqtt.removeObjectFromServer(touchedObject.optString("id"));
    }/*  w w  w.j av a 2  s.c om*/

}

From source file:com.aigo.kt03airdemo.ui.view.ResideLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    // Preserve the open state based on the last view that was touched.
    if (!mCanSlide && action == MotionEvent.ACTION_DOWN && getChildCount() > 1) {
        // After the first things will be slideable.
        final View secondChild = getChildAt(1);
        if (secondChild != null) {
            mPreservedOpenState = !mDragHelper.isViewUnder(secondChild, (int) ev.getX(), (int) ev.getY());
        }/* www  .  ja  v a 2  s  . c o m*/
    }

    if (!mCanSlide || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();
        return super.onInterceptTouchEvent(ev);
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

    boolean interceptTap = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;
        mInitialMotionY = y;

        if (mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y) && isDimmed(mSlideableView)) {
            interceptTap = true;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final float x = ev.getX();
        final float y = ev.getY();
        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int slop = mDragHelper.getTouchSlop();

        View view = findViewAtPosition(this, (int) x, (int) y);

        if (adx > slop && ady > adx || view != null) {
            if (view != null) {
                Log.d(TAG, "touch on unscrollable view");
            }
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
    }
    }

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);

    return interceptForDrag || interceptTap;
}

From source file:com.example.SmartBoard.DrawingView.java

/***********************************************************************************/
//handles color dropper mode touch events
public void onTouchColorDropperMode(MotionEvent event) {

    int X = (int) event.getX();
    int Y = (int) event.getY();
    dropperX = X;//from   w  ww. ja v a2  s  .c o  m
    dropperY = Y;

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        JSONObject touchedObject = getTouchedObject(X, Y);
        if (touchedObject != null) {

            try {
                touchedObject.put("color", dropperColor);
                //for text
                if (touchedObject.optString("type").compareTo("Text") == 0) {
                    Bitmap bm = textToBitmap(touchedObject.optString("text"), dropperColor,
                            touchedObject.optInt("x"), touchedObject.optInt("y"), touchedObject.optInt("size"));
                    touchedObject.put("textBitmap", mqtt.bitmapToString(bm));
                }
                mqtt.publishObject(touchedObject);
            } catch (JSONException e) {

            }
            invalidate();
        }
    }

}

From source file:com.callba.phone.widget.refreshlayout.RefreshLayout.java

private boolean footerTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    int pointerIndex;
    float y;//w ww .ja  v  a  2s  .  c  o  m
    float yDiff;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsFooterBeingDragged = false;
        mFooterCurrPercentage = 0;
        break;

    case MotionEvent.ACTION_MOVE:
        pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (pointerIndex < 0) {
            Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
            return false;
        }

        y = MotionEventCompat.getY(ev, pointerIndex);
        yDiff = y - mInitialMotionY;

        if (!mIsFooterBeingDragged && yDiff < -mTouchSlop) {
            mIsFooterBeingDragged = true;
        }

        if (mIsFooterBeingDragged) {
            setTriggerPercentage(mAccelerateInterpolator.getInterpolation(
                    MathUtils.clamp(-yDiff, 0, mFooterDistanceToTriggerSync) / mFooterDistanceToTriggerSync));
            mLastMotionY = y;
        }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER && pointerIndex < 0) {
            if (action == MotionEvent.ACTION_UP) {
                Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
            }
            return false;
        }

        try {
            y = MotionEventCompat.getY(ev, pointerIndex);
        } catch (Throwable e) {
            y = 0;
        }

        yDiff = y - mInitialMotionY;

        if (action == MotionEvent.ACTION_UP && -yDiff > mFooterDistanceToTriggerSync) {
            // User movement passed distance; trigger a refresh
            if (mListener != null)
                startFooterRefresh();

        } else {
            mCancel.run();
        }

        mIsFooterBeingDragged = false;
        mFooterCurrPercentage = 0;
        mActivePointerId = INVALID_POINTER;
        return false;
    }

    return mIsFooterBeingDragged;
}

From source file:com.android.dialer.widget.OverlappingPaneLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mCanSlide) {
        return super.onTouchEvent(ev);
    }/*from   ww w  . j av  a 2s  . c om*/

    mDragHelper.processTouchEvent(ev);

    final int action = ev.getAction();
    boolean wantTouchEvents = true;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        mInitialMotionX = x;
        mInitialMotionY = y;
        break;
    }
    }

    return wantTouchEvents;
}

From source file:de.tlabs.ssr.g1.client.SourcesView.java

@Override
public boolean onScroll(MotionEvent firstEvent, MotionEvent thisEvent, float distanceX, float distanceY) {

    // transform sound source or surface?
    if (lastTouchSoundSource != null) { // transform sound source
        synchronized (GlobalData.audioScene) {
            // is lastTouchSoundSource selected?
            if (!lastTouchSoundSource.isSelected()) {
                // select only this source
                GlobalData.audioScene.deselectAllSoundSources();
                GlobalData.audioScene.selectSoundSource(lastTouchSoundSource);
            }/*from w w w  .  jav  a2  s . co  m*/

            // save positions of sources if this is first scroll event
            if (!scrolling) {
                scrolling = true;
                firstScrollPoint[0] = firstEvent.getX();
                firstScrollPoint[1] = firstEvent.getY();
                ArrayList<SoundSource> selectedSources = GlobalData.audioScene.getSelectedSoundSources();
                int numSources = selectedSources.size();
                for (int i = 0; i < numSources; i++) { // loop through all currently selected sources
                    selectedSources.get(i).getXY(point);
                    GlobalData.audioScene.mapPoint(point);
                    viewportTransformation.mapPoints(point);
                    selectedSources.get(i).savePosition(point);
                }
            }

            // enough time elapsed to do next position update?
            long currentTime = SystemClock.uptimeMillis();
            if (currentTime - lastSendTime < MAX_POSITION_UPDATE_FREQ)
                return true;
            lastSendTime = currentTime;

            // translate or rotate?
            if (transformationMode == TransformationMode.TRANSLATE) { // translate
                // generate server request string
                String strMsg = "<request>";
                ArrayList<SoundSource> selectedSources = GlobalData.audioScene.getSelectedSoundSources();
                int numSources = selectedSources.size();
                SoundSource soundSource;
                for (int i = 0; i < numSources; i++) { // loop through all currently selected sources
                    soundSource = selectedSources.get(i);

                    // if source is fixed, skip it
                    if (soundSource.isPositionFixed())
                        continue;

                    strMsg += "<source id='" + soundSource.getId() + "'>";
                    // transform screen coords into object coords, consider offset
                    point[0] = soundSource.getSavedX() + thisEvent.getX() - firstScrollPoint[0];
                    point[1] = soundSource.getSavedY() + thisEvent.getY() - firstScrollPoint[1];
                    inverseViewportTransformation.mapPoints(point);

                    if (soundSource.getSourceModel() == SoundSource.SourceModel.PLANE) { // recalculate orientation for plane waves
                        float norm = FloatMath.sqrt(point[0] * point[0] + point[1] * point[1]); // for plane waves, if source is movable
                        if (norm != 0.0f) {
                            float newAzimuth;
                            if (point[1] >= 0.0f)
                                newAzimuth = (float) (Math.acos(point[0] / norm) / Math.PI * 180.0f) - 180.0f
                                        + GlobalData.audioScene.getReference().getAzimuth();
                            else
                                newAzimuth = (float) -(Math.acos(point[0] / norm) / Math.PI * 180.0f) - 180.0f
                                        + GlobalData.audioScene.getReference().getAzimuth();
                            strMsg += "<orientation azimuth='" + String.valueOf(newAzimuth) + "'/>";
                        }
                    }

                    GlobalData.audioScene.inverseMapPoint(point);
                    strMsg += "<position x='" + String.valueOf(point[0]) + "' y='" + String.valueOf(point[1])
                            + "'/>";
                    strMsg += "</source>";
                }
                strMsg += "</request>\0";

                // send changes to server
                sendToServer(strMsg);
            } else { // rotate
                // not implemented
            }
        }
    } else { // transform surface
        if (!scrolling) {
            scrolling = true;
            firstScrollPoint[0] = thisEvent.getX();
            firstScrollPoint[1] = thisEvent.getY();
            currentSavedTranslation[0] = currentTranslation[0];
            currentSavedTranslation[1] = currentTranslation[1];
        }

        // translate or rotate?
        if (transformationMode == TransformationMode.TRANSLATE) { // translate
            if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
                point[0] = thisEvent.getX() - firstScrollPoint[0];
                point[1] = thisEvent.getY() - firstScrollPoint[1];
            } else if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                point[0] = thisEvent.getY() - firstScrollPoint[1];
                point[1] = -(thisEvent.getX() - firstScrollPoint[0]);
            }
            setCurrentTranslation(currentSavedTranslation[0] + point[0], currentSavedTranslation[1] + point[1]);
        } else { // rotate
            // not implemented
        }
    }

    return true;
}

From source file:com.appunite.scroll.ScaleImageView.java

@SuppressWarnings("UnusedDeclaration")
public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    final ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
    mMinEdge = viewConfiguration.getScaledEdgeSlop();

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScaleImageView, defStyle, defStyle);
    assert a != null;

    Drawable src = null;//w  ww  .  jav  a  2 s  .c  o  m
    try {
        mMinWidth = a.getDimensionPixelSize(R.styleable.ScaleImageView_android_minWidth, 0);
        mMinHeight = a.getDimensionPixelSize(R.styleable.ScaleImageView_android_minHeight, 0);
        src = a.getDrawable(R.styleable.ScaleImageView_android_src);
    } finally {
        a.recycle();
    }

    ScaleGestureDetector.OnScaleGestureListener scaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        /**
         * This is the active focal point in terms of the viewport. Could be a local
         * variable but kept here to minimize per-frame allocations.
         */
        private PointF viewportFocus = new PointF();

        @Override
        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
            float focusX = scaleGestureDetector.getFocusX();
            float focusY = scaleGestureDetector.getFocusY();
            float scaleFactor = scaleGestureDetector.getScaleFactor();
            float previousScale = mScale;
            mScale *= scaleFactor;

            doScale(focusX, focusY, scaleFactor);
            return true;
        }

    };
    GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {

            releaseEdgeEffects();
            mScroller.forceFinished(true);
            ViewCompat.postInvalidateOnAnimation(ScaleImageView.this);
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return performClick();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            mZoomer.forceFinished(true);
            mZoomStartScale = mScale;
            mZoomFocalPoint.set(e.getX(), e.getY());
            mZoomer.startZoom(ZOOM_AMOUNT);
            ViewCompat.postInvalidateOnAnimation(ScaleImageView.this);
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            getRealTranslation(mTranslation, mRealTranslation);
            mRealTranslation.offset(-distanceX, -distanceY);
            getTranslation(mRealTranslation, mTranslation);

            computeMaxScrollSize(mMaxScrollBuffer);
            getImageRect(mRectF);
            float scrolledX = -mRectF.left;
            float scrolledY = -mRectF.top;
            boolean canScrollX = mRectF.left > mContentRect.left || mRectF.right < mContentRect.right;
            boolean canScrollY = mRectF.top > mContentRect.top || mRectF.bottom < mContentRect.bottom;
            validateTranslation();

            disallowParentInterceptWhenOnEdge(distanceX, distanceY);

            if (mScale > mMinScale) {
                if (canScrollX && scrolledX < 0) {
                    mEdgeEffectLeft.onPull(scrolledX / (float) mContentRect.width());
                    mEdgeEffectLeftActive = true;
                }
                if (canScrollY && scrolledY < 0) {
                    mEdgeEffectTop.onPull(scrolledY / (float) mContentRect.height());
                    mEdgeEffectTopActive = true;
                }
                if (canScrollX && scrolledX > mMaxScrollBuffer.x) {
                    mEdgeEffectRight.onPull((scrolledX - mMaxScrollBuffer.x) / (float) mContentRect.width());
                    mEdgeEffectRightActive = true;
                }
                if (canScrollY && scrolledY > mMaxScrollBuffer.y) {
                    mEdgeEffectBottom.onPull((scrolledY - mMaxScrollBuffer.y) / (float) mContentRect.height());
                    mEdgeEffectBottomActive = true;
                }
            }

            ViewCompat.postInvalidateOnAnimation(ScaleImageView.this);
            return true;
        }

        private void disallowParentInterceptWhenOnEdge(float directionX, float directionY) {
            final ViewParent parent = getParent();
            if (parent != null && (mAllowParentHorizontalScroll || mAllowParentVerticalScroll)) {
                getImageRect(mRectF);
                if (mAllowParentHorizontalScroll) {
                    if (directionX > 0 && Math.abs(mRectF.right - mContentRect.right) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                    if (directionX < 0 && Math.abs(mRectF.left - mContentRect.left) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
                if (mAllowParentVerticalScroll) {
                    if (directionY > 0 && Math.abs(mRectF.bottom - mContentRect.bottom) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                    if (directionY < 0 && Math.abs(mRectF.top - mContentRect.top) > mMinEdge) {
                        parent.requestDisallowInterceptTouchEvent(true);
                    }
                }
            }
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            disallowParentInterceptWhenOnEdge(velocityX, velocityY);
            fling((int) -velocityX, (int) -velocityY);
            return true;
        }
    };

    mScaleGestureDetector = new ScaleGestureDetector(context, scaleGestureListener);
    mGestureDetector = new GestureDetectorCompat(context, gestureListener);

    mScroller = new OverScroller(context);
    mZoomer = new Zoomer(context);

    // Sets up edge effects
    mEdgeEffectLeft = new EdgeEffectCompat(context);
    mEdgeEffectTop = new EdgeEffectCompat(context);
    mEdgeEffectRight = new EdgeEffectCompat(context);
    mEdgeEffectBottom = new EdgeEffectCompat(context);

    setSrcDrawable(src);
}

From source file:ca.mymenuapp.ui.widgets.SlidingUpPanelLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);

    if (!mCanSlide || !mIsSlidingEnabled || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
        mDragHelper.cancel();/*from ww w. j a va 2  s  .co  m*/
        return super.onInterceptTouchEvent(ev);
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mDragHelper.cancel();
        return false;
    }

    final float x = ev.getX();
    final float y = ev.getY();
    boolean interceptTap = false;

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        mIsUnableToDrag = false;
        mInitialMotionX = x;
        mInitialMotionY = y;
        if (isDragViewUnder((int) x, (int) y) && !mIsUsingDragViewTouchEvents) {
            interceptTap = true;
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final float adx = Math.abs(x - mInitialMotionX);
        final float ady = Math.abs(y - mInitialMotionY);
        final int dragSlop = mDragHelper.getTouchSlop();

        // Handle any horizontal scrolling on the drag view.
        if (mIsUsingDragViewTouchEvents) {
            if (adx > mScrollTouchSlop && ady < mScrollTouchSlop) {
                return super.onInterceptTouchEvent(ev);
            }
            // Intercept the touch if the drag view has any vertical scroll.
            // onTouchEvent will determine if the view should drag vertically.
            else if (ady > mScrollTouchSlop) {
                interceptTap = isDragViewUnder((int) x, (int) y);
            }
        }

        if ((ady > dragSlop && adx > ady) || !isDragViewUnder((int) x, (int) y)) {
            mDragHelper.cancel();
            mIsUnableToDrag = true;
            return false;
        }
        break;
    }
    }

    final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);

    return interceptForDrag || interceptTap;
}

From source file:com.android.contacts.common.list.ContactListItemView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final float x = event.getX();
    final float y = event.getY();
    // If the touch event's coordinates are not within the view's header, then delegate
    // to super.onTouchEvent so that regular view behavior is preserved. Otherwise, consume
    // and ignore the touch event.
    if (mBoundsWithoutHeader.contains((int) x, (int) y) || !pointIsInView(x, y)) {
        return super.onTouchEvent(event);
    } else {//from  w  w  w . j av  a2s  .co m
        return true;
    }
}