android.view.MotionEvent Java Examples

The following examples show how to use android.view.MotionEvent. 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: ChartView.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
/**
 * to be called to process the events
 *
 * @param event
 * @return true if there was a tap event. otherwise returns false.
 */
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        lastDown = System.currentTimeMillis();
        lastPoint = new PointF(event.getX(), event.getY());
    } else if (lastDown > 0 && event.getAction() == MotionEvent.ACTION_MOVE) {
        if (Math.abs(event.getX() - lastPoint.x) > 60
                || Math.abs(event.getY() - lastPoint.y) > 60) {
            lastDown = 0;
        }
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (System.currentTimeMillis() - lastDown < 400) {
            return true;
        }
    }
    return false;
}
 
Example #2
Source File: SaturationValuePicker.java    From spline with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX() - mPadding;
    float y = event.getY() - mPadding;

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Reject touches in the padding region
            if (x < 0 || y < 0 || x > mWidth + mInset * 2 || y > mHeight + mInset * 2) {
                return false;
            }
        case MotionEvent.ACTION_MOVE:
            setSaturation(getSaturationForX(x - mInset));
            setValue(getValueForY(y - mInset));
            break;
    }

    return true;
}
 
Example #3
Source File: ImageMatrixTouchHandler.java    From PinchToZoom with MIT License 6 votes vote down vote up
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	if (mode == DRAG) {
		if (flingDuration > 0 && !isAnimating()) {
			float factor = ((float) flingDuration / 1000f) * flingExaggeration;
			float[] values = corrector.getValues();
			float dx = (velocityX * factor) * values[Matrix.MSCALE_X];
			float dy = (velocityY * factor) * values[Matrix.MSCALE_Y];
			PropertyValuesHolder flingX = PropertyValuesHolder.ofFloat(FlingAnimatorHandler.PROPERTY_TRANSLATE_X, values[Matrix.MTRANS_X], values[Matrix.MTRANS_X] + dx);
			PropertyValuesHolder flingY = PropertyValuesHolder.ofFloat(FlingAnimatorHandler.PROPERTY_TRANSLATE_Y, values[Matrix.MTRANS_Y], values[Matrix.MTRANS_Y] + dy);
			valueAnimator = ValueAnimator.ofPropertyValuesHolder(flingX, flingY);
			valueAnimator.setDuration(flingDuration);
			valueAnimator.addUpdateListener(new FlingAnimatorHandler(corrector));
			valueAnimator.setInterpolator(new DecelerateInterpolator());
			valueAnimator.start();
			return true;
		}
	}
	return super.onFling(e1, e2, velocityX, velocityY);
}
 
Example #4
Source File: CleanEditText.java    From smart-farmer-android with Apache License 2.0 6 votes vote down vote up
/**
 * @说明:isInnerWidth, isInnerHeight为ture,触摸点在删除图标之内,则视为点击了删除图标
 * event.getX() 获取相对应自身左上角的X坐标
 * event.getY() 获取相对应自身左上角的Y坐标
 * getWidth() 获取控件的宽度
 * getHeight() 获取控件的高度
 * getTotalPaddingRight() 获取删除图标左边缘到控件右边缘的距离
 * getPaddingRight() 获取删除图标右边缘到控件右边缘的距离
 * isInnerWidth:
 * getWidth() - getTotalPaddingRight() 计算删除图标左边缘到控件左边缘的距离
 * getWidth() - getPaddingRight() 计算删除图标右边缘到控件左边缘的距离
 * isInnerHeight:
 * distance 删除图标顶部边缘到控件顶部边缘的距离
 * distance + height 删除图标底部边缘到控件顶部边缘的距离
 **/
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (getCompoundDrawables()[2] != null) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            Rect rect = getCompoundDrawables()[2].getBounds();
            int height = rect.height();
            int distance = (getHeight() - height) / 2;
            boolean isInnerWidth = x > (getWidth() - getTotalPaddingRight()) && x < (getWidth() - getPaddingRight());
            boolean isInnerHeight = y > distance && y < (distance + height);
            if (isInnerWidth && isInnerHeight) {
                this.setText("");
            }
        }
    }
    return super.onTouchEvent(event);
}
 
