Java Code Examples for android.view.MotionEvent

The following examples show how to use android.view.MotionEvent. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: ar-drawing-java   Source File: DrawAR.java    License: 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 2
Source Project: spline   Source File: SaturationValuePicker.java    License: 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 Project: NewFastFrame   Source File: SuperRecyclerView.java    License: 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 4
Source Project: dynamiclistview   Source File: DynamicListLayout.java    License: 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 5
Source Project: AndroidSwipeLayout   Source File: SwipeLayout.java    License: 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
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 7
/**
 * 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 8
Source Project: AndroidPdfViewerV2   Source File: DragPinchManager.java    License: 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 9
Source Project: smart-farmer-android   Source File: CleanEditText.java    License: 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 10
Source Project: PinchToZoom   Source File: ImageMatrixTouchHandler.java    License: 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 11
Source Project: XRadarView   Source File: XRadarView.java    License: 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 12
Source Project: ncalc   Source File: Graph3DView.java    License: 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 13
Source Project: ImagePicker   Source File: ImagePickerViewPager.java    License: 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 14
Source Project: HeroVideo-master   Source File: MediaPlayerView.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (isInPlaybackState() && mMediaController != null) {
        toggleMediaControlsVisiblity();
    }
    return false;
}
 
Example 15
Source Project: Ticket-Analysis   Source File: SuperSwipeRefreshLayout.java    License: 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 16
Source Project: AndroidBeautifulSearch   Source File: DribSearchView.java    License: 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 17
Source Project: fangzhuishushenqi   Source File: VerticalSeekbar.java    License: 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 18
Source Project: android-recipes-app   Source File: MotionEventCompat.java    License: 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 19
Source Project: ExpressHelper   Source File: ViewDragHelper.java    License: 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 20
Source Project: NewsMe   Source File: SwipeLayout.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
	if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
		start();
	}
	try {
		mDragHelper.processTouchEvent(event);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return true;
}
 
Example 21
Source Project: Melophile   Source File: HomePager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
  if (this.enabled) {
    return super.onInterceptTouchEvent(event);
  }

  return false;
}
 
Example 22
Source Project: wallpaper   Source File: UGallery.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	int keyCode;
	if (isScrollingLeft(e1, e2)) {
		keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
	} else {
		keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
	}
	onKeyDown(keyCode, null);
	return true;
}
 
Example 23
Source Project: android-bottomsheet   Source File: ResolverDrawerLayout.java    License: ISC License 5 votes vote down vote up
private void resetTouch() {
    mActivePointerId = MotionEvent.INVALID_POINTER_ID;
    mIsDragging = false;
    mOpenOnClick = false;
    mInitialTouchX = mInitialTouchY = mLastTouchY = 0;
    mVelocityTracker.clear();
}
 
Example 24
Source Project: imsdk-android   Source File: PhotoView.java    License: 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 25
@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 26
Source Project: YViewPagerDemo   Source File: YViewPager.java    License: 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 27
Source Project: meizhi   Source File: TouchImageView.java    License: 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 28
Source Project: aurora-imui   Source File: ImgBrowserViewPager.java    License: 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 29
Source Project: Indic-Keyboard   Source File: InputView.java    License: 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 30
Source Project: ClipCircleHeadLikeQQ   Source File: ClipHeaderActivity.java    License: 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;
}