Example #5
Source File: SwipeLayout.java    From AndroidSwipeLayout with MIT License 6 votes vote down vote up
@Override
public boolean onDoubleTap(MotionEvent e) {
    if (mDoubleClickListener != null) {
        View target;
        View bottom = getCurrentBottomView();
        View surface = getSurfaceView();
        if (bottom != null && e.getX() > bottom.getLeft() && e.getX() < bottom.getRight()
                && e.getY() > bottom.getTop() && e.getY() < bottom.getBottom()) {
            target = bottom;
        } else {
            target = surface;
        }
        mDoubleClickListener.onDoubleClick(SwipeLayout.this, target == surface);
    }
    return true;
}
 
Example #6
Source File: DragPinchManager.java    From AndroidPdfViewerV2 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    OnTapListener onTapListener = pdfView.getOnTapListener();
    if (onTapListener == null || !onTapListener.onTap(e)) {
        ScrollHandle ps = pdfView.getScrollHandle();
        if (ps != null && !pdfView.documentFitsView()) {
            if (!ps.shown()) {
                ps.show();
            } else {
                ps.hide();
            }
        }
    }
    pdfView.performClick();
    return true;
}
 
Example #7
Source File: SwipeOpenItemTouchHelper.java    From SwipeOpenItemTouchHelper with Apache License 2.0 6 votes vote down vote up
private void updateDxDy(MotionEvent ev, int directionFlags, int pointerIndex) {
  final float x = ev.getX(pointerIndex);
  final float y = ev.getY(pointerIndex);
  // Calculate the distance moved
  dX = x - initialTouchX;
  dY = y - initialTouchY;
  if ((directionFlags & LEFT) == 0) {
    dX = Math.max(0, dX);
  }
  if ((directionFlags & RIGHT) == 0) {
    dX = Math.min(0, dX);
  }
  if ((directionFlags & UP) == 0) {
    dY = Math.max(0, dY);
  }
  if ((directionFlags & DOWN) == 0) {
    dY = Math.min(0, dY);
  }
}
 
Example #8
Source File: DynamicListLayout.java    From dynamiclistview with MIT License 6 votes vote down vote up
public void setEvent(MotionEvent event, Listener listener) {
    float y = event.getY();

    if (savedY == 0)
        savedY = y;

    if (Math.abs(savedY - y) < 10)
        return;

    ScrollDirection nowDirection = savedY > event.getY() ? ScrollDirection.DOWN : ScrollDirection.UP;

    if (nowDirection != this.scrollDirection && listener != null)
        listener.onScrollDirectionChanged(DynamicListLayout.this, mDynamicListView, nowDirection);

    this.scrollDirection = nowDirection;
    this.savedY = event.getY();
}
 
Example #9
Source File: XRadarView.java    From XRadarView with MIT License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            int x = (int) event.getX();
            int y = (int) event.getY();
            for (int i = 0; i < titleRects.size(); i++) {
                Rect rect = titleRects.get(i);
                if (rect != null && rect.contains(x, y)) {
                    if (onTitleClickListener != null) {
                        onTitleClickListener.onTitleClick(XRadarView.this, i, x, y, rect);
                        return true;
                    }
                }
            }
            break;
    }
    return super.onTouchEvent(event);
}
 
Example #10
Source File: Graph3DView.java    From ncalc with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        renderer.stopRotate();
        startX = event.getX();
        startY = event.getY();
        return true;
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        // Moves the trace or derivative

        renderer.move((event.getX() - startX) / width * 360, (event.getY() - startY) / height * 360);
        startX = event.getX();
        startY = event.getY();
        return true;
        //Resets values when users finishes their gesture
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        pinchDist = -1;
        startX = -1;
        startY = -1;
    }
    return true;
}
 
Example #11
Source File: SuperRecyclerView.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    if (mRefreshEnabled) {
        final int action = MotionEventCompat.getActionMasked(e);
        final int actionIndex = MotionEventCompat.getActionIndex(e);
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                mActivePointerId = MotionEventCompat.getPointerId(e, 0);
                mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
                mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
            }
            break;
            case MotionEvent.ACTION_POINTER_DOWN: {
                mActivePointerId = MotionEventCompat.getPointerId(e, actionIndex);
                mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
                mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
            }
            break;
            case MotionEventCompat.ACTION_POINTER_UP: {
                onPointerUp(e);
            }
            break;
        }
    }
    return super.onInterceptTouchEvent(e);
}
 
Example #12
Source File: DrawAR.java    From ar-drawing-java with Apache License 2.0 6 votes vote down vote up
/**
 * onTouchEvent handles saving the lastTouch screen position and setting bTouchDown and bNewStroke
 * AtomicBooleans to trigger addPoint and addStroke on the GL Thread to be called
 */
@Override
public boolean onTouchEvent(MotionEvent tap) {
    this.mDetector.onTouchEvent(tap);

    if (tap.getAction() == MotionEvent.ACTION_DOWN ) {
        lastTouch.set(new Vector2f(tap.getX(), tap.getY()));
        bTouchDown.set(true);
        bNewStroke.set(true);
        return true;
    } else if (tap.getAction() == MotionEvent.ACTION_MOVE || tap.getAction() == MotionEvent.ACTION_POINTER_DOWN) {
        lastTouch.set(new Vector2f(tap.getX(), tap.getY()));
        bTouchDown.set(true);
        return true;
    } else if (tap.getAction() == MotionEvent.ACTION_UP || tap.getAction() == MotionEvent.ACTION_CANCEL) {
        bTouchDown.set(false);
        lastTouch.set(new Vector2f(tap.getX(), tap.getY()));
        return true;
    }

    return super.onTouchEvent(tap);
}
 
Example #13
Source File: RangeSeekBar.java    From openshop.io-android with MIT License 5 votes vote down vote up
private void trackTouchEvent(MotionEvent event) {
    final int pointerIndex = event.findPointerIndex(mActivePointerId);
    final float x = event.getX(pointerIndex);

    if (Thumb.MIN.equals(pressedThumb) && !mSingleThumb) {
        setNormalizedMinValue(screenToNormalized(x));
    } else if (Thumb.MAX.equals(pressedThumb)) {
        setNormalizedMaxValue(screenToNormalized(x));
    }
}
 
Example #14
Source File: MotionEventCompat.java    From android-recipes-app with Apache License 2.0 5 votes vote down vote up
@Override
public float getX(MotionEvent event, int pointerIndex) {
    if (pointerIndex == 0) {
        return event.getX();
    }
    throw new IndexOutOfBoundsException("Pre-Eclair does not support multiple pointers");
}
 
Example #15
Source File: ViewDragHelper.java    From ExpressHelper with GNU General Public License v3.0 5 votes vote down vote up
private void saveLastMotion(MotionEvent ev) {
    final int pointerCount = ev.getPointerCount();
    for (int i = 0; i < pointerCount; i++) {
        final int pointerId = ev.getPointerId(i);
        final float x = ev.getX(i);
        final float y = ev.getY(i);
        mLastMotionX[pointerId] = x;
        mLastMotionY[pointerId] = y;
    }
}
 
Example #16
Source File: VerticalSeekbar.java    From fangzhuishushenqi with Apache License 2.0 5 votes vote down vote up
private void trackTouchEvent(MotionEvent event) {
    final int height = getHeight();
    final int top = getPaddingTop();
    final int bottom = getPaddingBottom();
    final int available = height - top - bottom;

    int y = (int) event.getY();

    float scale;
    float progress = 0;

    // 下面是最小值
    if (y > height - bottom) {
        scale = 1.0f;
    } else if (y < top) {
        scale = 0.0f;
    } else {
        scale = (float) (y - top) / (float) available;
        progress = mTouchProgressOffset;
    }

    final int max = getMax();
    progress += scale * max;

    setProgress((int) progress);
    // 便于监听用户触摸改变进度
    mOnSeekBarChangeListener.onProgressChanged(this, (int) progress, true);

}
 
Example #17
Source File: DribSearchView.java    From AndroidBeautifulSearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float mx = event.getX();
        float my = event.getY();
        if (rect.contains(mx, my) && mClickSearchListener != null && mState == SEARCH) {
            mClickSearchListener.onClickSearch();
        }
    }
    if (other != null) {
        return other.onTouch(v, event);
    } else {
        return false;
    }
}
 
Example #18
Source File: PhotoView.java    From imsdk-android with MIT License 5 votes vote down vote up
private boolean onTouchEventInternal(@NonNull MotionEvent event) {
    int touchCount = event.getPointerCount();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_1_DOWN:
        case MotionEvent.ACTION_POINTER_2_DOWN:
            maxTouchCount = Math.max(maxTouchCount, touchCount);
            return true;
        default:
            break;
    }
    return false;
}
 
Example #19
Source File: FuckViewPager.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
        }
    }
}
 
Example #20
Source File: EclairGestureDetector.java    From MoeGallery with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = ev.getPointerId(0);
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            mActivePointerId = INVALID_POINTER_ID;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // Ignore deprecation, ACTION_POINTER_ID_MASK and
            // ACTION_POINTER_ID_SHIFT has same value and are deprecated
            // You can have either deprecation or lint target api warning
            final int pointerIndex = Compat.getPointerIndex(ev.getAction());
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = ev.getPointerId(newPointerIndex);
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
            }
            break;
    }

    mActivePointerIndex = ev
            .findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId
                    : 0);
    return super.onTouchEvent(ev);
}
 
Example #21
Source File: SuperSwipeRefreshLayout.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
    if (pointerId == mActivePointerId) {
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mActivePointerId = MotionEventCompat.getPointerId(ev,
                newPointerIndex);
    }
}
 
Example #22
Source File: YViewPager.java    From YViewPagerDemo with Apache License 2.0 5 votes vote down vote up
private void onSecondaryPointerUpVertical(MotionEvent ev) {
    final int pointerIndex = MotionEventCompat.getActionIndex(ev);
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastMotionY = ev.getY(newPointerIndex);
        mActivePointerId = ev.getPointerId(newPointerIndex);
        if (mVelocityTracker != null) {
            mVelocityTracker.clear();
        }
    }
}
 
Example #23
Source File: TouchImageView.java    From meizhi with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    if (doubleTapListener != null) {
        return doubleTapListener.onDoubleTapEvent(e);
    }
    return false;
}
 
Example #24
Source File: ImgBrowserViewPager.java    From aurora-imui with MIT License 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
	try {
		return super.onInterceptTouchEvent(ev);
	} catch (IllegalArgumentException e) {
		e.printStackTrace();
		return false;
	}
}
 
Example #25
Source File: InputView.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
public boolean onTouchEvent(final int x, final int y, final MotionEvent me) {
    mReceiverView.getGlobalVisibleRect(mEventReceivingRect);
    // Translate global coordinates to <code>ReceiverView</code> local coordinates.
    me.setLocation(translateX(x), translateY(y));
    mReceiverView.dispatchTouchEvent(me);
    onForwardingEvent(me);
    return true;
}
 
Example #26
Source File: ClipHeaderActivity.java    From ClipCircleHeadLikeQQ with Apache License 2.0 5 votes vote down vote up
public boolean onTouch(View v, MotionEvent event) {
    ImageView view = (ImageView) v;
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            // 设置开始点位置
            start.set(event.getX(), event.getY());
            mode = DRAG;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);
            if (oldDist > 10f) {
                savedMatrix.set(matrix);
                midPoint(mid, event);
                mode = ZOOM;
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
                matrix.set(savedMatrix);
                matrix.postTranslate(event.getX() - start.x, event.getY()
                        - start.y);
            } else if (mode == ZOOM) {
                float newDist = spacing(event);
                if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = newDist / oldDist;
                    matrix.postScale(scale, scale, mid.x, mid.y);
                }
            }
            break;
    }
    view.setImageMatrix(matrix);
    return true;
}
 
Example #27
Source File: CustomGestureDetector.java    From PhotoViewer with Apache License 2.0 5 votes vote down vote up
private float getActiveY(MotionEvent ev) {
    try {
        return ev.getY(mActivePointerIndex);
    } catch (Exception e) {
        return ev.getY();
    }
}
 
Example #28
Source File: DragTouchAdapter.java    From SwipeRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mMenuRecyclerView.startDrag(this);
            break;
        }
    }
    return false;
}
 
Example #29
Source File: ImagePickerViewPager.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
    try
    {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException ex)
    {
        ex.printStackTrace();
    }
    return false;
}
 
Example #30
Source File: ZoomViewPager.java    From ZoomImageView with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    try {
        return super.onInterceptTouchEvent(event);
    } catch (Exception e) {
        return false;
    }
}